Reputation: 301
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!
Upvotes: 0
Views: 410
Reputation: 9215
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];
}
Upvotes: 1
Reputation: 68476
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];
}
?>
Upvotes: 3
Reputation: 324
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>";
}
Upvotes: 0
Reputation: 2438
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];
}
?>
Upvotes: 1