簡體   English   中英

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

[英]How to pull a specific key data from multidimensional array

我正在將XML文件轉換為關聯數組以提取數據,問題是我必須根據數組編號進行10次循環才能獲取數據。

有沒有更好的方法來獲取特定的列數據而不創建很多循環? 因為我想將它們分配給變量。

我試圖從中獲取數據的數組

Array
(
    [catalog] => Array
        (
            [comp] => Array
                (
                    [0] => Array
                        (
                            [look] => Array
                                (
                                    [shp] => Array
                                        (
                                            [wok] => Array
                                                (
                                                    [group] => Array
                                                        (
                                                            [customer] => Array
                                                                (
                                                                    [author] => jack
                                                                    [title] => Midnight Rain1
                                                                    [genre] => Fantasy
                                                                    [price] => 5.95
                                                                    [publish_date] => 2000-12-16
                                                                    [description] => A former architect battles corporate zombies.
                                                                )

                                                            [customer2] => Array
                                                                (
                                                                    [author] => lemass
                                                                    [title] => Midnight Rain1
                                                                    [genre] => Fantasy
                                                                    [price] => 5.95
                                                                    [publish_date] => 2000-12-16
                                                                    [description] => A former architect battles corporate zombies.
                                                                )

                                                        )

                                                )

                                        )

                                )

                        )

                    [1] => Array
                        (
                            [look] => Array
                                (
                                    [shp] => Array
                                        (
                                            [wok] => Array
                                                (
                                                    [group] => Array
                                                        (
                                                            [customer] => Array
                                                                (
                                                                    [author] => jon
                                                                    [title] => Midnight Rain1
                                                                    [genre] => Fantasy
                                                                    [price] => 5.95
                                                                    [publish_date] => 2000-12-16
                                                                    [description] => A former architect battles corporate zombies.
                                                                )

                                                            [customer2] => Array
                                                                (
                                                                    [author] => kirito
                                                                    [title] => Midnight Rain1
                                                                    [genre] => Fantasy
                                                                    [price] => 5.95
                                                                    [publish_date] => 2000-12-16
                                                                    [description] => A former architect battles corporate zombies.
                                                                )

                                                        )

                                                )

                                        )

                                )

                        )

                )

        )

)

我正在嘗試獲取這樣的數據!

我有2個數組“ customer”和“ customer1”

我想要這樣的數據

客戶=>作者

輸出

jack
jon

因為它們在客戶群中

它有可能做到這一點?

假設您的數組存儲在$arr您將訪問comp索引,然后對其進行循環,因為它們是數字索引。 然后,您有一個陣列可以減少更多內容。 數組結構似乎使這一切膨脹了,但可以正常工作

$arr; //Set this to your converted xml
$comps = $arr['catalog']['comp'];

foreach($comps as $comp){
    echo $comp['look']['shp']['wok']['group']['customer']['author'];
}
<?php
$aVar = Array
(
    'catalog' => Array
        (
            'comp' => Array
                (
                    0 => Array
                        (
                            'look' => Array
                                (
                                    'shp' => Array
                                        (
                                            'wok' => Array
                                                (
                                                    'group' => Array
                                                        (
                                                            'customer' => Array
                                                                (
                                                                    'author' => 'jack',
                                                                    'title' => 'Midnight Rain1',
                                                                    'genre' => 'Fantasy',
                                                                    'price' => 5.95,
                                                                    'publish_date' => '2000-12-16',
                                                                    'description' => 'A former architect battles corporate zombies.'
                                                                ),
                                                            'customer2' => Array
                                                                (
                                                                    'author' => 'lemass',
                                                                    'title' => 'Midnight Rain1',
                                                                    'genre' => 'Fantasy',
                                                                    'price' => 5.95,
                                                                    'publish_date' => '2000-12-16',
                                                                    'description' => 'A former architect battles corporate zombies.'
                                                                )
                                                        )
                                                )
                                        )
                                )
                        ),
                    1 => Array
                        (
                            'look' => Array
                                (
                                    'shp' => Array
                                        (
                                            'wok' => Array
                                                (
                                                    'group' => Array
                                                        (
                                                            'customer' => Array
                                                                (
                                                                    'author' => 'jon',
                                                                    'title' => 'Midnight Rain1',
                                                                    'genre' => 'Fantasy',
                                                                    'price' => 5.95,
                                                                    'publish_date' => '2000-12-16',
                                                                    'description' => 'A former architect battles corporate zombies.'
                                                                ),
                                                            'customer2' => Array
                                                                (
                                                                    'author' => 'kirito',
                                                                    'title' => 'Midnight Rain1',
                                                                    'genre' => 'Fantasy',
                                                                    'price' => 5.95,
                                                                    'publish_date' => '2000-12-16',
                                                                    'description' => 'A former architect battles corporate zombies.'
                                                                )
                                                        )
                                                )
                                        )
                                )
                        )
                )
        )
);


function findKey($array, $keySearch) {
    foreach ($array as $key => $item) {
        if ($key === $keySearch) {
            echo $item . '<br>';
            //return true; // if just the first is wanted
        } else if (is_array($item) && findKey($item, $keySearch)) {
            return true;
        }
    }
    return false;
}

findKey($aVar, 'author');

打印輸出:

插口

lemass

喬恩

基里托

檢查多維數組中是否存在特定的數組鍵-PHP

暫無
暫無

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

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