-1

I am trying to dynamically merge multiple arrays. Consider below three different array inputs:

{
    "container_id": [
    {
      "key_0": "NYKU3922051"
    },
    {
      "key_0": "LACU3922051"
    }
    ],
  "out_ref": [
    {
      "key_0": "CI84621"
    },
    {
      "key_0": "DI1000"
    }
  ],
  "in_ref2": [
    {
      "key_0": "555-1106260-024"
    },
    {
      "key_0": "898-1106260-024"
    }
  ]
}

I am trying to dynamically merge above three arrays container_id, out_ref and in_ref2 into one associative one like this:

  {
     "array_one": {
         "key_0": "NYKU3922051",
         "key_0": "CI84621",
         "key_0": "555-1106260-024"
     },
     "array_two": {
         "key_0": "LACU3922051",
         "key_0": "DI1000",
         "key_0": "898-1106260-024"        
     }
  }

So what we have above, is that the first item of each array, is merged into a new array.

I am not quite sure where to start, as I can' really wrap my head around this one.

This is my PHP:

  $arrays = $request->all();

  $newArray = [];
  foreach ($arrays as $key => $value) {
       //???
  }

EDIT The new array doesn't necessarily need to use the same key if this is not possible.

3
  • @Yoshi - OK they don't necessarily need to use the key key_0. It can just as well be: key_0, key_1, key_2 Commented Jan 24, 2019 at 14:10
  • Key can't be duplicate, only value can be Commented Jan 24, 2019 at 14:12
  • @oliverbj So you'd be okay with a numerically-indexed array instead of an associative array? Commented Jan 24, 2019 at 14:13

3 Answers 3

3

Your best option is to skip the "key_0" and get a result like:

array(2) {
  ["array_one"]=>
  array(3) {
    [0]=>
    string(11) "NYKU3922051"
    [1]=>
    string(7) "CI84621"
    [2]=>
    string(15) "555-1106260-024"
  }
  ["array_two"]=>
  array(3) {
    [0]=>
    string(11) "LACU3922051"
    [1]=>
    string(6) "DI1000"
    [2]=>
    string(15) "898-1106260-024"
  }
}

By using this code that loops and seperates the values to the two arrays.

$arr = json_decode($str,true);

foreach($arr as $sub){
    $new["array_one"][] = $sub[0]["key_0"];
    $new["array_two"][] = $sub[1]["key_0"];
}

var_dump($new);

https://3v4l.org/QKq4H


Dynamic version that can handle any amount of subarrays.
I added a counter that counts up and keeps track which array to place the value in.

$i = 1;
foreach($arr as $sub){
    foreach($sub as $val){
        $new["array_" . $i][] = $val["key_0"];
        $i++;
    }
    $i = 1;
}

var_dump($new);

Results in:

array(3) {
  ["array_1"]=>
  array(3) {
    [0]=>
    string(11) "NYKU3922051"
    [1]=>
    string(7) "CI84621"
    [2]=>
    string(15) "555-1106260-024"
  }
  ["array_2"]=>
  array(3) {
    [0]=>
    string(11) "LACU3922051"
    [1]=>
    string(6) "DI1000"
    [2]=>
    string(15) "898-1106260-024"
  }
  ["array_3"]=>
  array(3) {
    [0]=>
    string(9) "something"
    [1]=>
    string(4) "else"
    [2]=>
    string(4) "here"
  }
}

https://3v4l.org/OkcXT

Sign up to request clarification or add additional context in comments.

3 Comments

But what if there is “array_three” and four? In this example there is two arrays because each of the arrays gave two values. But if they have three or four values then I would need to dynamically add more arrays
@oliverbj I added a dynamic version
thanks! The dynamic version is perfect - it solves my problem!
0

Why can't you construct array like this, this is the link helpful for you for array traversing traversing an array in php and https://www.tutorialspoint.com/php/php_arrays.htm

{
     "array_one": {
             "key_0": {
                    0 : "NYKU3922051",
                    1 : "CI84621",
                    2 : "555-1106260-024"
              }
     },
     "array_two": {
             "key_0": {
                 0 : "LACU3922051",
                 1 : "DI1000",
                 2 : "898-1106260-024"        
            }
  }

8 Comments

I don’t control the input arrays.
Really not sure if this is an answer?
@oliverbj your expected output is not possible in this case as in PHP array keys must be unique
As said, I don’t care if the keys have dynamic names like “key_0”, “key_1” etc.
@oliverbj then why do you need keys in associative array, simply ignore them and use numeric array to avoid value overwrite
|
0

Try something like this

$arrays = $request->all();
$result = [];
foreach ($arrays as $array) {
    foreach ($array as $key_index => $keyObject) {
        if(!isset($result['array'.$key_index])) {
            $result['array'.$key_index] = ['key_0' => $keyObject['key_0']];
        } else {
            $result['array'.$key_index]['key_'.count($result['array'.$key_index])] = $keyObject['key_0'];
        }
    }
}

the part $keyObject['key_0'] can be changed to be more dynamic. a way to get the value inside that associative array with only one element.

you will get something like

array(2) {
  ["array0"]=>
  array(3) {
    ["key_0"]=>
    string(11) "NYKU3922051"
    ["key_1"]=>
    string(7) "CI84621"
    ["key_2"]=>
    string(15) "555-1106260-024"
  }
  ["array1"]=>
  array(3) {
    ["key_0"]=>
    string(11) "LACU3922051"
    ["key_1"]=>
    string(6) "DI1000"
    ["key_2"]=>
    string(15) "898-1106260-024"
  }
}

https://3v4l.org/vpqrI

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.