1

I have an array like this:

Array(
   [0] => Array(
       [0] => Orange,
       [1] => Mango,
       [2] => Banana
   )
   [1] => Array(
       [0] => Orange Tree
       [1] => Banana Tree
   )
)

How can i do it like below without using like Array[1][2] = mango tree:

   Array(
   [0] => Array(
       [0] => Orange,
       [1] => Mango,
       [2] => Banana
   )
   [1] => Array(
       [0] => Orange Tree
       [1] => Banana Tree
       [2] => Mango Tree
   )
)

That means that I have to insert this without using Array[1][2] = Mango tree or something like that. I want a way which can be used in foreach loops.

1
  • You have no structured way of determining which array contains trees, so your best bet is $myArray[1][] = 'Mango Tree' at the moment.
    – scrowler
    Commented Sep 21, 2016 at 0:54

1 Answer 1

2

Try using [] to append to an array.

<?php
$data = array(
    array(
        'Orange',
        'Mango',
        'Banana'
    ),
    array()
);

foreach($data[0] as $fruit) {
    $data[1][] = $fruit . ' Tree';
}

echo '<pre>';
print_r($data);
echo '</pre>';