foreach循环仅在循环中输出第一个值
foreach loop output first value only in loop
为什么 foreach 循环只打印数组中的第一个值?
$Jdata_cate = '[{"category_id":"103","name":"Martin","parent_id":0},{"category_id":"10","name":"Juan","parent_id":0},{"category_id":"9","name":"Kasi","parent_id":0}]';
$J_Min = strtolower($Jdata_cate);
$J_MinDecoded = json_decode($J_Min, true);
$Ddata_cate = '[{"category_id":"55","name":"Abc","parent_id":0},{"category_id":"41","name":"Pedro","parent_id":0},{"category_id":"40","name":"Kasi","parent_id":0}]';
$D_Min = strtolower($Ddata_cate);
$D_MinDecoded = json_decode($D_Min, true);
$both_arrays = array_merge((array)$J_MinDecoded, (array)$D_MinDecoded);
$Delete_repeated = array_unique($both_arrays);
foreach($Delete_repeated as $y=>$y_value){
echo $y_value['name'] . '<br>';
}
问题在于您调用 array_unique($both_arrays)
默认行为是将项目作为字符串进行比较,但它们是数组,因此失败。
解决方案是添加SORT_REGULAR
标志作为第二个参数。
$Delete_repeated = array_unique($both_arrays, SORT_REGULAR);
试试这个解决方案:
function array_unique_multidimensional($array, $key)
{
$temp_array = array();
$i = 0;
$key_array = array();
foreach ($array as $val) {
if (!in_array($val[$key], $key_array)) {
$key_array[$i] = $val[$key];
$temp_array[$i] = $val;
}
$i++;
}
return $temp_array;
}
$Jdata_cate = '[{"category_id":"103","name":"Martin","parent_id":0},{"category_id":"10","name":"Juan","parent_id":0},{"category_id":"9","name":"Kasi","parent_id":0}]';
$J_Min = strtolower($Jdata_cate);
$J_MinDecoded = json_decode($J_Min, true);
$Ddata_cate = '[{"category_id":"55","name":"Abc","parent_id":0},{"category_id":"41","name":"Pedro","parent_id":0},{"category_id":"40","name":"Kasi","parent_id":0}]';
$D_Min = strtolower($Ddata_cate);
$D_MinDecoded = json_decode($D_Min, true);
$both_arrays = array_merge((array)$J_MinDecoded, (array)$D_MinDecoded);
$Delete_repeated = array_unique_multidimensional($both_arrays, 'name');
foreach ($Delete_repeated as $y => $y_value) {
echo $y_value['name'] . '<br>';
}
在这里而不是 array_unique
我使用我定义的函数
为什么 foreach 循环只打印数组中的第一个值?
$Jdata_cate = '[{"category_id":"103","name":"Martin","parent_id":0},{"category_id":"10","name":"Juan","parent_id":0},{"category_id":"9","name":"Kasi","parent_id":0}]';
$J_Min = strtolower($Jdata_cate);
$J_MinDecoded = json_decode($J_Min, true);
$Ddata_cate = '[{"category_id":"55","name":"Abc","parent_id":0},{"category_id":"41","name":"Pedro","parent_id":0},{"category_id":"40","name":"Kasi","parent_id":0}]';
$D_Min = strtolower($Ddata_cate);
$D_MinDecoded = json_decode($D_Min, true);
$both_arrays = array_merge((array)$J_MinDecoded, (array)$D_MinDecoded);
$Delete_repeated = array_unique($both_arrays);
foreach($Delete_repeated as $y=>$y_value){
echo $y_value['name'] . '<br>';
}
问题在于您调用 array_unique($both_arrays)
默认行为是将项目作为字符串进行比较,但它们是数组,因此失败。
解决方案是添加SORT_REGULAR
标志作为第二个参数。
$Delete_repeated = array_unique($both_arrays, SORT_REGULAR);
试试这个解决方案:
function array_unique_multidimensional($array, $key)
{
$temp_array = array();
$i = 0;
$key_array = array();
foreach ($array as $val) {
if (!in_array($val[$key], $key_array)) {
$key_array[$i] = $val[$key];
$temp_array[$i] = $val;
}
$i++;
}
return $temp_array;
}
$Jdata_cate = '[{"category_id":"103","name":"Martin","parent_id":0},{"category_id":"10","name":"Juan","parent_id":0},{"category_id":"9","name":"Kasi","parent_id":0}]';
$J_Min = strtolower($Jdata_cate);
$J_MinDecoded = json_decode($J_Min, true);
$Ddata_cate = '[{"category_id":"55","name":"Abc","parent_id":0},{"category_id":"41","name":"Pedro","parent_id":0},{"category_id":"40","name":"Kasi","parent_id":0}]';
$D_Min = strtolower($Ddata_cate);
$D_MinDecoded = json_decode($D_Min, true);
$both_arrays = array_merge((array)$J_MinDecoded, (array)$D_MinDecoded);
$Delete_repeated = array_unique_multidimensional($both_arrays, 'name');
foreach ($Delete_repeated as $y => $y_value) {
echo $y_value['name'] . '<br>';
}
在这里而不是 array_unique
我使用我定义的函数