我有一个多维数组,我使用foreach循环循环。
然后我需要检查这些数组是否具有'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'
));
所以$ arrMulti [0] ['page_parent']会与$ arrMulti [2] ['page_parent']匹配,所以我需要使用这些创建一个新数组,如下所示:
$arrParentIDs = array( 'parent_id' => array(
1,
3
));
对于糟糕的解释感到抱歉,但您对如何做到这一点有任何想法吗?
谢谢!
答案 0 :(得分:2)
$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
答案 1 :(得分:1)
尝试这样的事情......
foreach($arrMulti as $array) {
foreach($array as $key=>$val) {
//your statement/condition
}
}