<?php
$data = [
[
"price_in_dollar" => 1000,
"price_in_euro" => 1000,
"price_in_pound" => 1000,
"price_in_rupee" => 1000,
"toy_id" => 1,
"toy_name" => "Truck"
],
[
"price_in_dollar" => 1000,
"price_in_euro" => 1000,
"price_in_pound" => 1000,
"price_in_rupee" => 1000,
"toy_id" => 2,
"toy_name" => "Bicycle"
]
];
$result = [];
foreach ($data as $row) {
['toy_id' => $id, 'toy_name' => $name] = $row;
unset($row['toy_id'], $row['toy_name']);
foreach ($row as $slug => $rate) {
$result[] = compact(['id', 'name', 'slug', 'rate']);
}
}
var_export($result);
- Output for 7.4.0 - 7.4.33, 8.0.1 - 8.0.30, 8.1.0 - 8.1.33, 8.2.0 - 8.2.29, 8.3.0 - 8.3.25, 8.4.1 - 8.4.12
- array (
0 =>
array (
'id' => 1,
'name' => 'Truck',
'slug' => 'price_in_dollar',
'rate' => 1000,
),
1 =>
array (
'id' => 1,
'name' => 'Truck',
'slug' => 'price_in_euro',
'rate' => 1000,
),
2 =>
array (
'id' => 1,
'name' => 'Truck',
'slug' => 'price_in_pound',
'rate' => 1000,
),
3 =>
array (
'id' => 1,
'name' => 'Truck',
'slug' => 'price_in_rupee',
'rate' => 1000,
),
4 =>
array (
'id' => 2,
'name' => 'Bicycle',
'slug' => 'price_in_dollar',
'rate' => 1000,
),
5 =>
array (
'id' => 2,
'name' => 'Bicycle',
'slug' => 'price_in_euro',
'rate' => 1000,
),
6 =>
array (
'id' => 2,
'name' => 'Bicycle',
'slug' => 'price_in_pound',
'rate' => 1000,
),
7 =>
array (
'id' => 2,
'name' => 'Bicycle',
'slug' => 'price_in_rupee',
'rate' => 1000,
),
)
preferences:
139.93 ms | 409 KiB | 5 Q