drlq92444 2014-11-26 20:41
浏览 75
已采纳

PHP如何从另一个多维数组中检索多维数组,以便记住父键

I am trying to find the value of a key in a multi dimensional array while matching the parent key too.

For example:

Array 1 - Array of keys I am trying to find the value of.

$array1 = array(
    'title',
    'metadata' => array('size', 'mime'),
    'dates' => array('published' => 
                    array('firstedition' => '12/22/22', 
                        'secondedition' => '22/22/22', 
                        'thirdedition'),
    'description',
    'mime'
 );

Array 2: The array I'm searching for the keys in array1

$array2 = array(
    'title' => 'hello',
    'metadata' => array('size' => '22', 'mime' => 'text/html'),
    'dates' => array('published' => 
                    array('firstedition' => '12/22/22', 
                        'secondedition' => '22/22/22', 
                        'thirdedition' => '22/22/14'),
    'type' => 'book',
    'description' => 'This is a description',
    'mime' => 'something/pdf',
    'firstedition' => '99/99/99'
 );

All the values in Array 1 are the keys I want to find in Array 2. If the value in Array 1 is an array, I want to find the value in Array 2 respective of the values parents.

For example:

Array 1: Metadata -> Size

Search on Array 2 returns: 22


Array 1: Metadata -> Mime

Search on Array 2 returns: text/html


Array 1: Mime

Search on Array 2 returns: something/pdft


Array 1: dates -> published -> firstedition

Array 2 returns:

12/22/22

Array 1: firstedition

Array 2 returns: 99/99/99

Notice the difference of outputs between "dates -> published -> firstedition" and just "firstedition". So I want to search all the values in array 1 as keys to find within array 2 and display them like:

key -> possiblesubkey -> possiblesubkey : value

If possible to return an array with the path of the key and value for example:

    array('dates\published\firstedition', '12/22/22');
    array('firstedition', '99/99/99');

Also, Array1 is part of a larger array, with different file types, array1 is under the key "books", and within array2 is the key "type", so depending on what type array2 is the the values from the keys I want in array2 may differ. For example in array1 there might also be "songs" of which I'd like a different set of values for such as "bitrate" but I don't want to look for "firstedition" because it doesn't exist.

I've posted this example as I've already managed to identify what type array2 is and find that corresponding key which outputs what I have posted as array1.

Any help would be greatly appreciated can't seem to get my hand around this.

展开全部

  • 写回答

1条回答 默认 最新

  • dongyixiu3110 2015-03-10 06:09
    关注

    I managed to accomplish this by squashing the array to paths with this function:

    public static function convertToPaths($myArray)
    {
        $ritit = new RecursiveIteratorIterator(new RecursiveArrayIterator($myArray));
        $result = array();
        foreach ($ritit as $leafValue) {
            $keys = array();
            foreach (range(0, $ritit->getDepth()) as $depth) {
                $keys[] = $ritit->getSubIterator($depth)->key();
            }
            $result[ join('.', $keys) ] = $leafValue;
        }
        return $result;
    }
    

    Which returns the array and values as paths, such as "dates.published.firstedition" - I was then able to access the information and check whether it exists by using the in array function.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
编辑
预览

报告相同问题?

手机看
程序员都在用的中文IT技术交流社区

程序员都在用的中文IT技术交流社区

专业的中文 IT 技术社区,与千万技术人共成长

专业的中文 IT 技术社区,与千万技术人共成长

关注【CSDN】视频号,行业资讯、技术分享精彩不断,直播好礼送不停!

关注【CSDN】视频号,行业资讯、技术分享精彩不断,直播好礼送不停!

客服 返回
顶部