Manipulate and loop through nested PHP arrays
Ok... I hate array's... I dont understand them and dont like working with them... but im working on a project where I have no control of the data I am pulling, but need a way to sort it into a format I can manipulate...
So, the data looks something like this:
$arr = Array (
[1] => Array ( [December] => a date here [December (2)] => a location here... [December (3)] => an event here... )
[2] => Array ( [December] => a date here [December (2)] => a location here... [December (3)] => an event here... )
[3] => Array ( [December] => a date here [December (2)] => a location here... [December (3)] => an event here... )
[4] => Array ( [December] => a date here [December (2)] => a location here... [December (3)] => an event here... )
[5] => Array ( [December] => 开发者_如何学Pythona date here [December (2)] => a location here... [December (3)] => an event here... )
)
First I need to change 'December', 'December (2)' & 'December (3)' to 'Date', 'Location' & 'Event'...
Then I need a way to reference the new keys in each array in a foreach loop or similar that I can then insert into a database?
Something like:
$arr[1][Date];
but I guess $arr[1] would need to be something like $arr[$i]...?
Can someone help me with this please? It would be much appreciated... I have trying to get this right for sometime...
You would want to copy the values over into a new array. You could do something like:
foreach ($arr as $a)
{
// this gets the keys 'December', 'December (2)', etc
$keys = array_keys($a)
// this copies the values to the new array
$newarr['date'] = $a[$keys[0]];
$newarr['location'] = $a[$keys[1]];
$newarr['event'] = $a[$keys[2]];
// ... continue with as many fields as you have or need
}
Each element would then be accessible via $newarr[1]['date']
and so on. The upshot of using array_keys()
instead of hardcoding in "December(3)" and so on is that this function will work regardless of the month (I'm assuming you'll probably have to run this against January, February, and so on.
In all honesty, though, if you aren't comfortable with arrays, you probably shouldn't be trying to do database work...
First I need to change 'December', 'December (2)' & 'December (3)' to 'Date', 'Location' & 'Event'...
foreach ($arr as &$a) {
$a["Date"] = $a["December (2)"];
unset($a["December (2)"]);
$a["Location & Event"] = $a["December (3)"];
unset($a["December (3)"]);
}
//$a is a reference; this avoids accidental overwrites on the last subarray
unset($a);
You can loop the array again, and you'll have the new keys:
foreach ($arr as $a) {
//do sth with $a["December"];
//do sth with $a["Date"];
//do sth with $a["Location & Event"];
}
Something like this:
<?php
$array = array(
array(
'date' => ...,
'location' => ...,
'event' => ....
),
...
);
?>
?
Assuming the keys are always 'December', 'December (2)' etc, this should take care of the renaming:
foreach ($array as &$entry) {
$entry = array('Date' => $entry['December'], 'Location' => $entry['December (2)'], 'Event' => $entry['December (3)']);
}
From there, each entry is accessible at $array[0]['Date']
, $array[1]['Date']
etc. You can loop over them in a for
loop and use $array[$i]['Date']
, or you use another foreach
loop. That's a basic exercise I'll leave up to you though.
精彩评论