我正在学习PHP,并且在这一部分上陷入了困境。
我需要创建Array并通过For循环进行遍历,但是我被卡住了。我得到数组到字符串转换错误或空白页。
$array = array(
array(1, "FirstName1", "LastName1", "email1@gmail.com"),
array(2, "FirstName2", "LastName2", "email2@gmail.com"),
array(3, "FirstName3", "LastName3", "email3@gmail.com"),
);
这是我的阵列需要的外观。我可以用什么方式使for循环通过?
还有什么办法可以使像这样的字符串索引通过多维数组呢?
$array = array(
array("ID" => 1,"fname" => "name","lname" => "name","email" => "mail@gmail.com"),
array("ID" => 2,"fname" => "name","lname" => "name","email" => "mail@gmail.com"),
array("ID" => 3,"fname" => "name","lname" => "name","email" => "mail@gmail.com")
);
答案 0 :(得分:0)
因此$array
是一个关联数组的数组。要打印其中的所有值,请执行以下操作:
// Loop thorugh the $array
for($i=0;$i<count($array);$i++)
{
echo 'Printing '.$i.' row';
// Loop through each element of the $array
foreach($array[$i] as $key=>$value)
{
echo $key." = ".$value.PHP_EOL;
}
echo PHP_EOL;
}