PHP array massaging
I have this array:
Array
(
0 => "3_some val",
1 => "1_some other val",
2 => "0_val",
3 => "2_value",
4 => "4_other value"
)
given the array above, is there any way to make an array like this from this?
Array
(
0 => "val",
1 => "some other val",
2 => "value",
3 => "some val",
4 => "other value"
)
actually make the number that precedes this underscore (_)be the key in the newly created array. thank
This should do it:
$arr1 = array (
0 => "3_some val",
1 => "1_some other val",
2 => "0_val",
3 => "2_value",
4 => "4_other value"
);
$result = array();
foreach($arr1 as $val) {
list($key, $value) = explode('_', $val, 2);
$result[$key] = $value;
}
// Sort by keys
ksort($result);
Execution print_r($result)will be printed:
Array
(
[0] => val
[1] => some other val
[2] => value
[3] => some val
[4] => other value
)