PHP foreach loop through multidimensional array

前端 未结 4 589
死守一世寂寞
死守一世寂寞 2020-11-27 04:39

I have an array:

$arr_nav = array( array( \"id\" => \"apple\", 
          \"url\" => \"apple.html\",
          \"name\" => \"My Apple\" 
        ),
         


        
4条回答
  •  醉酒成梦
    2020-11-27 04:55

    If you mean the first and last entry of the array when talking about a.first and a.last, it goes like this:

    foreach ($arr_nav as $inner_array) {
        echo reset($inner_array); //apple, orange, pear
        echo end($inner_array); //My Apple, View All Oranges, A Pear
    }
    

    arrays in PHP have an internal pointer which you can manipulate with reset, next, end. Retrieving keys/values works with key and current, but using each might be better in many cases..

提交回复
热议问题