0

I have faced a problem during get unique value in foreach loop.

Following is my array.

Array
(
    [20] => Array
        (
            [0] => Array
                (
                    [id] => 4
                    [category_title] => Specialist Range
                    [parent] => 20
                    [front_active] => 1
                    [category_date] => 2019-05-21 04:04:17
                )

            [1] => Array
                (
                    [id] => 4
                    [category_title] => Specialist Range
                    [parent] => 20
                    [front_active] => 1
                    [category_date] => 2019-05-21 04:04:17
                )

            [2] => Array
                (
                    [id] => 4
                    [category_title] => Specialist Range
                    [parent] => 20
                    [front_active] => 1
                    [category_date] => 2019-05-21 04:04:17
                )

            [3] => Array
                (
                    [id] => 6
                    [category_title] => Cater Foil Rolls
                    [parent] => 20
                    [front_active] => 1
                    [category_date] => 2019-05-21 04:04:24
                )

        )

    [21] => Array
        (
            [8] => Array
                (
                    [id] => 24
                    [category_title] => Specialist Range
                    [parent] => 21
                    [front_active] => 1
                    [category_date] => 2019-05-21 04:07:59
                )

            [9] => Array
                (
                    [id] => 24
                    [category_title] => Specialist Range
                    [parent] => 21
                    [front_active] => 1
                    [category_date] => 2019-05-21 04:07:59
                )



        )


)

I written following script for getting unique value in loop

$catArray = array();
foreach($catagory_list as $key=>$catagory){

        $catArray[$catagory['parent']][$key] = $catagory;
} 
ksort($catArray, SORT_NUMERIC);

foreach ($catArray as $key => $value) {

    if($key == 20 ){ 
        $catName ='local';
    }elseif ($key == 21) {
        $catName ='interstate';
    }elseif ($key == 22) {
        $catName ='wholesale';
    }elseif ($key == 23) {
        $catName ='tst';
    }

    //echo $key;

    foreach(array_unique($value) as $keys => $valuess){
        echo $valuess['category_title'];
        ?>
                <tr class="table_header">
                    <th><?php echo $catName." - ".$valuess['category_title'];?> </th>
                </tr>
    <?php

    }
}

Problem is 20 has 4 category_title but when i used array_unique Cater Foil Rolls category title of first parent array is not display.

Output only display

Specialist Range not showing (Cater Foil Rolls)

Samir Sheikh
  • 2,283
  • 2
  • 19
  • 37

2 Answers2

1

If you are trying to find unique records in this nested array, then I would key your associative array off of the value of "id". If you are only trying to find unique category_title, then I would key off the value of "category_title". In any case I would use the associate array functionality to ensure uniqueness on whatever key you choose. Then you can apply sorting, etc. on the result.

// find first unique records
$result = [];
foreach($parents as $key1=>$child1) {
    foreach($child1 as $key2=>$child2) {
        if(!isset($result[$child2['id']])) {
            $result[$child2['id']] = $child2;
        }
    }
}

// result = # => record

OR:

// find first unique categories
$result = [];
foreach($parents as $key1=>$child1) {
    foreach($child1 as $key2=>$child2) {
        if(!isset($result[$child2['category_title']])) {
            $result[$child2['category_title']] = $child2;
        }
    }
}
// result = category => record
0

I'm not convinced that array_unique works on multi-dimensional arrays.

See this question.

Mark
  • 1,852
  • 3
  • 18
  • 31