简体   繁体   English

如何在PHP的多维数组中获得唯一元素?

[英]How do I get unique elements in my multidimensional array in PHP?

This is my array: 这是我的数组:

Array
    (
        [0] => Array
            (
                [entity_id] => 1
                [value] => new
                [label] => New
            )
        [1] => Array
            (
                [entity_id] => 3
                [value] => pending_payment
                [label] => Pending Payment
            )
        [2] => Array
            (
                [entity_id] => 4
                [value] => pending_paypal
                [label] => Pending Paypal
            )
        [3] => Array
            (
                [entity_id] => 5
                [value] => processing
                [label] => Processing
            )
        [4] => Array
            (
                [entity_id] => 6
                [value] => complete
                [label] => Complete
            )
        [5] => Array
            (
                [entity_id] => 7
                [value] => canceled
                [label] => Canceled
            )
        [6] => Array
            (
                [entity_id] => 8
                [value] => closed
                [label] => Closed
            )
        [7] => Array
            (
                [entity_id] => 9
                [value] => holded
                [label] => Holded
            )
        [8] => Array
            (
                [entity_id] => 10
                [value] => payment_review
                [label] => Payment Review
            )
        [9] => Array
            (
                [entity_id] => 11
                [value] => new
                [label] => New
            )
        [10] => Array
            (
                [entity_id] => 13
                [value] => pending_payment
                [label] => Pending Payment
            )

UPDATE This is the result of the print_r . UPDATE这是print_r的结果。 As you can see the array[0] with array[6] are the same. 如您所见, array [0]array [6]相同。 Also the array[7] with the array[1]. 还有array [7]和array [1]。 How can I get rid of one of them ? 我如何摆脱其中之一? thx 谢谢

I tried smth like this: 我试过这样的东西:

$input = $my_array
$temp  = $input;

foreach ( $temp as &$data ) {
    unset($data['id']);
}

$output = array_intersect_key($input, array_unique($temp));

but with no any result :( . 但没有任何结果:(。

$result = array();

foreach ($myArray as $array) {
    if (isset($result[$array['value']])) {
        continue;
    }
    $result[$array['value']] = $array;
}

print_r($result);

Hey Attila this should help. 嘿,阿提拉应该会有所帮助。 Your $my_array is renamed in this example into $wholeArray . 在此示例中,您的$my_array重命名为$wholeArray And I thought it woulf be enough to compare the labels: 我认为足以比较标签:

$index=0;
$length = count($wholeArray);
foreach($wholeArray as $arrayElement){
    $temp = $arrayElement['label'];
    $index++;
    for($i = $index; $i < $length; $i++){
        if($wholeArray[$i]['label'] == $temp){
            unset($wholeArray[$i]);
            $length --;
        }
    }
}

If you have any questions to the code feel free to ask :) 如果您对代码有任何疑问,请随时提出:)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM