Php关联数组函数,用于在数组中查找重复的id值和非重复值

Php关联数组函数,用于在数组中查找重复的id值和非重复值,php,arrays,Php,Arrays,如果3页有2篇文章,那么获取这些文章的函数将返回[6]的数组,如下所示: Array ( [0] => Array ( [post_text] => Test 3:30 [post_id] => 46 [concrete_id] => 5c3f55b5ebb084c078c92d13 [post_data_start] => 2019-02-06

如果3页有2篇文章,那么获取这些文章的函数将返回[6]的数组,如下所示:

Array
(
    [0] => Array
        (
            [post_text] => Test 3:30
            [post_id] => 46
            [concrete_id] => 5c3f55b5ebb084c078c92d13
            [post_data_start] => 2019-02-06 15:31:00
            [post_type] => II
            [page_id] => 7
        )

    [1] => Array
        (
            [post_text] => Test 3:30
            [post_id] => 46
            [concrete_id] => fus98f67a9
            [post_data_start] => 2019-02-06 15:31:00
            [post_type] => II
        )

    [2] => Array
        (
            [post_text] => Test 3:30
            [post_id] => 46
            [concrete_id] => 5c3f55b5ebb084c078c94c0
            [post_data_start] => 2019-02-06 15:31:00
            [post_type] => II
        )

    [3] => Array
        (
            [post_text] => test
            [post_id] => 43
            [concrete_id] => 5c3f55b5ebb084c078c92d13
            [post_data_start] => 2019-02-06 17:31:00
            [post_type] => II
        )

    [4] => Array
        (
            [post_text] => test
            [post_id] => 43
            [concrete_id] => fus98f67a9
            [post_data_start] => 2019-02-06 17:31:00
            [post_type] => II
        )

    [5] => Array
        (
            [post_text] => test
            [post_id] => 43
            [concrete_id] => 5c581a33ebb084c078ceb6cc
            [post_data_start] => 2019-02-06 17:31:00
            [post_type] => II
        )

)
在cURL调用中,我必须将
concrete\u id
作为数组传递(因为每次调用需要花费10个字节),在这种情况下,我必须进行6次cURL调用,但我只能进行2次调用,将
concrete\u id
作为数组传递。我使用的API就是这样工作的(但问题与API无关)

我想得到一个具有唯一id的数组,但数组的值不同(或者只需
concrete\u id
即可):

这是我能想到的最好办法,但我的大脑很痛:

$tmpposts=$posts;
            foreach($posts as $k=>$post){
                foreach($tmpposts as $k2=>$post2){
                    if($post['post_id']==$post2['post_id']){
                        $post2['concrete_id']=array($post2['concrete_id']);

                        array_push($post2['concrete_id'],$post['concrete_id']);
                        $array[]=$post2;
                        array_splice($array,$k);
                    }
                }
            }

您可以使用simple
for loop
减少数组。考虑如下:

$arr = []; // example just with "id" and "concrete_id" but you can add all other data
$arr[] = array("id" => 46, "concrete_id" => "5c3f55b5ebb084c078c92d13");
$arr[] = array("id" => 46, "concrete_id" => "fus98f67a9");
$arr[] = array("id" => 46, "concrete_id" => "5c3f55b5ebb084c078c94c0");
$arr[] = array("id" => 43, "concrete_id" => "5c3f55b5ebb084c078c92d13");
$arr[] = array("id" => 43, "concrete_id" => "fus98f67a9");

foreach($arr as $e) {
    if (!isset($ans[$e["id"]])) {
        $ans[$e["id"]] = $e; // element doesn't exist set first time
        $ans[$e["id"]]["concrete_id"] = [$e["concrete_id"]]; // make the concrete_id as array with 1 element
    } else { // the id already set just append the concrete_id
        $ans[$e["id"]]["concrete_id"][] = $e["concrete_id"];
    }
}

如果不希望
id
作为结果中的键,则可以使用:
$ans=array\u values($ans)

Mah-man!这很好用。我数组中的id称为post_id,是的,我使用了数组_值。
$arr = []; // example just with "id" and "concrete_id" but you can add all other data
$arr[] = array("id" => 46, "concrete_id" => "5c3f55b5ebb084c078c92d13");
$arr[] = array("id" => 46, "concrete_id" => "fus98f67a9");
$arr[] = array("id" => 46, "concrete_id" => "5c3f55b5ebb084c078c94c0");
$arr[] = array("id" => 43, "concrete_id" => "5c3f55b5ebb084c078c92d13");
$arr[] = array("id" => 43, "concrete_id" => "fus98f67a9");

foreach($arr as $e) {
    if (!isset($ans[$e["id"]])) {
        $ans[$e["id"]] = $e; // element doesn't exist set first time
        $ans[$e["id"]]["concrete_id"] = [$e["concrete_id"]]; // make the concrete_id as array with 1 element
    } else { // the id already set just append the concrete_id
        $ans[$e["id"]]["concrete_id"][] = $e["concrete_id"];
    }
}