3 回答

TA貢獻(xiàn)1851條經(jīng)驗(yàn) 獲得超5個(gè)贊
按照您提供的代碼,我將假設(shè)$data1and$data2是一個(gè) laravel 集合類而不是一個(gè)數(shù)組,那么您可以在查詢 data2 之前嘗試以下代碼。
// get the id of all the brand item of the first array
$brandIdArr = $data1->pluck('brand_items')->flatten()->pluck('id')->all();
// then filter them out on your eager loading query
$data2 = Brand::where('id', $post['id_brand']);
$data2->with(['brandItems' => function($q)use($brandIdArr){ // added use
$q->where('is_inventory', 1)->whereNotIn('id',$brandIdArr)->with('unitOpname'); // added whereNotIn
}]);
$data2 = $data2->get();

TA貢獻(xiàn)1873條經(jīng)驗(yàn) 獲得超9個(gè)贊
用于in_array()在兩個(gè)數(shù)組中查找相似的值。然后找出第二個(gè)數(shù)組副本的鍵值正在使用什么array_search()。使用 foreach 循環(huán)取消設(shè)置重復(fù)項(xiàng)。
我在示例中使用了兩個(gè)已定義的顏色數(shù)組
注意兩個(gè)相似的變量purple和blue
$one = array(
"green" , "red" , "brown", "purple", "blue", "limegreen"
);
$two = array(
"yellow" , "orange" , "purple", "blue", "skyblue"
);
foreach($one as $key => $color){
if(in_array($color, $two)){
$key = array_search($color, $two);
unset($two[$key]);
}
}
現(xiàn)在使用以下方法將兩個(gè)數(shù)組合并為一個(gè)新數(shù)組array_merge(): 通過單獨(dú)保留第一個(gè)數(shù)組,在數(shù)組中定義在第二個(gè)值中重復(fù)的原始值,僅刪除第二個(gè)值
$all_colors = array_merge($one, $two);
var_dump($all_colors);
原始變量:
$one = array(
"green" , "red" , "brown", "purple", "blue", "limegreen"
);
$two = array(
"yellow" , "orange" , "purple", "blue", "skyblue"
);
注意鍵值確實(shí)不同
通過定義一個(gè)新變量$key并分配循環(huán)in_array()內(nèi)的變量,foreach()我們現(xiàn)在可以從我們的第二個(gè)數(shù)組中取消設(shè)置那些不同的鍵控變量,$two.
結(jié)果:
array(9) {
[0]=> string(5) "green"
[1]=> string(3) "red"
[2]=> string(5) "brown"
[3]=> string(6) "purple"
[4]=> string(4) "blue"
[5]=> string(9) "limegreen"
[6]=> string(6) "yellow"
[7]=> string(6) "orange"
[8]=> string(7) "skyblue"
}
現(xiàn)在沒有重復(fù)。用這個(gè)新的清理變量做你想做的事。
獎(jiǎng)金:
將這一切包裝在一個(gè)函數(shù)中。
function evalArrays($first_array, $second_array){
foreach($first_array as $k => $value){
if(in_array($value, $second_array)){
$keyvalues_to_remove = array_search($value, $second_array);
unset($second_array[$keyvalues_to_remove]);
}
}
$new_array = array_merge($first_array, $second_array);
return $new_array;
}
使用功能evalArrays($somearray, $anotherarray);

TA貢獻(xiàn)1946條經(jīng)驗(yàn) 獲得超4個(gè)贊
您可以通過散列每一行
$hashedArrayRow = md5(serialize($arrayRow));
然后將散列行存儲(chǔ)到新列中。
- 3 回答
- 0 關(guān)注
- 126 瀏覽
添加回答
舉報(bào)