-1

This seems simple enough, but my googling seems to be off to get the right answer:

I am dynamically making an array

$data = array();

$data['this is text'] = 1312;
$data['heres more text'] = 191;
$data['im also a unique entry'] = 239

I then want to go through each item and get the key and the value:

foreach($data as $datapoint)
{   
    var_dump($datapoint); //this seems to just be the value
    $k = key($garavg); //this does not work because datapoint is a value not an array
}

I simply want to be able to do this:

echo "$key has this many entries $value";
GregM
  • 3,624
  • 3
  • 35
  • 51
  • 2
    See the manual: http://php.net/manual/en/control-structures.foreach.php (Hint: It's `foreach ($data as $key => $value)`). – gen_Eric Sep 15 '15 at 19:38
  • you need to learn how to use Google better. This is purely basics. – CodeGodie Sep 15 '15 at 19:43
  • In case it helps for future googling, "dictionaries" are typically called _associative arrays_ in php. – HPierce Sep 15 '15 at 19:44
  • Acutally, `key` is a typo in your question as that is treated as a constant (even a function), rather than the intended `$key` variable. Plus, syntatically speaking; you're missing a semi-colon here `$data['im also a unique entry'] = 239`. *Just saying*, unless it's just a bad paste. – Funk Forty Niner Sep 15 '15 at 19:54

1 Answers1

2

foreach has that built in:

foreach($data as $key => $value )
{   
    echo "$key has this many entries $value";
}
Jesse Kernaghan
  • 4,544
  • 2
  • 18
  • 25