0

I'm working on a WordPress site and am using a plugin called 'Simple Fields', to add data to repeatable fields. I've entered data into three fields: (audioinfo, audiofile, audiocomposer).

I would now like to create a post that displays that information in the following way:

Audio Info 1

Audio File 1

Audio Composer 1

~~~

Audio Info 2

Audio File 2

Audio Composer 2

I've been trying to figure this out with foreach. Here's the closest I can arrive at (thought I know it's invalid). Can anyone make any suggestions of how to handle this?

<?php 
$audioinfo = simple_fields_values("audioinfo");
$audiofile = simple_fields_values("audiofile");
$audiocomposer = simple_fields_values("audiocomposer") 
foreach ($audioinfo as $audioinfos, $audiofile as $audiofiles, $audiocomposer as $audiocomposers){
echo $audioinfos;
echo $audiofile;
echo $audiocomposer;
}
?>

(Just as a side note, in case it's important, the first three lines appear to all be valid - that is, if I do a "foreach" on $audiocomposer alone, I successfully get: Bach Handel Beethoven.)

Is there something I can do to apply "foreach" to all three at the same time?

Thanks!

4 Answers 4

3

Assuming all the arrays of same length, making use of a normal for loop.

<?php 
$audioinfo = simple_fields_values("audioinfo");
$audiofile = simple_fields_values("audiofile");
$audiocomposer = simple_fields_values("audiocomposer");

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

3 Comments

Thanks for the quick reply. Yes indeed, all arrays will always be the same length. I like your nice and simple code. However, when I use it, I get a blank return. No parse error or anything like that. It's just the div where I've placed the php code is rendered empty. Have you any suggestions? Is there a different syntax for the for($i=0;$i<count($audioinfo);$i++) that I could try? Is more info about the plugin necessary? Thanks.
@Chris, Sorry it should be $i. I have edited the answer. Can you try again ?
Just for reference, in case any other Simple Fields user stumbles upon this: Shankar is totally right! This is a perfect answer to this question, in general. However, when I asked the question, I didn't know that Simple Fields uses more complex Arrays than I'd thought. So, while it hasn't solved my particular situation, it is the winner.
1

One more pretty solution:

<?php 
$audioinfo = simple_fields_values("audioinfo");
$audiofile = simple_fields_values("audiofile");
$audiocomposer = simple_fields_values("audiocomposer");

foreach ($audioinfo as $k => $val){
  echo $val;
  echo $audiofile[$k];
  echo $audiocomposer[$k];
}
?>

Comments

1

Assuming there are always going to be the same number of items in all 3 arrays, you could do something like this:

$items = count($audioinfo);
for ($i = 0; $i < $items; $i++)
{
    echo $audioinfo[$i];
    echo $audiofile[$i];
    echo $audiocomposer[$i];
}

5 Comments

Hi Mark, and thanks for the quick reply! This looks beautifully eloquent. I've plugged it in as follows: <?php $audioinfo = simple_fields_values("audioinfo"); $audiofile = simple_fields_values("audiofile"); $audiocomposer = simple_fields_values("audiocomposer"); $items = count($audioinfo); for ($i = 0; $ < $items; $i++) { echo $audioinfo[$i]; echo $audiofile[$i]; echo $audiocomposer[$i]; } ?> However, this returns the following error upon viewing the page: unexpected '<', expecting T_VARIABLE or '$' Hmm... Do you need more info about how the plugin works?
Sorry, there was a typo in the for... line. Should be $i < $items - have updated the post.
@Mark - Thanks! There must be another piece to this puzzle, because all iterations of echo $audiocomposer[$i] are being displayed at the top of all other output as such: "ArrayBach ArrayMozart ArrayRachmaninov" Incidentally, the same thing happens when I try to implement Yaroslav's solution. Do you have any further suggestions? NB - I found a way to return all field group data. I've added it to the bottom of my original question. Does this info help?
@Chris if you are presented with something like 'blablaArraybla' as the result of and echo then you surely have one of the components an array instead of a string, and my bet is that $audioinfo[$i] or $audiofile[$i]... it seems the composers are well enough.
@khael Thanks for this. You're right, of course. It's $audiofile that's acting up. I've discovered that the "audiofile" field returns an array of info. So, to echo the URL of an item in the "audiofile" field, I need to use: echo $audiofile['url'] Knowing this, I tried to change the above for statement to this: $items = count($audioinfo); for ($i = 0; $i < $items; $i++) { echo $audioinfo[$i]; echo $audiofile['url'][$i]; echo $audiocomposer[$i]; } But no such luck. Is there a way to request iteration $i of the 'url' part of the "audiofile" array?
0

here i gave one example how to handle the 3 array value in single foreach , hope it will much help to you

$audioinfo =array('popsong','rocksong','molodysong');
  $audiofile=array('jackson','rahman','example');
  $audiocomposer =array('first','second','third');

  foreach($audioinfo as $key => $value)
   {
    echo $value."</br>";
    echo $audiofile[$key]."</br>";
    echo $audiocomposer[$key]."</br>";

   }

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.