I want to create a key-value pairs in an array within a foreach. Here is what I have so far:
function createOfferUrlArray($Offer) {
$offerArray = array();
foreach ($Offer as $key => $value) {
$keyval = array($key => $value[4] );
array_push($offerArray,$keyval);
}
return $offerArray;
}
If I declare the array within the foreach, it will overwrites it on each iteration, but defining it outside the foreach doesn't work either and causes triplets:
array[0] => key => value
array[1] => key => value
How do I make it so I only get key-value pairs like this?
key => value
key => value
Create key-value pairs within a foreach like this:
Something like this?
Create key value pairs on the phpsh commandline like this:
Get the count of key value pairs:
Get the keys as an array:
In PHP >= 5.3 it can be done like this:
or