Loop through an array php

前端 未结 5 877
故里飘歌
故里飘歌 2020-11-22 17:13

I have this array... how do you print each of the filepath and filename? What is the best way to do this?

  Array ( 
    [0] => Array ( 
             [fid         


        
5条回答
  •  悲&欢浪女
    2020-11-22 17:49

    Using foreach loop without key

    foreach($array as $item) {
        echo $item['filename'];
        echo $item['filepath'];
    
        // to know what's in $item
        echo '
    '; var_dump($item);
    }
    

    Using foreach loop with key

    foreach($array as $i => $item) {
        echo $item[$i]['filename'];
        echo $item[$i]['filepath'];
    
        // $array[$i] is same as $item
    }
    

    Using for loop

    for ($i = 0; $i < count($array); $i++) {
        echo $array[$i]['filename'];
        echo $array[$i]['filepath'];
    }
    

    var_dump is a really useful function to get a snapshot of an array or object.

提交回复
热议问题