PHP array_push in array inside another array

0

Good evening, I need to create a code in php that meets the following situation:

I have an array of arrays $ arrayTudo = [$ array1, $ array2, $ array3];

I calculate an X value and I want to throw it in array1, for example. It could be in any of them. How to do array_push in this case? Is it possible to throw this value X into an array inside another array?

Before coding, I need to understand how to do this.

It would look something like this:

<%php
$array1 = [];
$array2 = [];
$array3 = [];

$arrayTudo = [$array1, $array2, $array3];

$x = 10;


array_push($arrayTudo[0], $x);
%>

I select one of the arrays to receive the value X. The first array, for example. Is this possible?

    
asked by anonymous 24.03.2018 / 01:44

2 answers

0

You can use array_merge in this case. See:

$arrayTudo = [array1, array2, array3];

$x = 10;
$y = 20;

$arrayValores = [$arrayTudo[0] => [$x, $y]];

$result = array_merge($arrayTudo, $arrayValores);

The results:

var_dump($result[array1]);

Output:

array (size=2)
  0 => int 10
  1 => int 20 

All:

 var_dump($result);

Output:

array (size=4)
  0 => string 'array1' (length=6)
  1 => string 'array2' (length=6)
  2 => string 'array3' (length=6)
  'array1' => 
    array (size=2)
      0 => int 10
      1 => int 20 
    
24.03.2018 / 02:11
0

Search for "array", sometimes it helps you ...

I would stay + - like this:

$array[0][0] = 10; 
$array[0][1] = 20;

Watch this video

In a repeat structure, you will have to know how to put each one:

$a = 0;
while ($a < 3) {

   $b = 0;
   while ($b < 5) {

   $array[$a][$b] = $b*2;

   $b++;
   }

$a++;
}

echo '<pre>';
print_r($array);

Result:

Array
(
    [0] => Array
        (
            [0] => 0
            [1] => 2
            [2] => 4
            [3] => 6
            [4] => 8
        )

    [1] => Array
        (
            [0] => 0
            [1] => 2
            [2] => 4
            [3] => 6
            [4] => 8
        )

    [2] => Array
        (
            [0] => 0
            [1] => 2
            [2] => 4
            [3] => 6
            [4] => 8
        )

)
    
24.03.2018 / 01:49