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.

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>';
      }
 }
Sign up to request clarification or add additional context in comments.

5 Comments

See the opcoming update to the post, it does not display [0] it starts at [1]
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
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
your code gave me insight in the solution thank you.
so it was $x++ instead of ++$x or another problem?

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.