简体   繁体   English

多维数组-搜索相同的值

[英]Multidimensional array - search for same value

I have a multidimensional array which I am looping through using a foreach loop. 我有一个多维数组,正在使用foreach循环遍历。

I have then need to check whether any of those arrays have the key of 'parent_page' and the same value of any other arrays, such as: 然后,我需要检查这些数组中的任何一个是否具有“ parent_page”键和任何其他数组的相同值,例如:

$arrMulti = array(array(
    'page_id' => 1,
    'page_parent' => 28,
    'page_title' => 'Testing'
), array(
    'page_id' => 2,
    'page_parent' => 30,
    'page_title' => 'A seperate page'
), array(
    'page_id' => 3,
    'page_parent' => 28,
    'page_title' => 'Testing Sub Page'
));

So $arrMulti[0]['page_parent'] would match with $arrMulti[2]['page_parent'], so I need to then create a new array using those, something like this: 因此$ arrMulti [0] ['page_parent']将与$ arrMulti [2] ['page_parent']匹配,因此我需要使用这些创建一个新数组,如下所示:

$arrParentIDs = array( 'parent_id' => array(
    1,
    3
));

Sorry for the poor explanation, but do you have any idea on how to do this? 不好意思的解释,很抱歉,但是您对此有任何想法吗?

Thanks! 谢谢!

$parentIds = array();
foreach($arrMulti as $temp):

    if(isset($temp['page_parent'] && !in_array($temp['page_parent'], $parentIds)){
        $parentIds[] = $temp['page_parent'];
    }

endforeach;

var_dump($parentIds);//to show the contents

Try something like this.. 尝试这样的事情。

foreach($arrMulti as $array) {
    foreach($array as $key=>$val) {
        //your statement/condition
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM