0

I have an array which contains multiple of the same data:

array(2) {
  [0]=>
  array(2) {
    ["id"]=>
    string(11) "43000173601"
    ["data"]=>
    array(2) {
      [0]=>
      array(2) {
        ["id"]=>
        string(5) "52874"
        ["name"]=>
        string(3) "x70"
      }
      [1]=>
      array(2) {
        ["id"]=>
        string(5) "52874"
        ["name"]=>
        string(3) "x70"
      }
    }
  }
  [1]=>
  array(2) {
    ["id"]=>
    string(11) "43000173602"
    ["data"]=>
    array(1) {
      [0]=>
      array(2) {
        ["id"]=>
        string(5) "52874"
        ["name"]=>
        string(3) "x70"
      }
    }
  }
}

I have tried using array_unique() to remove these entries, but receive this error:

Array to string conversion

The outer arrays contain route ID's, some busses may have 2 different routes so in this case, they can stay however, I just want to remove the dupe entries inside the 1 route:

[0]=>
  array(2) {
    ["id"]=>
    string(11) "43000173601"
    ["data"]=>
    array(2) {
      [0]=>
      array(2) {
        ["id"]=>
        string(5) "52874"
        ["name"]=>
        string(3) "x70"
      }
      [1]=>
      array(2) {
        ["id"]=>
        string(5) "52874"
        ["name"]=>
        string(3) "x70"
      }
    }
9
  • 1
    And what do you want from us? Commented Oct 20, 2018 at 19:08
  • To help me remove the duplicate entries inside the routes, please @u_mulder Commented Oct 20, 2018 at 19:10
  • Always the lowest key that should remain? Commented Oct 20, 2018 at 19:10
  • this comment in the docs might help: php.net/manual/de/function.array-unique.php#97285 Commented Oct 20, 2018 at 19:11
  • 1
    When posting questions it's best to post arrays made by var_export and not print_r the ones from var export are usable in code, the ones from print_r are human readable, so to use it one has to fix it first, where as var export can just be pasted and worked with. I'd help you but I'm to lazy to fix it. One day I will make a print_r converter ... Commented Oct 20, 2018 at 19:24

3 Answers 3

1

Code can be something like this:

$new_array = [];
foreach ($your_array as $item) {
    if (!isset($new_array[$item['id']])) {
        // item id is not in `new_array` - add `item`
        $new_array[$item['id']] = $item;
    } else {
        // item id already presents - add `data` item to it
        $new_array[$item['id']]['data'][] = $item['data'];
    }
}
Sign up to request clarification or add additional context in comments.

1 Comment

Thanks! This did work but I found an easier way to do it, but I will mark this since it does work!
1

You can use array_column to make the array associative. That will remove any duplicates.
Array_values will then remove the associative and make it normal indexed array again.
Rsort makes sure you get the lowest key as the result array.

rsort($arr);
$arr = array_values(array_column($arr, Null, "id"));

Comments

1

I fixed it by doing this:

$stripped = [];
foreach($arr as $single) {
    $stripped[] = ['id' => $single['id'], 'data' => array_unique($single['data'])];
}

Since the duplicates existed inside the inner arrays, not the outer array, I had to use array_unique() on the inner arrays.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.