[英]compare two multidimensional arrays
我有這個數組:
$default['products']['categories']['list'] = 0;
$default['products']['categories']['search'] = 0;
$default['products']['categories']['delete'] = 0;
$default['products']['categories']['create'] = 0;
$default['products']['categories']['edit'] = 0;
$default['products']['product']['list'] = 0;
$default['products']['product']['search'] = 0;
$default['products']['product']['delete'] = 0;
$default['products']['product']['create'] = 0;
$default['products']['product']['edit'] = 0;
$default['products']['product']['info'] = 0;
$default['products']['product']['get_barcode_img'] = 0;
$default['products']['product']['update_amount'] = 0;
$default['api']['version'] = 0;
$default['user']['create'] = 0;
$default['user']['delete'] = 0;
$default['user']['resetpassword'] = 0;
現在我想將它與另一個數組進行比較以檢查第二個數組中是否缺少某些內容。
$2nd['products']['categories']['list'] = 0;
$2nd['products']['categories']['search'] = 0;
$2nd['products']['categories']['delete'] = 0;
$2nd['products']['categories']['create'] = 0;
$2nd['products']['categories']['edit'] = 0;
$2nd['products']['product']['list'] = 0;
$2nd['products']['product']['search'] = 0;
$2nd['products']['product']['delete'] = 0;
$2nd['products']['product']['create'] = 0;
$2nd['products']['product']['edit'] = 0;
$2nd['products']['product']['info'] = 0;
$2nd['products']['product']['get_barcode_img'] = 0;
$2nd['products']['product']['update_amount'] = 0;
$2nd['api']['version'] = 0;
所以我想檢測“用戶”的東西是否丟失,然后將丟失的東西插入到第二個數組中
下面的示例將檢查$userArray
包含$default
頂層的所有字段。 如果缺少什么,它會添加它。 通過做這種方式,你不會覆蓋任何字段(在頂層)用戶已指定。
<?php
//Your array of default permissions
$default = array(
"products" => array(
"categories" => array(
"list" => 0,
"search" => 0,
"delete" => 0,
"create" => 0,
"edit" => 0,
),
"product" => array(
"list" => 0,
"search" => 0,
"delete" => 0,
"create" => 0,
"edit" => 0,
"info" => 0,
"get_barcode_img" => 0,
"update_amount" => 0,
)
),
"api" => array(
"version" => 0,
),
"user" => array(
"create" => 0,
"delete" => 0,
"resetpassword" => 0,
),
);
//The user specified array of permissions, missing everything at "user" section
$userArray = array(
"products" => array(
"categories" => array(
"list" => 0,
"search" => 0,
"delete" => 0,
"create" => 0,
"edit" => 0,
),
"product" => array(
"list" => 0,
"search" => 0,
"delete" => 0,
"create" => 0,
"edit" => 0,
"info" => 0,
"get_barcode_img" => 0,
"update_amount" => 0,
)
),
"api" => array(
"version" => 0,
),
);
foreach( $default as $key=>$group ){
if( !isset( $userArray[$key] )) {
$userArray[ $key ] = $group;
}
}
echo "<pre>" . print_r( $userArray, 1 ) . "</pre>";
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.