drlq92444 2014-11-27 04: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 14: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.

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

报告相同问题?

悬赏问题

  • ¥15 关于#java#的问题:找一份能快速看完mooc视频的代码
  • ¥15 这种微信登录授权 谁可以做啊
  • ¥15 请问我该如何添加自己的数据去运行蚁群算法代码
  • ¥20 用HslCommunication 连接欧姆龙 plc有时会连接失败。报异常为“未知错误”
  • ¥15 网络设备配置与管理这个该怎么弄
  • ¥20 机器学习能否像多层线性模型一样处理嵌套数据
  • ¥20 西门子S7-Graph,S7-300,梯形图
  • ¥50 用易语言http 访问不了网页
  • ¥50 safari浏览器fetch提交数据后数据丢失问题
  • ¥15 matlab不知道怎么改,求解答!!