PHP array keys values
Asked Answered
F

2

6

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"];
}
Foreknowledge answered 15/9, 2010 at 23:1 Comment(1)
Very first code block on the very first Google result for "php foreach" yields exactly what you'd expect.Rider
B
16
foreach ($data as $key => $value){
  echo "$key => $value\n";
}
Belia answered 15/9, 2010 at 23:3 Comment(0)
B
5
foreach($data as $key => $value)
{
    // $key and $value variable are available here
    // First iteration values would be: $key = "some" and $value = "163"
}
Beneficent answered 15/9, 2010 at 23:3 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.