0

So I have two seperate arrays. One with the IDs of the target, and one with the name of the target. Both are in cronilogical order.

Example:

  $array[1] => Array
    (
        [0] => 113139803806479652682
        [1] => 100276549585470687394
    )

and

 $array[2] => Array
    (
        [0] => EDM
        [1] => Space
    )

So in $array[1] the [0] in that is the ID for "EDM" (as seen in $array[2]) and then [1] in $array[1] is the name ID for "Space".

What I want to achieve is outputting a new array which would look like this:

  [0] => Array
 ( 
  [0] => 113139803806479652682
  [1] => EDM
 )
  [1] => Array
 (
 [0] => 100276549585470687394
 [1] => Space
 )

Basically a new array which returns a multiple dimensional array with the ID and name in the same array as taken from the two sepearte arrays that had the names and IDs in order already, just I want them formed so they go together.

I hope this makes sense. I'm kind of new to PHP and coding in general.

4 Answers 4

1
$results = [];

foreach($array1 as $key => $value) {
    $results[$key] = [$array1[$key],$array2[$key]];
}
Sign up to request clarification or add additional context in comments.

2 Comments

Well. My array I'm working with is already a multi dimensional array. I basically want to combine the results of [1] and [4] ([1] being the IDs and [4] being the names) into a new multi-dimensional array combinging the IDs and names into each other's array. If that makes any since.
maybe $results[$key] = [$array1[$key][1],$array2[$key][4]]? if your array is different from what's shown in the question, please update the question.
0
 $newarray=array();

foreach($array1 as $k=>$val){

  $newarray[$k]['ID'] = $val;
  $newarray[$k]['name'] = $array2[$k];

}

2 Comments

What's this code about? You should include brief too.
This solution takes the OP's plain English too literally and does not provide the desired result. Also, these are not separate arrays, they are two subarrays within a parent array.
0

It may be not the most elegant solution, but it'll do the trick. Here the code:

$arraySum = array();
for($i=0; $i<count($array1); $i++) {
    $arraySum[$i][$i] = $array1[$i];
    $arraySum[$i][$i+1] = $array2[$i];
}

1 Comment

Calling count() on each iteration of the loop is not best practice -- it should be cached. There is no $array1 nor $array2 to use so this code will not work for the posted question. $arraySum is a poor choice for a variable name because it does not contain any summed data. All answers, even simple ones, should be explained to researchers.
0

By calling upon array_map() with a null callback parameter and a "splat operator" on $array to "unpack it" as multiple single-dimensional input arrays, you can cleverly generate your desired, re-indexed output array in a concise one-liner.

Code: (Demo)

$array[1] = [
     '113139803806479652682',
     '100276549585470687394'
];

$array[2] = [
     'EDM',
     'Space'
];

var_export(array_map(null, ...$array));

Output:

array (
  0 => 
  array (
    0 => '113139803806479652682',
    1 => 'EDM',
  ),
  1 => 
  array (
    0 => '100276549585470687394',
    1 => 'Space',
  ),
)

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.