PHP foreach inside loop on array that added inside the loop

i am php newbie,i practice on arrays.

I created a foreach on some string that i converted to array,then inside the foreach i added more 2 arrays one before the end of the loop and one random.

here the code:

<?php
   //Some string to practice on
      $links = " http://hi.co.il<br>
        ??????  ht tp://hi.co.il<br>
   ????? http://hi.co.il<br>
   http://hi.co.il<br>
   http://hi.co.il<br>
   http://mega.co.il<br>
   http://hi.co.il";

   $links = explode('<br>',trim($links));


   $currenturl = array("http://mega.co.il","http://mega.co.il/","http://mega.co.il/#");

$i = 0;
$len = count($links);
$rand = rand(0,$len-1);

foreach($links as $key => $value) {
   $i++;//Practice counter

   $value = preg_replace('/s+/', '', $value);//Clean all whitespaces
   $links[$key] = strstr($value, 'http');//Cut the value from the left and return only the right.

    if(in_array($value,$currenturl)) {
        unset($links[$key]);
    }

    //Practice to my self

    if(is_array($value) == false){
       $links[$rand] = array("dfd","dfdf","dfdf");
    }

   //just a practice to add a array to a value before the end of the loop.
   if($i == $len-2){
       $links[$key] = array( array("rose", 1.25 , 15),
                             array("daisy", 0.75 , 25),
                             array("orchid", 1.15 , 7) 
                           ); 
   }


     //Not working , this is the main question how to foreach on value that is a array
     if(is_array($value)){
              foreach($value[$key] as $key => $val){
                  echo 'this is second foreach echo';
                   echo $val;
               }
         }

}

$links = array_filter($links);// Clean empty array values
$links = array_values($links);//Reindex array keys

   echo '<pre>';
   print_r($links);
   echo '</pre>';



   ?>

This output:

Array
(
    [0] => http://hi.co.il
    [1] => Array
        (
            [0] => dfd
            [1] => dfdf
            [2] => dfdf
        )

    [2] => http://hi.co.il
    [3] => http://hi.co.il
    [4] => Array
        (
            [0] => Array
                (
                    [0] => rose
                    [1] => 1.25
                    [2] => 15
                )

            [1] => Array
                (
                    [0] => daisy
                    [1] => 0.75
                    [2] => 25
                )

            [2] => Array
                (
                    [0] => orchid
                    [1] => 1.15
                    [2] => 7
                )

        )

    [5] => http://hi.co.il
)

The question why is_array($value) no working because its always return false even when a value is array? And how i foreach and the arrays i added inside the loop ? Its possible ?

Thanks .


is_array返回false,因为$ value是字符串,而不是数组。

链接地址: http://www.djcxy.com/p/53022.html

上一篇: foreach循环中current()的意外行为

下一篇: 在foreach循环内添加循环的PHP foreach