我正在将XML文件转换为关联数组以提取数据,问题是我必须根据数组数进行10次循环才能获得数据。
是否有更好的方法来获得特定的列数据而不创建许多循环?因为我想把它们赋值给变量
数组我试图从
获取数据 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.
)
)
)
)
)
)
)
)
我删除了所有其他数据以使其更易于阅读,但数组中还有许多其他值。总之,我怎样才能得到author的值呢?
echo $array['author'];
假设我有许多作者数据,而不是上面的例子
请帮助!。
编辑 .....................
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.
)
)
)
)
)
)
)
)
)
正如你在上面看到的,我只想得到父键为tata而不是tata2的值
所以我可以把它们分别插入数据库
试试下面的代码,它会给你所有的作者从多维数组作为一个数组,而不使用for循环。此外,如果你想从多维数组中检索其他值,那么你需要在in_array条件下传递数组键,并根据你的要求准备数据…
$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);
希望对您有所帮助....
也可以构建自己的自定义递归函数并从数组中过滤出所需的值,然后构建自定义数组,如lolo1 =>author和lolo1 =>author....
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);
希望对您有所帮助....