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 关于#matlab#的问题:在模糊控制器中选出线路信息,在simulink中根据线路信息生成速度时间目标曲线(初速度为20m/s,15秒后减为0的速度时间图像)我想问线路信息是什么
  • ¥15 banner广告展示设置多少时间不怎么会消耗用户价值
  • ¥16 mybatis的代理对象无法通过@Autowired装填
  • ¥15 可见光定位matlab仿真
  • ¥15 arduino 四自由度机械臂
  • ¥15 wordpress 产品图片 GIF 没法显示
  • ¥15 求三国群英传pl国战时间的修改方法
  • ¥15 matlab代码代写,需写出详细代码,代价私
  • ¥15 ROS系统搭建请教(跨境电商用途)
  • ¥15 AIC3204的示例代码有吗,想用AIC3204测量血氧,找不到相关的代码。