Share Knowledge Share Knowledge - 4 months ago 9
PHP Question

php Multidimensional arrray

I have Multidimensional array and it looks like this. this my snippet down below.

Snippet:

array (
'width_id' =>
array (
0 => string '8',
1 => string '24'
),
'profile_id' =>
array (
0 => string '8',
1 => string '13'
),
'diameter_id' =>
array (
0 => string '4',
1 => string '13'
),
'speed_id' =>
array (
0 => string '6',
1 => string '13'
),
'load_id' =>
array (
0 => string '12',
1 => string '31'
)
)


How can i create array like this from the above one.

$toDb = array (
array(
'width_id' => 8,
'profile_id' => 8,
'diameter_id' => 4,
'speed_id' => 6,
'load_id' => 12
),
array(
'width_id' => 24,
'profile_id' => 13,
'diameter_id' => 13,
'speed_id' => 13,
'load_id' => 31
))


This is my code down below and i can't get it done any help please.

$ToDb = array();
//$i = 0;
$count = 0;
foreach($tyreSpec as $row ) {
$count = count($row);
}

for($i = 0; $i < $count; $i++) {
foreach($tyreSpec as $row) {
array_push($ToDb, $row[$i]);
}
}

Answer

You can use two simple loops to achieve your goal:

$toDb = array();
foreach ($fromDb as $key=>$val) {
    for ($i=0; $i<count($val); ++$i)
        $toDb[$i][$key] = $val[$i];
}
Comments