0

In essence, I have 3 arrays. These are serialised, stored to the DB, un-serialised and then outputted to a page. myFirstArray, mySecondArray, & myThirdArray.

From what I gather - I need to be using a foreach loop, or a for loop with a counter, as the 3 arrays are all of (the same) unknown length. By that I mean one user may have stored 4 items into each of the 3 arrays, but another user might have stored 8 items into each of the 3 arrays.

I'm trying to get the output to look something like this:

myFirstArray[0], mySecondArray[0], myThirdArray[0]
myFirstArray[1], mySecondArray[1], myThirdArray[1]
myFirstArray[2], mySecondArray[2], myThirdArray[2]

The current code I have is as follows:

foreach ($myFirstArray as $value1){
    echo $value1 . " ";
}
foreach ($mySecondArray as $value2){
    echo $value2 . " ";
}
foreach ($myThirdArray as $value3){
    echo $value3 . "<br>";
} 

I am aware that this code is never going to output my arrays as I would like, but I'm having some difficulty with working out the logic behind what I need. I haven't rushed straight to StackOverflow to ask, but nothing else I've seen has been very helpful!

1

1 Answer 1

1

Since both arrays have the same length, I propose

$length = count($myFirstArray);
for($i = 0; $i <$length ; $i++) {
    echo $myFirstArray[$i].','.$mySecondArray[$i].','.$myThirdArray[$i].'<br/>';
}

This will loop through all of your arrays at the same time :) .

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

2 Comments

I've tried this code, but it doesn't appear to output anything.
@SixTailedFox, sorry I missed the count function, just edited it. I'm too used to OO programming :)

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.