-1

My array is like this,

$options = Array
    (
    [0] => Array
        (
            [value] => 180
            [label] => Nokia
        )

    [1] => Array
        (
            [value] => 2341
            [label] => Suisses
        )

    [2] => Array
        (
            [value] => 143
            [label] => Nokia
        )
     [3] => Array
        (
            [value] => 2389
            [label] => 3D Pop Art 
        )
    )

and i want output as,

Array(
[0] => Array
        (
            [value] => 180
            [label] => Nokia
        )
[2] => Array
        (
            [value] => 143
            [label] => Nokia
        )
)

Can anyone suggest me in this.

Claudio
  • 5,078
  • 1
  • 22
  • 33
  • 2
    And your approach? – nice_dev Sep 26 '19 at 07:43
  • https://stackoverflow.com/questions/4948946/php-check-for-duplicate-values-in-a-multidimensional-array <- this helps ? – nice_dev Sep 26 '19 at 07:46
  • Possible duplicate of [PHP: Check for duplicate values in a multidimensional array](https://stackoverflow.com/questions/4948946/php-check-for-duplicate-values-in-a-multidimensional-array) – Rob Streeting Sep 26 '19 at 08:03

3 Answers3

0

You can try something like this:

$options = [
    0 => [
            'value' => 180,
            'label' => 'Nokia'
        ],

    1 => [
            'value' => 2341,
            'label' => 'Suisses'
        ],

    2 => [
            'value' => 143,
            'label' => 'Nokia'
        ],
     3 => [
            'value' => 2389,
            'label' => '3D Pop Art'
        ],
];

$labels = [];
$duplicates = [];
foreach ($options as $option) {
    if (!empty($duplicates[$option['label']])) {
        $duplicates[$option['label']][] = $option;
    }

    if (empty($labels[$option['label']])) {
        $labels[$option['label']] = $option;
    } else {
        $duplicates[$option['label']] = [
            $labels[$option['label']],
            $option
        ];
    }
}
Claudio
  • 5,078
  • 1
  • 22
  • 33
0

It seems you are looking for group by 'label'

 foreach($options as $v){
  $c[$v['label']][] = $v['value'];
 }
 print_r($c);

Working example : https://3v4l.org/62dv0

Rakesh Jakhar
  • 6,380
  • 2
  • 11
  • 20
0

The array_filter() function is what you are looking for:

<?php
$data = [
    [
        'value' => 180,
        'label' => "Nokia"
    ],
    [
        'value' => 2341,
        'label' => "Suisses"
    ],
    [
        'value' => 143,
        'label' => "Nokia"
    ],
    [
        'value' => 2389,
        'label' => "3D Pop Art"
    ]
];
$output = [];
array_walk($input, function($entry) use (&$output) {
    $output[$entry['label']][] = $entry;
});

print_r(
    array_filter(
        $output, 
        function($entry) {
            return count($entry) > 1;
        }
    )
);

The output obviously is:

Array
(
    [Nokia] => Array
        (
            [0] => Array
                (
                    [value] => 180
                    [label] => Nokia
                )

            [1] => Array
                (
                    [value] => 143
                    [label] => Nokia
                )

        )
)

That output slightly differs from the one suggested by you. But it has the advantage that you can tell entries apart by the doubled label.

arkascha
  • 41,620
  • 7
  • 58
  • 90