According to the value 5 4 1 contained in array two, unset the above array one without the key 5 4 1 to find the simplest way to write it. ha
// 數(shù)組一
array(6) {
[1] => string(12) "伊凡木門"
[2] => string(12) "夢天木門"
[3] => string(15) "大自然地板"
[4] => string(12) "尚品宅配"
[5] => string(15) "德國都芳漆"
[6] => string(12) "左右沙發(fā)"
}
數(shù)組二
array(3) {
[0] => int(5)
[1] => int(4)
[2] => int(1)
}
I finally solved it with the following method. If the masters have a better way of writing, please feel free to enlighten me
function get_vip_brand_list($uid = UID)
{
// 第一個數(shù)組
$brand_list = config('sales_brand');
// 第二個數(shù)組,反轉(zhuǎn)鍵和值
$node = array_flip(get_auth_node($uid,'sales.brand'));
// 比較兩個數(shù)組的鍵名,并返回交集
$vip_node = array_intersect_key($brand_list, $node);
return $vip_node;
}
You can use the functions of the array_diff series to operate. You can decide by yourself whether to use array_diff_key or assoc for the specific business.
According to the value 5 4 1 contained in array 2, find the simplest way to write the unset of the above array 1 which does not exist and the key is not 5 4 1. Ha
means I don’t understand
<?php
$keys1 = array_keys($array1); // 獲取數(shù)組1key列表
$diffKeys = array_diff($keys1,$array2);// 結(jié)算數(shù)組1和數(shù)組2 key差集
foreach($diffKeys as $key){
unset($array1[$key]);
}
Create a new array to store the values ??you want to retain. Then loop through array two, and then use the array_push function to push the values ??to be retained in array one into the newly created array.
$arr1 = array(
1 => "伊凡木門",
2 => "夢天木門",
3 => "大自然地板",
4 => "尚品宅配",
5 => "德國都芳漆",
6 => "左右沙發(fā)"
);
$arr2 = array(5, 4, 1);
$keys = array_keys($arr1);
$remove = array_diff($keys, $arr2);
foreach ($remove as $key) {
unset($arr1[$key]);
}
var_dump($arr1);
First flip array 2, and then find the intersection. I think your solution is the correct one
foreach($arr2 as $value) {
foreach($arr1 as $key => $val) {
if($value == $key) {
unset($arr1[$key]);
}
}
}
print_r($arr1);
You can learn about the array_slice function