Mark Lalor
Mark Lalor

Reputation: 7887

PHP array keys values

I have an array like this

$data = array(
    "some"   => "163",
    "rand"  => "630",
    "om"    => "43",
    "words" => "924",
    "as"    => "4",
    "keys"  => "54"
);

How can I get each set's key associated with their values like this:

foreach ($data as $stuff){
  $this->$stuff["key"] = $stuff["value"];
}

Upvotes: 6

Views: 21618

Answers (2)

user142162
user142162

Reputation:

foreach($data as $key => $value)
{
    // $key and $value variable are available here
    // First iteration values would be: $key = "some" and $value = "163"
}

Upvotes: 5

Matthew
Matthew

Reputation: 48284

foreach ($data as $key => $value){
  echo "$key => $value\n";
}

Upvotes: 16

Related Questions