更优雅的获取具有不同值的数组的方式

时间:2021-12-24 07:36:45

I have this array:

我有这个数组:

$array[] = [
      'a' => $a,
      'b' => $b,
];

The array contains of let's say 10 entries, $a can be in there with the same value many times and I need only one of those entries for a db insert.

这个数组包含10个元素,$a可以在其中多次使用相同的值对于db插入我只需要其中一个元素。

I can't manage to get array_unique working as it throws

我无法让array_unique像它那样运行

 array to string conversion

error when trying to use it like

尝试使用时出错

 $result = array_unique($array);

I now made a little foreach loop that feels just wrong to do so:

现在我做了一个小小的foreach循环,感觉这样做是不对的:

    $z = [];
    foreach ($array as $x) {

        if (@!in_array($x['a'],$z)) {
            $z[] = $x['a'];
        }
    }

and I use $z for the insert thereafter.

然后我用$z作为插入。

Can someone point me in the right direction of how to distinct my array values?

有人能指出正确的方向来区分数组的值吗?

3 个解决方案

#1


3  

This should work for you:

这应该对你有用:

($result = array_unique($array); this didn't worked, because you have a multidimensional array!)

美元($结果= array_unique(数组);这行不通,因为你有一个多维数组!

<?php


    //Example data
    $array[] = [
          'a' => 1,
          'b' => 1,
          'c' => 1,
          'd' => 2,
          'e' => 2,

    ];

    $array = array_map("array_unique", $array);
    print_r($array);

?>

Output:

输出:

Array ( [0] => Array ( [a] => 1 [d] => 2 ) )

#2


1  

Based on your array that is two dimensional, you would need:

根据你的二维数组,你需要:

$array = array_map('array_unique', $array);

Or if you don't need a two dimensional array, just use:

如果不需要二维数组,只需使用:

$array = [
      'a' => $a,
      'b' => $b,
];

And then: $array = array_unique($array);

然后:$array = array_unique($array);

#3


0  

One thing not mentioned is that arrays are built in unique, if you can manage the keys for them yourself. Associative arrays can only have the key once. So I like to do is use the primary key or a unique identifier for the key.

没有提到的一件事是,数组是在unique中构建的,如果您能够自己管理它们的键的话。关联数组只能有一次键。我喜欢使用主键或者唯一的键标识符。

You can't have an array with the same keys like this.

你不可能有这样一个键相同的数组。

array(
     'a' => $a
     'a' => $b
)

Because the key a is already a unique identifier. If you follow.

因为键a已经是唯一的标识符。如果你遵循。

#1


3  

This should work for you:

这应该对你有用:

($result = array_unique($array); this didn't worked, because you have a multidimensional array!)

美元($结果= array_unique(数组);这行不通,因为你有一个多维数组!

<?php


    //Example data
    $array[] = [
          'a' => 1,
          'b' => 1,
          'c' => 1,
          'd' => 2,
          'e' => 2,

    ];

    $array = array_map("array_unique", $array);
    print_r($array);

?>

Output:

输出:

Array ( [0] => Array ( [a] => 1 [d] => 2 ) )

#2


1  

Based on your array that is two dimensional, you would need:

根据你的二维数组,你需要:

$array = array_map('array_unique', $array);

Or if you don't need a two dimensional array, just use:

如果不需要二维数组,只需使用:

$array = [
      'a' => $a,
      'b' => $b,
];

And then: $array = array_unique($array);

然后:$array = array_unique($array);

#3


0  

One thing not mentioned is that arrays are built in unique, if you can manage the keys for them yourself. Associative arrays can only have the key once. So I like to do is use the primary key or a unique identifier for the key.

没有提到的一件事是,数组是在unique中构建的,如果您能够自己管理它们的键的话。关联数组只能有一次键。我喜欢使用主键或者唯一的键标识符。

You can't have an array with the same keys like this.

你不可能有这样一个键相同的数组。

array(
     'a' => $a
     'a' => $b
)

Because the key a is already a unique identifier. If you follow.

因为键a已经是唯一的标识符。如果你遵循。