0

Hello I am trying to print a specific value from an array of objects. I am trying to get a value from an array name $allPhotos with a object's property of "nme"'s value.

This is what im trying: echo $allPhotos[0]["nme"];

this is what the array looks like:

var_dump($allPhotos);

array(2) {
  [0]=> object(Photo)#1 (10) { 
    ["product"]=> array(5) { 
      ["PKG1"]=> string(4) "6500" 
      ["PKG2"]=> string(4) "9500" 
      ["8x10"]=> string(4) "1500" 
      ["5x7"]=> string(3) "750" 
      ["4x6"]=> string(3) "300" 
    } 
    ["price"]=> NULL ["sku"]=> string(1) "1" 
    ["nme"]=> string(5) "test1" 
    ["dir"]=> string(51) "http://" 
    ["status"]=> string(1) "1" ["gallery"]=> string(16) "Church Directory"    
    ["galleryCover"]=> string(1) "0" 
    ["family"]=> string(0) "" 
    ["familyCover"]=> string(0) "" 
} 

[1]=> object(Photo)#2 (10) { 
  ["product"]=> array(5) { 
    ["PKG1"]=> string(4) "6500" 
    ["PKG2"]=> string(4) "9500" 
    ["8x10"]=> string(4) "1500" 
    ["5x7"]=> string(3) "750" 
    ["4x6"]=> string(3) "300" 
  } 
  ["price"]=> NULL 
  ["sku"]=> string(1) "2" 
  ["nme"]=> string(5) "test2" 
  ["dir"]=> string(51) "http://" 
  ["status"]=> string(1) "1" 
  ["gallery"]=> string(16) "Church Directory" 
  ["galleryCover"]=> string(1) "0" 
  ["family"]=> string(0) "" 
  ["familyCover"]=> string(0) "" 
 } 
}

Thanks in advance!

1
  • Have you tried echo $allPhotos[0]["nme"]; ? Commented Oct 29, 2011 at 5:24

1 Answer 1

4

I believe echo $allPhotos[0]->nme; should work.

Sign up to request clarification or add additional context in comments.

3 Comments

I believe so too. Have no experience with this sort of array indexing as I haven't needed it yet, though a simple Google search got me the same answer. Might also be a duplicate of this: link
Yep that's it, Thanks a lot. Just wondering why does $allPhotos[0]["nme"]; not work? is it because it is still in an object in the array?
The array is an array of objects. The object $allPhotos[0] is not an array, and so you have to use -> to access its properties. Think of it this way: $n = $allPhotos[0]; echo $n->nme;

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.