3

Hello guys i have coded something like this ..I just dont know wheather the code is right or not ..But i have a question

THe code is

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

I know that value of name can be acessed by $featured['name'] but now I just need to know wheather the key of array can be acessed with value like $value['name']. Is it possbile like that ?..

Any help would be appreciated ..Thanks

1

3 Answers 3

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

Yes, it supports that in the next iteration of the loop.

Output:

name - 12
yeah - 10

BTW, one more way of accessing the keys from array.

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

Output:

name
yeah
Sign up to request clarification or add additional context in comments.

2 Comments

then how to execute yeah key and 10 value ?
will it support the both name=>12 and yeah=>10 according to ur code ?
1

You most probably want to do:

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

The foreach($featured as $key => $value) statement iterates the array and for each iteration $key and $value contain both the key and value for the tuple.

3 Comments

ma qus in not like that ..i just need to know wheather can we acess name key like $value['name'] ?
No, but you can access it as $featured['name'];
No, PHP does not support that.
0

Take a look at this: http://php.net/array_search it searches for the value and returns it's key.

It's not like accessing $array['value'] but it's still userfull if you want to find the key.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.