Initializing an array with a value

Hi guys, I encoded something like this .. I just don't know if the code is correct or not. But I have a question

The code

$featured = array('name' => 12,'yeah' => 10);
foreach($featured as $key => $value){
  echo $value['name'];
}

I know that the name value can be used by $ featured ['name'] but now I just need to know that the array key can be useful as the value of $ value ['name']. Is it possible?..

Any help would be appreciated. Thank.

+3
source share
3 answers
$featured = array('name' => 12,'yeah' => 10);
foreach($featured as $key => $value){
  echo $key; // outputs: name
  echo " - ";
  echo $value; // outputs: 12
  echo "<br />";
}

Yes, he supports this in the next iteration of the loop.

Conclusion:

name - 12
yeah - 10

BTW, another way to access key from an array.

$featured = array('name' => 12,'yeah' => 10);
while (current($featured)) {
  echo key($featured).'<br />';
  next($featured);
}

Conclusion:

name
yeah
+2
source

You most likely want to do:

echo "{$key} => {$value}";

foreach($featured as $key => $value) array $key $value , .

+1

: http://php.net/array_search .

Not like accessing $ array ['value'], but it is still useful if you want to find the key.

0
source

All Articles