Reputation: 13
I have an array which looks like this:
Array
(
[20] => ADEP EGKK
[21] => ADES EGLL
[22] => AOARCID ABC
[23] => AOOPR ABC
[24] => ARCID ABC123
[25] => ARCTYP MD11
As you can see the first array key is 20, because the first 19 I unset using preg_match:
if (isset($_POST['plan']))
$fplparts = explode("-", $fpl);
$pattern = "/FAC|TITLE|BEGIN|END|PT|PTID|ATSRT|ICAOCONTENT/i";
foreach($fplparts as $key => $value) {
if (preg_match($pattern, $value)){
unset($fplparts[$key]);
}
}
print_r($fplparts);
Now how I would like my array to look like is this:
Array
(
[ADEP] => EGKK
[ADES] => EGLL
[AOARCID] => ABC
[AOOPR] => ABC
[ARCID] => ABC123
[ARCTYP] => MD11
So basically, I would like to move the first word of each value and make it the key.
What is the best way to go about this?
Thanks in advanced.
Upvotes: 1
Views: 234
Reputation: 2912
You can do it like this:
$newArray = array();
foreach ($oldArray as $item)
{
list($key, $value) = explode(' ', $item, 2);
$newArray[$key] = $value;
}
But there are some limitations, for example you have to be sure that first string (which you want as key) will be unique, otherwise you will rewrite your data.
Third parameter (2
) in explode()
is used to make sure that ADEP EGKK EGKK EGKK
will result in:
[ADEP] => EGKK EGKK EGKK
Upvotes: 1