简体   繁体   English

PHP用于回显数组

[英]PHP for echos an array

I am having trouble trying to echo each iteration of a particular part of an array. 我在尝试回显数组特定部分的每次迭代时遇到麻烦。

Here are the clues. 这是线索。 I can't quite piece them together (since I don't yet know much about php arrays). 我不能将它们拼凑在一起(因为我对php数组了解得还不多)。

If I do the following: 如果我执行以下操作:

<?php
$audiofile = simple_fields_value("audiofile");
echo $audiofile['url'];
?>

I get the URL of the file, which is what I want. 我得到文件的URL,这就是我想要的。 However, I have four different URL's (A "two-dimensional array", I believe?), and I want to echo each of them. 但是,我有四个不同的URL(我相信是“二维数组”),并且我想回显每个URL。

According to the Simple Fields documentation, I know that I need to change the second line to: 根据“简单字段”文档,我知道我需要将第二行更改为:

$audiofile = simple_fields_values("audiofile");

This leads me to change the php as follows: 这导致我按如下方式更改php:

<?php
$audiofile = simple_fields_values("audiofile");
for($i=0;$i<count($audiofile);$i++)
{
echo $audiofile[$i];
}
?>

But this only echoes "ArrayArrayArrayArray". 但这仅回显“ ArrayArrayArrayArray”。

Which makes sense, because $audiofile is returning an array of info, FROM WHICH I only want the ['url']. 这是有道理的,因为$ audiofile返回一个信息数组,因此我只需要['url']。

So, I tried the following: 因此,我尝试了以下方法:

<?php
$audiofile = simple_fields_values("audiofile");
for($i=0;$i<count($audiofile);$i++)
{
echo $audiofile['url'][$i];
}
?>

But that echoes null. 但这回响为空。

Any ideas? 有任何想法吗?

I think you may have your array key order backwards is all. 我认为您可能会将阵列键顺序向后退了。 Have you tried flipping ['url'][$i] into [$i]['url']? 您是否尝试过将['url'] [$ i]翻转为[$ i] ['url']? Like this: 像这样:

<?php
$audiofile = simple_fields_values("audiofile");
for($i=0;$i<count($audiofile);$i++) {
    echo $audiofile[$i]['url'];
}
?>

You can either do: 您可以执行以下操作:

<?php
$audiofile = simple_fields_values("audiofile");
for($i=0;$i<count($audiofile);$i++)
{
    print_r($audiofile[$i]);
}
?>

or do a var_dump 或者做一个var_dump

<?php
$audiofile = simple_fields_values("audiofile");
for($i=0;$i<count($audiofile);$i++)
{
    var_dump($audiofile[$i]);
}
?>

Why not with a foreach construct? 为什么不使用foreach构造?

$audiofile = simple_fields_values("audiofile");
foreach ($audiofile as $key => $value) {
    echo $audiofile[$key]['url'];
}

It's more handful than a for cycle, basically you haven't to count items and increment the index by yourself because foreach has been built ad hoc to iterate over php arrays. 它比for循环好用,基本上您不必自己计算项目并自己增加索引,因为foreach是临时构建的,可以迭代php数组。

http://php.net/manual/en/control-structures.foreach.php http://php.net/manual/en/control-structures.foreach.php

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM