Reputation: 765
I'm converting XML file into associative array to pull out the data, the problem is that I have to make 10 loops depends on arrays number in order to get the data.
is there a better way to get a specific column data without creating many loops? because I want to assign them to variables.
the array I'm trying to get data from
Array
(
[catalog] => Array
(
[book] => Array
(
[0] => Array
(
[took] => Array
(
[dodo] => Array
(
[ahmadz] => Array
(
[lolo] => Array
(
[author] => Ralls, Kim
[title] => Midnight Rain
[genre] => Fantasy
[price] => 5.95
[publish_date] => 2000-12-16
[description] => A former architect battles corporate zombies,
an evil sorceress, and her own childhood to become queen
of the world.
)
)
)
)
)
[1] => Array
(
[took] => Array
(
[dodo] => Array
(
[ahmadz] => Array
(
[lolo] => Array
(
[author] => Ralls, Kim
[title] => Midnight Rain
[genre] => Fantasy
[price] => 5.95
[publish_date] => 2000-12-16
[description] => A former architect battles corporate zombies,
an evil sorceress, and her own childhood to become queen
of the world.
)
)
)
)
)
)
)
)
I removed all other data to make it easier to read, but there are many other values in the array. Anyway, how can I get the value of author for example.
echo $array['author'];
assuming that I have many author data, not one as the example above
Please help!.
Edited.....................
Array
(
[catalog] => Array
(
[book] => Array
(
[0] => Array
(
[took] => Array
(
[dodo] => Array
(
[ahmadz] => Array
(
[lolo] => Array
(
[tata] => Array
(
[author] => jac1
[title] => Midnight Rain1
[genre] => Fantasy
[price] => 5.95
[publish_date] => 2000-12-16
[description] => A former architect battles corporate zombies.
)
[tata2] => Array
(
[author] => jack2
[title] => Midnight Rain1
[genre] => Fantasy
[price] => 5.95
[publish_date] => 2000-12-16
[description] => A former architect battles corporate zombies.
)
)
)
)
)
)
[1] => Array
(
[took] => Array
(
[dodo] => Array
(
[ahmadz] => Array
(
[lolo] => Array
(
[tata] => Array
(
[author] => jack3
[title] => Midnight Rain1
[genre] => Fantasy
[price] => 5.95
[publish_date] => 2000-12-16
[description] => A former architect battles corporate zombies.
)
[tata2] => Array
(
[author] => jack4
[title] => Midnight Rain1
[genre] => Fantasy
[price] => 5.95
[publish_date] => 2000-12-16
[description] => A former architect battles corporate zombies.
)
)
)
)
)
)
)
)
)
As you see above I just want to get the value that has parent keys tata not tata2
so I can insert them separately into the database
Upvotes: 0
Views: 100
Reputation: 863
Also it is possible to build your own custom recursive function and filter out required values from array then build custom array like lolo=>author and lolo1 =>author.... from multidimentional array like below...
function my_walk_recursive($your_multidimentional_array, $find_value, &$filtered_array) {
foreach($your_multidimentional_array as $key => $data) {
if($data[$find_value] != '') {
$filtered_array[$key] = $data['author'];
return true;
}
elseif(is_array($data) && (!empty($data))) {
$result = my_walk_recursive($data, $find_value, $filtered_array);
if($result == true) {
continue;
}
}
}
return $filtered_array;
}
$filtered_array = array();
$final_array = array();
$final_array = my_walk_recursive($test_array, 'author', $filtered_array);
var_dump($final_array);
Hope this helps....
Upvotes: 1
Reputation: 863
Try below code which will give you all authors as an array from multidimensional array without using forloops.., also if you want to retrieve other values from multidimentional array then you need to pass array key in in_array at if condition and prepare data according to your requirement...
$author_array = array();
array_walk_recursive($your_multidimentional_array, function($value, $key) {
if (in_array($key, array("author"))) {
global $author_array;
$author_array[] = $value;
}
});
print_r($author_array);
Hope this helps....
Upvotes: 1