Reputation: 11
Table:
cartId cusid 2 3 3 3 2 4 3 4 2 5 3 5
$cartid = array(2,3);
$cusid = array(3,4,5);
Pls is there a way I can achieve the above table using foreach loop? I tried this
foreach($cartid as $index=>$cartid2){
foreach($cusid as $index=>$cusid2){
echo "$cartid2 ===== $cusid2";
}
}
But didn't work, its saying offset. Pls I need help. View the image for better understanding
Upvotes: 0
Views: 51
Reputation: 11
Nick thanks for your answer, it really worked for me, i appreciate so much. I had to use an if statement to get exactly what i wanted.
foreach($rider_id1 as $rider_id2){
foreach($cart_id1 as $cart_id2){
if($rider_id2!=""){
if($cart_id2!=""){
echo "$cart_id2 ====== $rider_id2<br>";
}
}
}
echo "<br>";
}
Thanks dude!
Upvotes: 0
Reputation: 147236
You have a couple of problems with your code. Firstly you are overwriting $index
in the inner loop (however, since $index
is not used, that's not a big deal). Secondly your loops are nested incorrectly, your outer loop should be on $cusid
and the inner loop on $cartid
. Try this:
$cartid = array(2,3);
$cusid = array(3,4,5);
echo "cartid\tcusid\n";
foreach($cusid as $cus){
foreach($cartid as $cart){
echo "$cart\t$cus\n";
}
echo "\n";
}
Output:
cartid cusid
2 3
3 3
2 4
3 4
2 5
3 5
Upvotes: 1