簡體   English   中英

在PHP中按鍵過濾多維數組

[英]Filter a multidimensional array by key in PHP

我有一個關於在PHP中過濾多維數組的問題。 我想要實現的是從現有數組中提取特定的數組元素(mediacontent的值)。 這是我到目前為止所得到的:

現有的多維數組:

$multidimensional_array = array(
    'entry' => array(
        0 => array(
            'width' => array(
                '$t' => '1536'
            ),
            'height' => array(
                '$t' => '2048'
            ),
            'id' => array(
                '$t' => '878974'
            ),
            'mediagroup' => array(
                'mediacontent' => array(
                    'url' => 'http://website/urltotheobject.png',
                    'width' => 384,
                    'medium' => 'image',
                    'type' => 'image/jpeg',
                    'height' => 512
                )
            )
        ),
        1 => array(
            'width' => array(
                    '$t' => '5486'
            ),
            'height' => array(
                    '$t' => '1144'
            ),
            'id' => array(
                '$t' => '485435'
            ),
            'mediagroup' => array(
                'mediacontent' => array(
                    'url' => 'http://website/urltotheobject.png',
                    'width' => 512,
                    'medium' => 'image',
                    'type' => 'image/jpeg',
                    'height' => 384
                )
            )   
        )
    )
);

一個(不正常工作)函數來過濾數組

function filterResponseByKey($keyToFilter,$array){

    foreach ($array as $key => $value) 
    {
        if($key == $keyToFilter)
        {
            return $value;
        }
        elseif(is_array($value))
        {
            $result = filterResponseByKey($keyToFilter, $value);

            if($result != false)
            {
                return $result;             
            }
        }
    }
    return false;
}

我對PHP很新,希望你們能指出我正確的方向並告訴我這里做錯了什么。

我對以下(替代)網站進行了研究,但我找不到符合我需求的答案。

理解遞歸PHP filter_array

對於您的具體情況,我建議您擁有一個對象數組,因為元素0和1是相似的,並且將mediacontent作為內部變量。

如果你真的想要這樣做,你只能通過使用地圖獲得一系列的mediacontent

 array_map($multidimensional_array['entry'],function($obj){
     return $obj['mediagroup']['mediacontent'];
 });

如果你想要的是更動態和遞歸的東西

 function recursive_filter_by_key($keyname,$list){
    $result=[];
    foreach($list as $key=>$obj){
         if($key==$keyname) 
            array_push($result,$obj);

         else if(gettype($obj)=='array')//not necesary to check if its equal to the $key as this wouldn't run if it was
                 $result = array_merge($result,recursive_filter_by_key($obj));
    }
    return $result;

 }

只要它們是您要搜索的鍵的值,此函數就可以在其結果數組中返回完整數組

我希望你發表評論,因為我認為我不太了解你的問題。

如果你沒有越過它,網站上還有另一個類似的帖子如何在PHP數組中遞歸運行array_filter?

最實用的解決方案是使用迭代器

$iter = new RecursiveIteratorIterator(
                new RecursiveArrayIterator($multidimensional_array),
                RecursiveIteratorIterator::SELF_FIRST
);
foreach($iter as $key => $value) {
  if(ctype_alpha($key) && $key == 'mediacontent') {
    echo "Media Content: ".print_r($value, true)."\n";
  }
}

這將輸出:

Media Content: Array
(
    [url] => http://website/urltotheobject.png
    [width] => 384
    [medium] => image
    [type] => image/jpeg
    [height] => 512
)

Media Content: Array
(
    [url] => http://website/urltotheobject.png
    [width] => 512
    [medium] => image
    [type] => image/jpeg
    [height] => 384
)

迭代器是PHP的一個被低估的功能,雖然它們有時會有一個棘手的語法,在這種情況下,你對節點而不是葉子感興趣,需要額外的模式參數。 然而,遞歸迭代器確實簡化了許多遞歸操作。

你可以嘗試這種方式也只是為了mediacontent ....

$multidimensional_array = array(
        'entry' => array(
            0 => array(
                'width' => array(
                    '$t' => '1536'
                ),
                'height' => array(
                    '$t' => '2048'
                ),
                'id' => array(
                    '$t' => '878974'
                ),
                'mediagroup' => array(
                    'mediacontent' => array(
                        'url' => 'http://website/urltotheobject.png',
                        'width' => 384,
                        'medium' => 'image',
                        'type' => 'image/jpeg',
                        'height' => 512
                    )
                )
            ),
            1 => array(
                'width' => array(
                    '$t' => '5486'
                ),
                'height' => array(
                    '$t' => '1144'
                ),
                'id' => array(
                    '$t' => '485435'
                ),
                'mediagroup' => array(
                    'mediacontent' => array(
                        'url' => 'http://website/urltotheobject.png',
                        'width' => 512,
                        'medium' => 'image',
                        'type' => 'image/jpeg',
                        'height' => 384
                    )
                )
            )
        )
    );

    function getMediaContent($item){
        return $item['mediagroup']['mediacontent'];
    }
    $mediacontent = array_map('getMediaContent', $multidimensional_array['entry']);
    print '<pre>';
    print_r($mediacontent);
function filterResponseByKey($keyToFilter,$array){

    foreach ($array as $key => $value) 
    {
        if($key === $keyToFilter)
        {
            return $value;
        }
        elseif(is_array($value))
        {
            $result = filterResponseByKey($keyToFilter, $value);

            if($result != false)
            {
                return $result;             
            }
        }
    }
    return false;
}

工作中!

編輯:解釋:

使用“==”檢查查找是否相等而不管類型如何,所以當它遇到一個整數時,它會將兩個值都轉換為interger int('anystring')得到0,所以鍵0 = string'mediacontent'

暫無
暫無

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

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