get all values from arraylist in php

时间:2015-10-29 15:46:48

标签: php

I have arraylist like : [1, 2, 3, 4, 5]; How can I get each of these values in php? Is there any function ? I tried array() but it did not return anything.I really appreciate any help.

2 个答案:

答案 0 :(得分:7)

Let say your $array = [1,2,3,4,5];

To get the values:

foreach($array as $values):
      echo $values.'<br/>';
endforeach;

Or if you want based on elements, you can get by this way:

//To get value 1
echo $array[0];
// 0 being here the index of your array
// so echo $array[0] will output 1 since it's the first key of your array

答案 1 :(得分:0)

如果您有这样的数组

$numbers = array(1,2,3,4,5);

有五个值(即索引为0 - 4),您可以使用print_r()函数打印出数组中的所有值,如下所示:

print_r($numbers);

或者您可以使用任何PHP循环语句(如FOR STATEMENTS,FOREACH STATEMENTS,WHILE STATEMENTS)循环遍历数组中的每个元素。 e.g

foreach($numbers as $num){
echo $num;
echo '<br/>';
}

您也可以使用索引从数组中获取单个值,例如,如果我想打印数字&#39; 3&#39;我会写

echo $numbers[2];