Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

PHP create key => value pairs within a foreach

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
like image 540
BobFlemming Avatar asked May 10 '11 14:05

BobFlemming


1 Answers

Something like this?

foreach ($Offer as $key => $value) { 
  $offerArray[$key] = $value[4];
}
like image 100
Emil Vikström Avatar answered Oct 15 '22 00:10

Emil Vikström