I'm trying to create a tables with information from two arrays. Here are the two arrays:
First array, for table headers
Array
(
[0] => Color
[1] => Length
[2] => Waist
)
Second array, the one that needs modification
Array
(
[0] => Array
[0] => green [1] => Color
[1] => Array
[0] => 23 [1] => Length
)
Array
(
[0] =>
)
Array
(
[0] => Array
[0] => 23 [1] => Length
[1] => Array
[0] => 24 [1] => Waist
)
Array needs to look like this:
Array
(
[0] => Array
[0] => green [1] => Color
[1] => Array
[0] => 23 [1] => Length
[2] => Array
[0] => [1] => Waist
Array
(
[0] => Array
[0] => [1] => Color
[1] => Array
[0] => [1] => Length
[2] => Array
[0] => [1] => Waist
Array
(
[0] => Array
[0] => [1] => Color
[1] => Array
[0] => 23 [1] => Length
[2] => Array
[0] => 24 [1] => Waist
So the point is that the keys in the first level needs to match the keys in the array that makes the table headers, where [1] one the second level has the same value as the table header. Any ideas?
After some feedback, an alternative acceptable output array structure would be:
array(
array(
'Color' => 'green',
'Length' => 23,
'Waist' => null
),
array(
'Color' => null,
'Length' => null,
'Waist' => null
),
array(
'Color' => null,
'Length' => 23,
'Waist' => 24
)
)
You have a complex array structure for an easy set of data. Could your final array work better like this?
$data = array(
array(
'Color' => 'green',
'Length' => 23,
'Waist' => NULL
),
array(
'Color' => NULL,
'Length' => NULL,
'Waist' => NULL
),
array(
'Color' => NULL,
'Length' => 23,
'Waist' => 24
)
);
If you're dead set on your structure, though, this should work:
function format_my_array($keys, $malformed) {
foreach ($malformed as $key => $fragments) {
$temp = array(
'Color' => NULL,
'Length' => NULL,
'Waist' => NULL
);
foreach ($fragments as $fragment) {
if (isset($fragment[1])) {
switch($fragment[1]) {
case 'Length':
$temp['Length'] = $fragment[1];
break;
case 'Waist':
$temp['Waist'] = $fragment[1];
break;
default:
$temp['Color'] = $fragment[1];
break;
}
}
}
$malformed[$key] = array(
array($temp['Color'], 'Color'),
array($temp['Length'], 'Length'),
array($temp['Waist'], 'Waist')
);
}
return $malformed;
}