i have a recursivearray that does not return object/elements in a specific array structure:
Array im trying to use recursive is as follow:
$feedStruct = [database] => Array
(
[property] => Array
(
[0] => Array
(
[title] => Array
(
)
[tipology] => Array
(
)
[description] => Array
(
)
[location] => Array
(
)
[year] => Array
(
)
[price] => Array
(
)
[area_plot] => Array
(
)
[habitable_area] => Array
(
)
[ref] => Array
(
)
[video] => Array
(
)
[default_photo] => Array
(
)
[photo1] => Array
(
)
)
[1] => Array
(
[title] => Array
(
)
[tipology] => Array
(
)
[description] => Array
(
)
[location] => Array
(
)
[year] => Array
(
)
[price] => Array
(
)
[area_plot] => Array
(
)
[habitable_area] => Array
(
)
[ref] => Array
(
)
[video] => Array
(
)
[default_photo] => Array
(
)
[photo1] => Array
(
)
)
)
)
Code as follow:
$iterator = new RecursiveIteratorIterator(new RecursiveArrayIterator($feedStruct));
$keys = array();
foreach ($iterator as $key => $value) {
// Build long key name based on parent keys
for ($i = $iterator->getDepth() - 1; $i >= 0; $i--) {
$val = $iterator->getSubIterator($i)->key();
}
}
When this run with the above array it does not iterate, with other arrays i dont have a problem, any ideas why?
Thank you
your $iterator = new RecursiveIteratorIterator(new RecursiveArrayIterator($feedStruct));
will be interpreted as $iterator = new RecursiveIteratorIterator(new RecursiveArrayIterator($feedStruct), RecursiveIteratorIterator::SELF_FIRST);
because RecursiveIteratorIterator::LEAVES_ONLY
is the default parameter out of three (ie RecursiveIteratorIterator::SELF_FIRST
, RecursiveIteratorIterator::CHILD_FIRST
).
I will try explaining the parameters. Assume you have the array below
$array = array(
0 => array(
'city' => 'Jos',
'places_to_visit' => array(
'Lagos',
'Capetown'
)
),
1 => "sd"
);
$iterator = new RecursiveIteratorIterator(new RecursiveArrayIterator($feedStruct), $mode);
$keys = array();
foreach ($iterator as $key => $value) {
// Build long key name based on parent keys
for ($i = $iterator->getDepth() - 1; $i >= 0; $i--) {
$val[] = $iterator->getSubIterator($i)->key();
}
}
RecursiveIteratorIterator::LEAVES_ONLY
means iterate only last values in each structure. In the case of the above array $array
, if $mode = RecursiveIteratorIterator::LEAVES_ONLY
,
print_r($val);
/* Outputs
city => Jos
0 => Lagos
1 => Capetown
1 => sd */
RecursiveIteratorIterator::CHILD_FIRST
means iterate child values first before parents. In the case of the above array $array
, if $mode = RecursiveIteratorIterator::CHILD_FIRST
,
print_r($val);
/* Outputs
city => Jos
0 => Lagos
1 => Capetown
places_to_visit => Array()
0 => Array()
1 => sd */
RecursiveIteratorIterator::SELF_FIRST
means iterate only parent values first in each structure. In the case of the above array $array
, if $mode = RecursiveIteratorIterator::SELF_FIRST
,
print_r($val);
/*Outputs
0 => Array()
city => Jos
places_to_visit => Array()
0 => Lagos
1 => Capetown
1 => sd */
In your case, $iterator
will return 0 as there are no leaves (final values) in your structure $feedStruct
. Try RecursiveIteratorIterator::SELF_FIRST
, works quite well.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.