0

foreach doesn't fetch end of array. I want to use end of that in another method:

$array = array(
"Language programings"=>array("php" => 100,"js" => 200),'html'=>12);
 foreach ($array as $kk=>$val1)
   {
     echo $kk.'<br/>';
     foreach ($val1 as $key=>$val2)
      {  
         if (! end(array_keys($array)))
             echo $val2;
      }
      echo end(array_value);//must be show 12
   }
2
  • 1
    what you actually want to do?? Commented Sep 5, 2012 at 5:48
  • foreach will always iterate over all of the elements of an array, including the last. that's why it's called forEACH. try a regular for loop instead. Commented Sep 5, 2012 at 5:49

2 Answers 2

1

If I understand correctly, in your if() statement, you're attempting to see if the pointer is at the end of the array. Unfortunately, in this case, end() will never be false and so the line echo $val2; won't ever execute.

Try replacing

if (! end(array_keys($array)))

with

if ($key <> end(array_keys($array))

also your last line should be:

echo end(array_values($array));
Sign up to request clarification or add additional context in comments.

Comments

0

Try using below code:

$array = array("Language programings"=>array("php" => 100,"js" => 200),'html'=>12);

foreach($array as $key1 => $val1){
    if(is_array($val1)){
        echo $key1.'<br/>';
        foreach($val1 as $key2 => $val2){
            if(is_array($val2)){
                foreach($val2 as $k => $v){
                    echo $v.'<br/>';
                }
            } else {
                echo $val2.'<br/>';
            }
        }
    }
}
echo end(array_values($array));  

The result will be:

Language programings  
100  
200  
12

1 Comment

thanks, in result of this code html is additional. and not showing

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.