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.

izk
  • 234
  • 6
  • 19
  • maybe you should try `$x++` – Santa's helper Feb 23 '16 at 15:02
  • @Santa'shelper that's what i had before, same issue. – izk Feb 23 '16 at 15:03
  • That's your answer http://stackoverflow.com/questions/1921421/get-the-first-element-of-an-array – Onur Feb 23 '16 at 15:05
  • 1
    Can you please provide an example output of `var_export($orders);`? – VolkerK Feb 23 '16 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 – Santa's helper Feb 23 '16 at 15:06
  • @Onur not completely because it gives me a partial solution which i already know that i can use array_values etc. I know how to retrieve the first element . I want to retrieve the first element with the for loop. – izk Feb 23 '16 at 15:07
  • @ash at the moment I am more comfortable with for() because thats how I started off, is it bad practice to still use for() loop? – izk Feb 23 '16 at 15:21
  • if you know all your potential indexes are sequential, then `for()` is fine, I believe his recommendation is because just cycling through an array that may be generated form a variety of things where the indexes may not be sequential, it is more common (and arguably flexible) to loop through with `foreach()`. – Dave Feb 23 '16 at 15:24
  • @Dave Thank you for the help and well explained details. Hope my question was also clear. Trying to write my questions better and better. – izk Feb 23 '16 at 15:30

1 Answers1

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>';
      }
 }
Dave
  • 991
  • 1
  • 7
  • 15
  • See the opcoming update to the post, it does not display [0] it starts at [1] – izk Feb 23 '16 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 – Dave Feb 23 '16 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 – Dave Feb 23 '16 at 15:15
  • your code gave me insight in the solution thank you. – izk Feb 23 '16 at 15:20
  • so it was `$x++` instead of `++$x` or another problem? – Santa's helper Feb 23 '16 at 15:49