-1

I'm building out a filtering function that loops through provided data to generate select field options based on a declared array key.

I can get my function to output the expected unique results when the key value is a string, however when I set the key to an array, I only get the first 1 result.

This is the example I have:

    /* ----------------------
    Dump the output
---------------------- */
function dump($data) {
  if(is_array($data)) { //If the given variable is an array, print using the print_r function.
    print "<pre>\n";
    print_r($data);
    print "</pre>";
  } elseif (is_object($data)) {
    print "<pre>\n";
    var_dump($data);
    print "</pre>";
  } else {
    print "=========&gt; ";
    var_dump($data);
    print " &lt;=========";
  }
}

$data = '{"filter":[{"department":null,"location":{"country":"United States","country_code":"US","region":"New York","region_code":"NY","city":"New York","zip_code":null,"telecommuting":false}},{"department":null,"location":{"country":"United Kingdom","country_code":"GB","region":"England","region_code":"England","city":"Leeds","zip_code":null,"telecommuting":false}},{"department":"Project Management","location":{"country":"United Kingdom","country_code":"GB","region":"England","region_code":"England","city":"Manchester","zip_code":null,"telecommuting":false}},{"department":"Project Management","location":{"country":"United Kingdom","country_code":"GB","region":"England","region_code":"England","city":"London","zip_code":null,"telecommuting":false}},{"department":"Customer Success","location":{"country":"United Kingdom","country_code":"GB","region":"England","region_code":"England","city":"London","zip_code":null,"telecommuting":false}},{"department":null,"location":{"country":"United Kingdom","country_code":"GB","region":"England","region_code":"England","city":"London","zip_code":null,"telecommuting":false}}]}';

$response = json_decode($data, true);

// get unique values in array
function getUniqueValues($array, $args) {

    $result = array();

    foreach($array as $value) {

      if(!empty($value[$args])) {

        if(is_array($value[$args])) {

          foreach ($value[$args] as $k => $v) {
            $result[$k] = $v;
          }

        } else {

          $result[] = $value[$args];

        }

      }

    }

    return array_unique($result);
}

dump(getUniqueValues($response['filter'], 'location'));

dump(getUniqueValues($response['filter'], 'department'));

The output for any key matching 'department', outputs the expected results

Array 
(
    [0] => Project Management
    [2] => Customer Success
)

However when referencing a key with an array as the value I don't get the expected results.

Array
(
    [country] => United Kingdom
    [country_code] => GB
    [region] => England
    [city] => London
    [zip_code] => 
)

What I was expecting to see was something similar to this.

Array
(
    [0] => (
        [country] => United Kingdom
        [country_code] => GB
        [region] => England
        [city] => London
        [zip_code] => 
    ),
    [1] => (
        [country] => United Kingdom
        [country_code] => GB
        [region] => England
        [city] => Manchester
        [zip_code] => 
    )
)

I think I seem to of missed something, or I'm misunderstanding how array_unique($myArray) works. In any case how do I go about solving this to give the correct output?

Any help would be greatly appreciated.

Thanks :)

Matt
  • 806
  • 3
  • 12
  • 32

1 Answers1

0

Based on this answer https://stackoverflow.com/a/2561283/1376820, I've modified the loop to push each value that is an array in to a new array $arrChild.

if(is_array($value[$args])) {

    array_push($arrChild, $value[$args] );

    $result = array_intersect_key($arrChild, array_unique(array_map('serialize', $arrChild)));

  }

Full example here: https://eval.in/990453

Matt
  • 806
  • 3
  • 12
  • 32