1

I am trying to retrieve the last names of the orders placed.

code:

for ($x = 0; $x < 25; ++$x) {
 if (($orders[$x]['paymentStatus'] == 'paid') || ($orders[$x]['paymentIsPost'] == TRUE)) {
 echo '<pre>';
 print_r($orders[$x]['lastname']);
 echo '</pre>';
 }
}

question: code is not showing me the first lastname in the array actually the name that is in position $orders[0]. I thought that pre-incrementing the variable like this ++$x like this would solve the problem. To bad thats not the case. Any tips?

Update output:

[1]""
[2]""
[3]""
[4]""
[5]""

Not sharing lastnames, which is also not relevant.

asked Feb 23, 2016 at 14:58
9
  • maybe you should try $x++ Commented Feb 23, 2016 at 15:02
  • @Santa'shelper that's what i had before, same issue. Commented Feb 23, 2016 at 15:03
  • That's your answer stackoverflow.com/questions/1921421/… Commented Feb 23, 2016 at 15:05
  • 1
    Can you please provide an example output of var_export($orders);? Commented Feb 23, 2016 at 15:05
  • 1
    then u can try foreach instead of for, with foreach it will guarantee that loop the every element of the array Commented Feb 23, 2016 at 15:06

1 Answer 1

1

try the following to confirm which element of the array you are displaying (and the value of $x

 for($x = 0; $x < 25 ; $x++){
 if(($orders[$x]['paymentStatus'] == 'paid') || ($orders[$x]['paymentIsPost'] == true)){
 echo '<pre>';
 --print_r($orders[$x]['lastname']);
 echo '['.$x.']'.$orders[$x]['lastname'];
 echo '</pre>';
 }
 }
answered Feb 23, 2016 at 15:06
5
  • See the opcoming update to the post, it does not display [0] it starts at [1] Commented Feb 23, 2016 at 15:11
  • See VolkerK's request for an output of $orders just to make sure what data is in there as that could be the source of your problem Commented Feb 23, 2016 at 15:12
  • do a print_r($orders) prior to the for loop so we can see the data that is in there. I suspect that your 0 element isn't paid or paymentIsPost which is why it isn't displaying, or there isn't a 0 element Commented Feb 23, 2016 at 15:15
  • your code gave me insight in the solution thank you. Commented Feb 23, 2016 at 15:20
  • so it was $x++ instead of ++$x or another problem? Commented Feb 23, 2016 at 15:49

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

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.