1

I'm using a framework at work that I'm slightly unfamiliar with and trying to access elements of an object that are stored within an array called $items. I've tried die(print_r($items[0])) to try to get the first element but it says 0 is an undefined index. Here is the result of print_r($items):

Any help is much appreciated. If you have any questions I'll gladly answer because I know this is a bit vague. I think it would take up way too much space to explain how this framework actually works.

I figured out that the first element is 2 and not 0, but I'm still unable to access any of the elements within the object. When I tried print_r($items[2]->fields) it didn't return anything, just a blank page.

2
  • from the code it's like the key/index start at [2]. there was no zero Commented Jun 6, 2012 at 16:06
  • yeah you're right.. but I still don't know how to get any of the elements under fields Commented Jun 6, 2012 at 16:10

3 Answers 3

1

I think this is what you want:

$item = current($items);
foreach ($item->fields as $key => $val) {
  echo "$key => $val\n";
}

Update:

It seems like you cannot get $item->fields since it is a protected property of Dase_DBO_Project object:

[fields:protected] => Array
Sign up to request clarification or add additional context in comments.

Comments

1

I don't see any element with index 0 in your array, only keys 5, 4, 3 and 2. To get the first item from array use current($items) or reset($items).

Comments

1

Your array listed here does not have an index of 0 (For more help look here: http://php.net/manual/en/language.types.array.php) Rather than attempting to access each item with the index. Why don't you use a foreach?

foreach($items as $item)
{
  //Do what you want with each object here
  var_dump($item);
}

This will allow you to access each object without using the index. For more information using foreach take a look here: https://www.php.net/manual/en/control-structures.foreach.php

Cheers!

1 Comment

Yeah I was going to use that on another page, right now I'm just trying to pull a single element from the object within the array.

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.