Display array values in PHP

前端 未结 9 2247
名媛妹妹
名媛妹妹 2020-12-01 07:51

So, I\'m working with PHP for the first time and I am trying to retrieve and display the values of an array. After a lot of googling, the only methods I can find for this ar

相关标签:
9条回答
  • <?php $data = array('a'=>'apple','b'=>'banana','c'=>'orange');?>
    <pre><?php print_r($data); ?></pre>
    

    Result:
    Array
    (
              [a] => apple
              [b] => banana
              [c] => orange
    )

    0 讨论(0)
  • 2020-12-01 07:53

    There is foreach loop in php. You have to traverse the array.

    foreach($array as $key => $value)
    {
      echo $key." has the value". $value;
    }
    

    If you simply want to add commas between values, consider using implode

    $string=implode(",",$array);
    echo $string;
    
    0 讨论(0)
  • 2020-12-01 07:55

    Other option:

    $lijst=array(6,4,7,2,1,8,9,5,0,3);
    for($i=0;$i<10;$i++){
    echo $lijst[$i];
    echo "<br>";
    }
    
    0 讨论(0)
  • 2020-12-01 07:59

    the join() function must work for you:

    $array = array('apple','banana','ananas');
    $string = join(',', $array);
    echo $string;
    

    Output :

    apple,banana,ananas

    0 讨论(0)
  • 2020-12-01 08:02

    Iterate over the array and do whatever you want with the individual values.

    foreach ($array as $key => $value) {
        echo $key . ' contains ' . $value . '<br/>';
    }
    
    0 讨论(0)
  • 2020-12-01 08:06

    use implode(',', $array); for output as apple,banana,orange

    Or

    foreach($array as $key => $value)
    {
       echo $key." is ". $value;
    }
    
    0 讨论(0)
提交回复
热议问题