I have an array like this:
[apple] => 11
[pear] => 5
[banana] => 3
[cucumber] => 2
[tomatoes] => 8
I would like to create multi-dimensional array like this:
[randomArrayName1]
[apple] => 11
[banana] => 3
[tomatoes] => 8
[randomArrayName2]
[pears] => 5
[cucumber] => 2
I wrote this below but it is not working and I have no clue how to make it do: what I need it to do
$quantity = array();
foreach($fruit as $key => $value) {
$quantity = Finder($key);
}
randomArrayName1
and randomArrayName2
will be generated depending on Finder()
function's result. Depending on that result I want fruit array to be created as a second dimension array.
Solution: The problem was that the function I wrote that created names like randomArrayName1, randomArrayName2... was in fact a XML function. Therefore, result of this function was not an array, it was XML simple object. This caused problem on creating multidimensional array. Once I implemented the code below it converted SimpleXML to Array and code worked fine. $type is SimpleXML array $out is result array
foreach ( (array) $type as $index => $node )
$out[$index] = ( is_object ( $node ) ) ? xml2array ( $node ) : $node;