从foreach循环中获得唯一值期间遇到的问题

时间:2019-07-05 16:09:27

标签: php arrays foreach

在foreach循环中获取唯一值期间,我遇到了一个问题。

以下是我的数组。

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
                )



        )


)

我编写了以下脚本来获取循环中的唯一值

$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

    }
}

问题是20有4个category_title,但是当我使用array_unique Cater Foil Rolls 时,不会显示第一个父级数组的类别标题。

输出仅显示

专家范围未显示(Cater Foil Rolls)

2 个答案:

答案 0 :(得分:0)

如果您试图在此嵌套数组中查找唯一记录,那么我将把关联数组的键设置为“ id”的值之外。如果您只想查找唯一的category_title,那么我将取消“ category_title”的值。无论如何,我都会使用关联数组功能来确保所选键上的唯一性。然后您可以对结果进行排序等。

// 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

答案 1 :(得分:0)

我不相信array_unique可用于多维数组。

See this question.