0

Option 1:

$foo = array($obj1, $obj2, $obj3);
for ($i = 0; $i < 1; $i++) {
    echo $foo[$i]->attribute;
    echo $foo[$i]->attribute2;
}
//shows obj1's attribute and attribute2

Option 2:

$foo = array($obj1, $obj2, $obj3);
$first_foo = array_shift($foo); /* now we only have the first index in the new array */
foreach ($first_foo as $object) {
    echo $object->attribute;
    echo $object->attribute2;
}
//shows obj1's attribute and attribute2

Option 3:

$foo = array($obj1, $obj2, $obj3);
$first_foo = $foo[0] /* now we just have an object, obj1 */
echo $first_foo->attribute;
echo $first_foo->attribute2;
//shows obj1's attribute and attribute2

I used Option 3, but all of these feel kinda lacking... how would you do this? Is the loop in options 1 and 2 worth it if you feel like easily pulling the first two instead of one later on? I.e. pulling the latest news article vs. pulling the latest two etc.

wnajar
  • 747
  • 1
  • 7
  • 27

4 Answers4

2

Why so complicated?

Loops, array_shift(), ... it's all not neccessary.

You gave the solution yourself:

$foo[0]->attribute

Another one would be

reset($foo)->attribute

On your edit:

If you want to write the code so it is flexible later, do

$need = 1; // the variable number of elements you need
for($i = 0; $i < $need; $i++)
    echo $foo[$i]->attribute;
Lukas
  • 1,479
  • 8
  • 20
  • The extra complexity was because I was thinking it might be convenient if say, later on you wanted to pull 2 or 3 off the front. The extra complexity allows a little more robustness.. but at what cost? I edited the question a bit. Solid answer obviously. – wnajar May 10 '13 at 08:40
  • Added a simplistic `for` loop to fit your needs. – Lukas May 10 '13 at 08:43
  • I guess that's where the trouble is. Does this mean the `for` loop is really the best way to do it? I would have thought no... but Option 3, like the option you wrote seems somewhat 'hardcoded'. – wnajar May 10 '13 at 08:45
  • 1
    `array_shift()` on the other hand would return an array containing only one element (effectively useless), and has to be in a loop itself. `foreach` is a really complicated mechanism (AFAIK more complex than `while` and `for`) - see [here](http://stackoverflow.com/questions/10057671/how-foreach-actually-works). – Lukas May 10 '13 at 08:47
  • I think `for` literally is the best way to do it here.. would not have thought... – wnajar May 10 '13 at 08:53
1

The two iterations aren't needed in this case. I would just take your option #3 with a little less code.

$foo = array($obj1, $obj2, $obj3);
echo $foo[0]->attribute;
echo $foo[0]->attribute2;
Wurstbro
  • 974
  • 1
  • 9
  • 21
1

assuming:

$b = [0,1];
$c = [2,3];

let:

$a = [$b,$c];

$first_elem = array_shift($a);

print_r($first_elem);
jancha
  • 4,916
  • 1
  • 24
  • 39
0

This is to get more than 1 from array_shift which returns null after array is empty

$foo = array($obj1, $obj2, $obj3);
while($obj = array_shift($foo))
   echo $obj->attribute;

your option 2 can be changed to

$foo = array($obj1, $obj2, $obj3);
foreach ($foo as $object) {
    echo $object->attribute;
    echo $object->attribute2;
}

why don't use use it that way?

Robert
  • 19,800
  • 5
  • 55
  • 85