-2

This is the current output I have from an array by doing var_dump($getvals):

array(2) {
  ["value"]=>string(5) "150mm"
  ["label"]=>string(5) "150mm"
}
array(2) {
  ["value"]=>string(5) "150mm"
  ["label"]=>string(5) "150mm"
}
array(2) {
  ["value"]=>string(5) "150mm"
  ["label"]=>string(5) "150mm"
}
array(2) {
  ["value"]=>string(5) "150mm"
  ["label"]=>string(5) "150mm"
}
array(2) {
  ["value"]=>string(5) "125mm"
  ["label"]=>string(5) "125mm"
}
array(2) {
  ["value"]=>string(5) "125mm"
  ["label"]=>string(5) "125mm"
}

What I want to achieve is first of all, to ignore the ['label'] tables and then try removing any duplicates (150mm and 125mm). I have tried both array_unique and !inarray() but it won't work. Here is my code:

$getvals = get_sub_field('option_diameter'); // This is my array
$takens = array(); // I create an empty array for later on
foreach ($getvals as $key => $getval){ // Running through the array
  if ($key == 'value'){ // Ignoring Label table
    if(!in_array($getval, $takens)){ // Check if the current value is already inside $takens array
      $takens[] = $getval; // If not, then put it
    }
  }
}
var_dump($takens); // The output is below

This is what I get from var_dump($takens) :

array(1) {
  [0]=>string(5) "150mm"
}
array(1) {
  [0]=>string(5) "150mm"
}
array(1) {
  [0]=>string(5) "150mm"
}
array(1) {
  [0]=>string(5) "150mm"
}
array(1) {
  [0]=>string(5) "125mm"
}
array(1) {
  [0]=>string(5) "125mm"
}

The duplicate values are not removed. Any ideas?

dominotrix
  • 367
  • 5
  • 19

4 Answers4

0

You need to first fetch value by using array_diff_key. Then you need to use array_column, in which passing null as second parameter and unique by value as key to removing duplicates(as keys can not be duplicated). Then to reset indexes array_values

Here is the script,

$takens = array_values(array_column(array_map(function ($a) {
    return array_diff_key($a, ['label' => '']);
}, $getvals), null, 'value'));

Demo

Output:-

array(2) {
  [0]=>
  array(1) {
    ["value"]=>
    string(5) "150mm"
  }
  [1]=>
  array(1) {
    ["value"]=>
    string(5) "125mm"
  }
}
Rahul
  • 18,271
  • 7
  • 41
  • 60
0

You can use array_column

 $r = array_values(array_column($a, null, 'label'));

OR

if you want to keep single column

$r = array_values(array_column($a, 'value', 'label'));
print_r($r);

array_values will reorder the keys of the array

Working example :- https://3v4l.org/n8SA1

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

Use a key to remove the duplicate, Demo

$result = [];
foreach($array as $arr){
    $result[$arr['value']] = $arr;
}
print_r(array_values($result));
LF00
  • 27,015
  • 29
  • 156
  • 295
  • @dominotrix check the demo. I cannot see your demo, so I cannot figure out the issue with your code. – LF00 Oct 22 '19 at 09:31
  • I checked your demo of course, but the demo array you have is not the same as mine. The structure is different. Your code works for your array only - sadly not for mine. You think my array structure is wrong somehow? – dominotrix Oct 22 '19 at 09:39
  • I use the structure in your question. – LF00 Oct 22 '19 at 09:42
  • If the structure in your quesion is not the same as your's. Please edit it with what really you're using. – LF00 Oct 22 '19 at 09:43
  • The one i posted is the output of my array when i do `var_dump`. Did you actually copy-paste my array into 3v4L ? Cause it seems different. – dominotrix Oct 22 '19 at 09:48
0

It look like you forgot a foreach. Here a test done a few minutes ago :

$aArrayTest = array (
            array(
                'value'=>'150mm',
                'label'=>'150mm'
            ), 
            array(
                'value'=>'150mm',
                'label'=>'150mm'
            ),
            array(
                'value'=>'150mm',
                'label'=>'150mm'
            ),
            array(
                'value'=>'150mm',
                'label'=>'150mm'
            ),
            array(
                'value'=>'125mm',
                'label'=>'125mm'
            ),
            array(
                'value'=>'125mm',
                'label'=>'125mm'
            )
        );

    $aArrayResult = array();
    foreach ($aArrayTest as $aArray) { // Running through the array of Arrays
        foreach ($aArray as $key => $value){ // Running through each array
            if ($key == 'value'){ // Ignoring Label table
                if(!in_array($value, $aArrayResult)){ // Check if the current value is already inside $takens array
                    $aArrayResult[] = $value; // If not, then put it
                }
            }
        }
    }
    var_dump($aArrayResult);</pre>