'How to map the two arrays with a duplicate value?
I have two arrays.
$array1 = ['id_e' =>[91707, 91708]];
$array2 = ['id_s' => [18, 57]];
If I want to insert or delete into the database, I want to make one-to-many mappings on these two arrays. And the result I expect it to be a new array as shown below.
Final Array:
array (size=4)
0 =>
array (size=2)
'id_e' => int 91707
'id_s' => int 18
1 =>
array (size=2)
'id_e' => int 91707
'id_s' => int 57
array (size=2)
2 =>
array (size=2)
'id_e' => int 91708
'id_s' => int 18
3 =>
array (size=2)
'id_e' => int 91708
'id_s' => int 57
I'm stuck after returning array1 and array2. I'm a beginner in php.
How do I do this?
Solution 1:[1]
Easiest way is:
$res = array();
foreach ($array1['id_e'] as $ide)
foreach ($array2['id_s'] as $ids)
$res[] = array('id_e' => $ide, 'id_s' => $ids);
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
Solution | Source |
---|---|
Solution 1 |