-2

I'm trying using PHP to bring the two JSON lists into one, as follows:

list1

[1,2,3,4,5]

list2

["a","b","c","d","e"]

How to join lists to get:

[[1,"a"],[2,"b"],[3,"c"],[4,"d"],[5,"e"]]

How to link two lists to one, as in a given example?

kamfulebu
  • 229
  • 1
  • 3
  • 11

1 Answers1

2

You need to loop through the arrays, and create a new one with the combined values:

<?php

$arr1 = [1,2,3,4,5];
$arr2 = ["a","b","c","d","e"];
$result = [];

foreach ($arr1 as $i => $a) {
    $result[]  = [$a, $arr2[$i] ?? ''];
}

?>
Claudio
  • 5,078
  • 1
  • 22
  • 33