So, to simply put it, it looks like this:
Array
(
[0] => id
[1] => name
[2] => email
)
Array
(
[0] => 1
[1] => paula
[2] => paula@paula.com
[3] => 2
[4] => martin
[5] => martin@google.com
[6] => 3
[7] => kasandra
[8] => kasandra@google.com
[9] => 4
[10] => helena
[11] => helena@google.com
[12] => 5
[13] => sophia
[14] => sophia@google.com
[15] => 6
[16] => denis
[17] => denis@google.com
)
How to make those values from Array1 as keys in Array2 accordingly so that the end result looks like this:
Array
(
[id] => 1
[name] => paula
[email] => paula@paula.com
[id] => 2
[name] => martin
[email] => martin@google.com
[id] => 3
[name] => kasandra
[email] => kasandra@google.com
[id] => 4
[name] => helena
[email] => helena@google.com
[id] => 5
[name] => sophia
[email] => sophia@google.com
[id] => 6
[name] => denis
[email] => denis@google.com
)
you can use a for loop with step 3
$numCoords = count($array2)/3
for ($i = 0; $i < $numCoords; $i++ ){
$array[$i]['id'] = $array2[$i*3];
$array[$i]['name'] = $array2[($i*3)+1];
$array[$i]['email'] = $array2[($i*3)+2];
}
I would go with array_chunk
and get used to numeric indexes, but if you really want to have them as words you may map them:
$keys = [
0 => 'id',
1 => 'name',
2 => 'email'
];
$chunked = array_chunk($array2, $length=3);
$result = array_map(function ($chunk) {
global $keys;
return array_combine($keys, $chunk);
}, $chunked);
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.