如何遍历数组而不重复?

时间:2019-05-15 05:01:32

标签: php arrays

我正在尝试从API获取数据,并且收到了此消息

[
    {
        "service": 1,
        "name": "Followers",
        "type": "Default",
        "category": "First Category",
        "rate": "0.90",
        "min": "50",
        "max": "10000"
    },
    {
        "service": 2,
        "name": "Comments",
        "type": "Custom Comments",
        "category": "Second Category",
        "rate": "8",
        "min": "10",
        "max": "1500"
    }
]

我想获得每个服务的类别,而不必重复相同的类别两次。

已编辑* 我有此代码

            $servers = $this->Setting->Loop('api','WHERE is_active = 1');
            foreach($servers->result() as $server){
                foreach($this->Api_Connect->services($server->api_url, $server->api_key) as $item) {
                    echo '<option data-server='.$server->id.' data-percent='.$server->addon_percent.' data-price='.$item['rate'].' data-min='.$item['min'].' data-max='.$item['max'].' value="'.$item['service'].'">- '.$item['name'].'</option>';
                }
            }

使用api网址和密钥连接到每个服务器,并随服务一起返回。

4 个答案:

答案 0 :(得分:3)

我们可以设置一个新数组,并在新数组中推送类别,然后检查新值是否不存在:

$data = '[
    {
        "service": 1,
        "name": "Followers",
        "type": "Default",
        "category": "First Category",
        "rate": "0.90",
        "min": "50",
        "max": "10000"
    },
    {
        "service": 2,
        "name": "Comments",
        "type": "Custom Comments",
        "category": "Second Category",
        "rate": "8",
        "min": "10",
        "max": "1500"
    },
    {
        "service": 2,
        "name": "Comments",
        "type": "Custom Comments",
        "category": "Second Category",
        "rate": "8",
        "min": "10",
        "max": "1500"
    }
]';

$data = json_decode($data, true);

$category = array();
foreach ($data as $value) {
    if (!array_search($value["category"], $category)) {
        array_push($category, $value["category"]);
    }
}

var_dump($category);

输出

array(2) {
  [0]=>
  string(14) "First Category"
  [1]=>
  string(15) "Second Category"
}

编辑:

根据Andreas的建议,我们还可以在循环中使用if(in_array()),方法是:

 $category[$value["category"]] = $value["category"]; 

效率更高。

答案 1 :(得分:2)

最快的方法是使用array_column
Array_column将隔离数组的一列。
array_column的第三个参数将设置密钥名称。
这就是我们想要的。
因为您不希望重复,所以我们将某些内容设置为值,将“类别”设置为键。

这意味着它将循环一次并覆盖重复的密钥。
此处发布的其他方法将需要检查该项目是否已经添加到列表中,或者使用array_unique,它需要将每个项目与其他每个项目进行比较,当您给它一个slow function时,大阵列。

$data = '[
    {
        "service": 1,
        "name": "Followers",
        "type": "Default",
        "category": "First Category",
        "rate": "0.90",
        "min": "50",
        "max": "10000"
    },
    {
        "service": 2,
        "name": "Comments",
        "type": "Custom Comments",
        "category": "Second Category",
        "rate": "8",
        "min": "10",
        "max": "1500"
    },
    {
        "service": 2,
        "name": "Comments",
        "type": "Custom Comments",
        "category": "Second Category",
        "rate": "8",
        "min": "10",
        "max": "1500"
    }
]';

$data = json_decode($data, true);

$category = array_column($data, "rate", "category");

var_dump($category);

此输出:

// Note it's only the keys we want, the values are not interesting
array(2) {
  ["First Category"]=>
  string(4) "0.90"
  ["Second Category"]=>
  string(1) "8"
}

答案 2 :(得分:1)

$array = [];     
foreach($object as $key => $value) { 
        // object is data you receive from API 
        array_push($array,$value->category);
}
// to get unique values 
$array = array_unique($array); 

I hope this code solve your problem

答案 3 :(得分:0)

$jsonResponse='[
    {
        "service": 1,
        "name": "Followers",
        "type": "Default",
        "category": "First Category",
        "rate": "0.90",
        "min": "50",
        "max": "10000"
    },
    {
        "service": 2,
        "name": "Comments",
        "type": "Custom Comments",
        "category": "Second Category",
        "rate": "8",
        "min": "10",
        "max": "1500"
    }
]';

$listData=json_decode(jsonResponse,true);


foreach($listData as $valRes){
echo $valRes['category'];

}

you can get category