2

I have an array like this-

Array
(
    [0] => Array
        (
            [size] => 12" x 24"
            [size_description] => <p>Rectified</p>

        )

[1] => Array
    (
        [size] => 12" x 24"
        [size_description] => <p>Rectified</p>

    )

[2] => Array
    (
        [size] => 24" x 24"
        [size_description] => <p>Rectified</p>

    )

[3] => Array
    (
        [size] => 24" x 24"
        [size_description] => <p>Rectified</p>

    )

[4] => Array
    (
        [size] => 24" x 48"
        [size_description] => <p>Rectified</p>

    )

[5] => Array
    (
        [size] => 24" x 48"
        [size_description] => <p>Rectified</p>

    )

)

I want to get the distinct subarrays based on "size" and I can loop both the size and size_description. I have tried array_unique which is not working right, I am getting only one value that is size. What I tried is

$new_array = array_unique(array_map(function($elem){return $elem['size'];}, $size_array));

I want to get both the values. Is there any way to do this?

mickmackusa
  • 43,625
  • 12
  • 83
  • 136
Al Waqar
  • 111
  • 2
  • 9
  • I guess you could use [array_filter](https://www.php.net/manual/en/function.array-filter.php)? – Anthony Apr 08 '19 at 10:40
  • When you post array data on this site, always offer the `var_export()` output, so that volunteers don't have to reformat the data to use it in their tests. Your question would be clearer with an exact expected output. If the `size_description` values in your project data may be different, you should have crafted a more realistic question. – mickmackusa Apr 08 '19 at 11:43

3 Answers3

2

This will give you the desired result

$newArr = array();

foreach($arr as $key => $value){

   if(!in_array($value['size'], $newArr))
    $newArr[$value['size']] = $value;

  }

Result:-

 Array
(
  [12" x 24"] => Array
    (
        [size] => 12" x 24"
        [size_description] => Rectified


    )

[24" x 24"] => Array
    (
        [size] => 24" x 24"
        [size_description] => Rectified


    )

[24" x 48"] => Array
    (
        [size] => 24" x 48"
        [size_description] => Rectified


    )

)
Rakesh Jakhar
  • 6,380
  • 2
  • 11
  • 20
  • Thank you @Rakesh, your answer helped me a lot. But there is a better answer, so we must focus on quality. – Al Waqar Apr 08 '19 at 12:44
2

Use array_column() to assign new associative keys using size without changing the subarray contents. This is done with the null parameter.

Then just reindex with array_values()

Code: (Demo)

$array = [
    ['size' => '12" x 24"', 'size_description' => '<p>Rectified</p>'],
    ['size' => '12" x 24"', 'size_description' => '<p>Rectified</p>'],
    ['size' => '24" x 24"', 'size_description' => '<p>Rectified</p>'],
    ['size' => '24" x 24"', 'size_description' => '<p>Rectified</p>'],
    ['size' => '24" x 48"', 'size_description' => '<p>Rectified</p>'],
    ['size' => '24" x 48"', 'size_description' => '<p>Rectified</p>'],
];

var_export(array_values(array_column($array, null, 'size')));

Array keys may not be duplicated -- unique by force.

mickmackusa
  • 43,625
  • 12
  • 83
  • 136
1

the code given by @Rakesh_jakhar is cool, but accessing by key is faster in php-array due it is stored as a hash-table:

<?php
$size_array = [
    ...
];

$new_array = [];
foreach ($size_array as $item) {
    if (!($size_array[$item['size']] ?? null)) {
        $new_array[$item['size']] = $item;
    }
}

$new_array = array_values($new_array);
var_dump($new_array);

and if you need to have an numeric array - use array_values

(demo)

myxaxa
  • 1,391
  • 1
  • 8
  • 7