0

Why foreach loop printing only the first value from the 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($both_arrays);

    foreach($Delete_repeated as $y=>$y_value){
            echo $y_value['name'] . '<br>';
    } 
Premlatha
  • 1,676
  • 2
  • 20
  • 40
Meto ballaes
  • 103
  • 8
  • 1
    You've not provided enough information for us to go on. Can you share the arrays? – Jay Blanchard Apr 16 '19 at 17:17
  • Yes of course @JayBlanchard, let me a moment to modify the question – Meto ballaes Apr 16 '19 at 17:18
  • Make sure you add a `print_r($Delete_repeated)` that may well tell you what is wrong with the previous statements – RiggsFolly Apr 16 '19 at 17:27
  • Foreach loop will iterate basis on your total index present inside an array.If the array consist of single index then it will print single time. – Vitthal Apr 16 '19 at 17:28
  • @RiggsFolly Now i have added the arrays. If i add `print_r($Delete_repeated);` also prints the first value of the array. I supose that the problem it is, this part of the code `$both_arrays = array_merge((array)$J_MinDecoded, (array)$D_MinDecoded);` The problem it is that i need to mix the two arrays to know which data it its repeated. How i can fix it? – Meto ballaes Apr 16 '19 at 17:33
  • @Metoballaes ok the major question is: what is the unique array, I mean is the all elements of the array must be the same or only `name` or `category_id`? are `{"category_id":"9","name":"Kasi","parent_id":0}` and `{"category_id":"40","name":"Kasi","parent_id":0}` supposed to be considered as the same? – Zeusarm Apr 16 '19 at 18:13
  • Only the name @Zeusarm, How can i do that? – Meto ballaes Apr 16 '19 at 18:43

2 Answers2

0

The problem is where you call array_unique($both_arrays) the default behavior is to compare items as strings, but they are arrays so it fails.

The solution is to add the SORT_REGULAR flag as the second parameter.

$Delete_repeated = array_unique($both_arrays, SORT_REGULAR);
Arleigh Hix
  • 9,990
  • 1
  • 14
  • 31
  • Thanks for your help, now the code it working. As it is working, i noticed that the word Kasi it is not deleted because the category_id are differents. How can i deleted the word repeated Kasi in this case, if the category_id it is different. – Meto ballaes Apr 16 '19 at 18:14
0

try this solution:

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>';
}

here instead of array_unique I'm using my defined function

Zeusarm
  • 1,038
  • 6
  • 14