2

I was just wondering if there is a way to display this one after the other rather than first amounts than dates.

$apaidString = $apaid;
$daterString = $dater;
$apaidArray = explode(',', $apaidString);
$daterArray = explode(',', $daterString);

<?php foreach($apaidArray as $apaid_Array){
echo trim($apaid_Array,",").'<br>';}?>
<?php foreach($daterArray as $dater_Array){
echo trim($dater_Array,",").'<br>';}?>

This code will display:

50
60
10
11-11-2016
12-11-2016
14-11-2016

What i would need would be:

50 11-11-2016
60 12-11-2016
10 14-11-2016

I get the values from a MySQL.

2
  • 2
    Why playing ping pong with the variables? Commented Nov 14, 2016 at 16:51
  • @Cluster you need only one loop to do this.Its same as triangle pattern logic. Commented Nov 15, 2016 at 9:50

2 Answers 2

1

Walk both arrays at the same time :

<?php
$apaidString = "50,60,10";
$daterString = "11-11-2016,12-11-2016,14-11-2016";
$apaidArray = explode(',', $apaidString);
$daterArray = explode(',', $daterString);

for ( $i = 0; $i < count($apaidArray); $i++ )
{ echo $apaidArray[$i].'<br>';
  echo $daterArray[$i].'<br>';
}
?>

The result is:

50
11-11-2016
60
12-11-2016
10
14-11-2016
Sign up to request clarification or add additional context in comments.

5 Comments

You don't want count() within the loop ;)
Why count() should not be within the loop?
@Cluster, did you test my answer? It works great, it is what you need, just look at the result.
@bub, would you explain your comment? Cluster won't accept my answer because of your comment (and my answer works perfectly).
@JoseManuelAbarcaRodríguez oh your answer is fine, I just mentioned the count in the loop(). Do something like $count = count(...)
1

i think you can do like this but i'm not sure that is the better way

foreach($apaidArray as $key_array => $apaid_Array)
{
    echo trim($apaid_Array,",").' '.trim($daterArray[$key_array],",") .'</br>';
}

output:

50 11-11-2016
60 12-11-2016
10 14-11-2016

instead of

foreach($apaidArray as $apaid_Array){
echo trim($apaid_Array,",").'<br>';}
foreach($daterArray as $dater_Array){
echo trim($dater_Array,",").'<br>';}

and you can do directly

$apaidArray = explode(',', $apaid);
$daterArray = explode(',', $dater);

instead of

$apaidString = $apaid;
$daterString = $dater;
$apaidArray = explode(',', $apaidString);
$daterArray = explode(',', $daterString);

1 Comment

I got that before posting but still no joy.

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.