0

I have the following two foreach loops that are working for me:

foreach ( $pa_speciesreactivityabbrs as $pa_speciesreactivityabbr ) {
echo '<span>' .$pa_speciesreactivityabbr->name. '</span>';
}

foreach ( $pa_speciesreactivitys as $pa_speciesreactivity ) {
echo '<span>' .$pa_speciesreactivity->name. '</span>';
}

However I need to combine the $pa_speciesreactivityabbrs and $pa_speciesreactivitys in the same loop to output $pa_speciesreactivityabbr->name and $pa_speciesreactivity->name together.

I have gone through a number SO answers from other posts, but can't seem to apply them to my situation.

2
  • At the time of putting your values in the object why don't you use single one. in an object any number of data can store of any format? that's only the solution. Other wise you have to use two foreach loop. Commented May 8, 2015 at 20:27
  • I'm using WordPress and both data sets are coming from separate get_the_terms , so I am not sure if, or how I would combine the two. Commented May 8, 2015 at 20:37

1 Answer 1

2

Looks like your objects are in normal arrays. Assuming you don't use associative arrays, you could do this easily by looping through one of them and output data from both at the same time. Like this:

foreach ($array1 as $index => $obj1) {
    echo '<span>' . $obj1->name . $array2[$index]->name . '</span>';
}

I shortened the array names to make it more readable, but I'm sure you see how it works.

Sign up to request clarification or add additional context in comments.

1 Comment

Thanks for your help. You suggestions is similar to something I had tried previously, but didn't seem to work for me. This is my version of your example code foreach ($pa_speciesreactivityabbrs as $index => $obj1) { echo '<span>' . $obj1->name . $pa_speciesreactivitys[$index]->name . '</span>'; }

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.