簡體   English   中英

如何從多維數組中提取特定數據

[英]How to pull specific data from multidimensional array

我正在將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.
                                                        )

                                                )

                                        )

                                )

                        )

                )

        )

)

我刪除了所有其他數據以使其更易於讀取,但是數組中還有許多其他值。 無論如何,我如何獲得作者的價值。

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的值

所以我可以將它們分別插入數據庫

嘗試下面的代碼,它將為您提供所有作者作為多維數組的數組,而無需使用forloops ..,同樣,如果您想從多維數組中檢索其他值,則需要在if條件下在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);

希望這可以幫助....

也可以構建自己的自定義遞歸函數並從數組中過濾出所需的值,然后從多維數組中構建自定義數組,例如lolo => 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);

希望這可以幫助....

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM