0

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).

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. However, I have four different URL's (A "two-dimensional array", I believe?), and I want to echo each of them.

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
$audiofile = simple_fields_values("audiofile");
for($i=0;$i<count($audiofile);$i++)
{
echo $audiofile[$i];
}
?>

But this only echoes "ArrayArrayArrayArray".

Which makes sense, because $audiofile is returning an array of info, FROM WHICH I only want the ['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?

4
  • Instead of echo use var_dump($audiofile[$i]); Commented Oct 4, 2013 at 17:05
  • Try echo $audiofile[$i]['url'] Commented Oct 4, 2013 at 17:06
  • You may look at php.net/manual/fr/class.recursivearrayiterator.php Commented Oct 4, 2013 at 17:06
  • var_dump(simple_fields_values("audiofile")) and it should be more clear for you ;) Commented Oct 4, 2013 at 17:08

3 Answers 3

5

I think you may have your array key order backwards is all. Have you tried flipping ['url'][$i] into [$i]['url']? Like this:

<?php
$audiofile = simple_fields_values("audiofile");
for($i=0;$i<count($audiofile);$i++) {
    echo $audiofile[$i]['url'];
}
?>
Sign up to request clarification or add additional context in comments.

1 Comment

There I go looking for zebras. This did it!! You rock!
0

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

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

Comments

0

Why not with a foreach construct?

$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.

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.