4

I have a multidimensional array that is built like this (don't mind the string lengths, I modified the values)

array(27) {
  [0]=>
  array(2) {
    ["name"]=>
    string(16) "Arsenal FC"
    ["id"]=>
    string(2) "1"
  }
  [1]=>
  array(2) {
    ["name"]=>
    string(8) "Liverpool FC"
    ["id"]=>
    string(3) "2"
  }
[2]=>
  array(2) {
    ["name"]=>
    string(18) "Manchester United"
    ["id"]=>
    string(3) "3"
  }
}

Now I want to add an array, that is built like this

array(2) {
  ["name"]=>
  string(18) "Chelsea FC"
  ["id"]=>
  string(3) "4"

between the second and third array element.

I have done it like this

$this->clubs = array_slice($this->clubs, 0, 2, true) +
['name' => 'Chelsea FC', 'id' => 4] +
array_slice($this->clubs, 2, sizeof($this->clubs), true);

However, this returns the following array

array(27) {
  [0]=>
  array(2) {
    ["name"]=>
    string(16) "Arsenal FC"
    ["id"]=>
    string(2) "1"
  }
  [1]=>
  array(2) {
    ["name"]=>
    string(8) "Liverpool FC"
    ["id"]=>
    string(3) "2"
  }
  ["name"]=>
  string(20) "Chelsea FC"
  ["id"]=>
  int(4)
  [2]=>
  array(2) {
    ["name"]=>
    string(18) "Manchester United"
    ["id"]=>
    string(3) "3"
  }

As you can see, the array is added to the correct position, but not in the correct format (not multidimensional). What I would need to do now, is to add the array at the position and increase the count of all other indexes.

I tried to achieve it like this

if ($otherClubs->count() > 0) {

    array_walk($this->clubs, function($item, $key) use($index, $otherClubs) {
        if ($key > $index) {
            $this->clubs[$key + $otherClubs->count()] = $item;
            unset($this->otherClubs[$key]);
        }
    });
}

but this destroyed the array completely

array(36) {
  [0]=>
  array(2) {
    ["name"]=>
    string(16) "Arsenal FC"
    ["id"]=>
    string(2) "1"
  }
  [1]=>
  array(2) {
    ["name"]=>
    string(8) "Liverpool FC"
    ["id"]=>
    string(3) "2"
  }
  ["name"]=>
  string(20) "Chelsea FC"
  ["id"]=>
  int(3)
  [2]=>
  array(1) {
    ["id"]=>
    string(3) "4"
  }

How can I achieve this?

2 Answers 2

5

UPDATE: try array_splice:The OPTION 1 is better and less messy and it will preserve the key.

1):

$arr=array (
    '0'=>
    array(
        "name"=>"Arsenal FC",
        "id"=>"1"
    ),
    '1'=>
    array(
        "name"=>"Liverpool FC",
        "id"=>"2"
    ),
    '2'=>
    array(
        "name"=>"Manchester United",
        "id"=>"3"
));

array_splice($arr, 2, 0, array(array('name' => 'Chelsea FC', 'id' => 4)));
echo "<pre>";
var_dump($arr);

OUTPUT:

array(4) { [0]=> array(2) { ["name"]=> string(10) "Arsenal FC" ["id"]=> string(1) "1" } [1]=> array(2) { ["name"]=> string(12) "Liverpool FC" ["id"]=> string(1) "2" } [2]=> array(2) { ["name"]=> string(10) "Chelsea FC" ["id"]=> int(4) } [3]=> array(2) { ["name"]=> string(17) "Manchester United" ["id"]=> string(1) "3" } }

OR your style of attempt.

2):

<?php 
$arr=array (
    '0'=>
    array(
        "name"=>"Arsenal FC",
        "id"=>"1"
    ),
    '1'=>
    array(
        "name"=>"Liverpool FC",
        "id"=>"2"
    ),
    '2'=>
    array(
        "name"=>"Manchester United",
        "id"=>"3"
));


$arr = array_values(array_slice($arr, 0, 2, true) + array(sizeof($arr)=>array('name' => 'Chelsea FC', 'id' => 4)) + array_slice($arr, 2, sizeof($arr), true));
echo "<pre>";
var_dump($arr);
?>
Sign up to request clarification or add additional context in comments.

1 Comment

Your first attempt is much better than the one I used. And it works without problems! Thank you!
2

The + tries the merge the array. So the array you are trying to add should also be multidimensional

$this->clubs = array_slice($this->clubs, 0, 2, true) +
[1 => ['name' => 'Chelsea FC', 'id' => 4]] +
array_slice($this->clubs, 2, sizeof($this->clubs), true);

The downside is that you should provide a key to the array that you want to add.

Or you could use array_merge

$this->clubs = array_merge(
    array_slice($this->clubs, 0, 2, true),
    [['name' => 'Chelsea FC', 'id' => 4]],
    array_slice($this->clubs, 2, sizeof($this->clubs), true)
);

7 Comments

Thanks for the comment, but when I do it like in your comment, the new array element is nowhere to be found in the new array, meaning when I dump it, it's simply not there
The new array must have a key to have a successful merge
Yeah, that's why I wrote, that I would need to increase all the other array values, so the new array element doesn't override the old ones. See my attempt with array_walk. But the problem I would have there, is, that I would increase the 4th element to have the index 5 and override the 5th array element. I need to somehow increase all the values without overriding others
Or just use array_merge if you don't want to provide a key (php.net/manual/en/function.array-merge.php)
array_merge however does not preserve keys, and I can't simply invent a new one. However, this lead me to another solution. I simply added a key, generated by the md5 hash value of the name and the id (so it's unique) [md5($club->name . $club->id) => ['name' => ' - ' . $club->name, 'id' => $club->id]] + This seems to work.
|

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.