Md. Hasan Mahmud
Md. Hasan Mahmud

Reputation: 197

how can i print a specific portion of array in php?

I am trying to print a number from an array. But when i do this:

echo $results[0][0];

i get error.I tried to print the whole array using print_r() function

echo print_r($results);

Then i get this result:

Array ( [0] => stdClass Object ( [lastOrderProcessedNumber] => 109089875875875 ) ) 1

I just need to print "109089875875875" this number

How can i do that? Thank you in advance

Upvotes: 0

Views: 118

Answers (3)

gioramies
gioramies

Reputation: 26

If you see $results is an array of objects, that means that $results[0] is an object, not an array, so you can't access its attributes as an array but instead as an object. Like this:

$results[0]->lastOrderProcessedNumber;

Upvotes: 1

Reece
Reece

Reputation: 550

As you can see from the print_r result, the indexed result is an object:

// $results is an 'Array' (access with square brackets)
Array
(
  // Index 0 is an Object (access with arrow operator)
  [0] => stdClass Object
  (
    [lastOrderProcessedNumber] => 109089875875875
  )
)

This means you have to access the property through the arrow operator, like so:

$results[0]->lastOrderProcessedNumber

If you're expecting to only have one result, or to grab the first result from $results, you can make use of reset:

reset($results)->lastOrderProcessedNumber

Upvotes: 1

Everett
Everett

Reputation: 9568

print_r() is a great way to inspect the contents of a variable. What it is showing you is that your variable holds an array whose first element (at index 0) is an object with an lastOrderProcessedNumber attribute. In PHP, you use -> to access object properties, so you should be able to retrieve the 109089875875875 value like this:

$results[0]->lastOrderProcessedNumber

Upvotes: 2

Related Questions