This question already has an answer here:
Let's suppose I have the following array:
Array
(
[0] => Array
(
[0] => 1200
[1] => 2541
[2] => 2540
[3] => 2539
)
[1] => Array
(
[0] => Television
[1] => Monitor
[2] => Car
[3] => Bike
)
[2] => Array
(
[0] => Electrodomestic
[1] => Computer Stuff
[2] => Vehicle
[3] => Vehicle
)
)
And I would like to arrange it individually, like this:
Array
(
[0] => Array
(
[0] => 1200
[1] => Television
[2] => Electrodomestic
)
[1] => Array
(
[0] => 2541
[1] => Monitor
[2] => Computer Stuff
)
[2] => Array
(
[0] => 2540
[1] => Car
[2] => Vehicle
)
[3] => Array
(
[0] => 2539
[1] => Bike
[2] => Vehicle
)
)
How can I do that?
I would like to reoder my simple array (from data I extracted from a REGEX) into an array containing my individual "objects".
</div>
Have nothing to do, so here's a possible solution:
$a = Array
(
'0' => Array
(
'0' => 1200,
'1' => 2541,
'2' => 2540,
'3' => 2539,
),
'1' => Array
(
'0' => 'Television',
'1' => 'Monitor',
'2' => 'Car',
'3' => 'Bike',
),
'2' => Array
(
'0' => 'Electrodomestic',
'1' => 'Computer Stuff',
'2' => 'Vehicle',
'3' => 'Vehicle',
)
);
// take a size of every subarray
$t = sizeof($a[0]);
// do a loop
$new_a = [];
for($i =0; $i < $t; $i ++) {
// `array_column` extracts every value
// with key `$i` from each element of
// `$a` array and adds these values
// to a new array
$new_a[] = array_column($a, $i);
}
echo'<pre>',print_r($new_a),'</pre>';
Warning: array_column
introduced in php5.5
.