2

I am still very new to PHP and from all the examples that are around they all seem to use foreach statements.

e.g.

foreach ($variable as $row) 

However I don't think I should be using this all the time, for example variables or objects I have an which only has one row or instance in an array.

I know its advantageous to use them for multiple rows which could be missed if you used a for loop.

But do I really need to use it just to echo one variable thats in an array?

e.g. for example this variable $stats

array(3) { ["user_interventions"] => int(4) ["fastest_intervention"] => array(1) { [0] => object(stdClass)#22 (1) { ["duration"] => string(8) "02:10:00" } } ["slowest_intervention"] => array(1) { [0] => object(stdClass)#23 (1) { ["duration"] => string(8) "02:26:00" } } } 
3
  • like... $stats["user_interventions"] ? Commented Aug 3, 2011 at 19:39
  • I have never seen someone purposely use a foreach loop to print only one value from a known-structure variable. I think the premise of your question is incorrect. You do not need to use a loop, just echo $array['keyname'], echo $object->propertyname and such. Commented Aug 3, 2011 at 19:40
  • but sometimes if i dont use a foreach it says "trying to get property of non object" but if i use a foreach loop it fixes that Commented Aug 3, 2011 at 19:43

4 Answers 4

4

if you know the 'address' of the value in your array, then there's no need for a loop:

echo $arr['user_interventions'][0]['duration']; // 02:10:00 

More details here.

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

Comments

1

No, you do not need to use a foreach loop every time you need to access an array value. Your example could be used in the following manner...

echo $stats['fastest_intervention']['0']->duration; // Outputs: 02:10:00 

Here's your variable dump with indentation (makes it easier to read).

array(3) { ["user_interventions"]=> int(4) ["fastest_intervention"]=> array(1) { [0]=> object(stdClass)#22 (1) { ["duration"]=> string(8) "02:10:00" } } ["slowest_intervention"]=> array(1) { [0]=> object(stdClass)#23 (1) { ["duration"]=> string(8) "02:26:00" } } } 

6 Comments

what does the ['0'] mean at the end? is that its position in the array? what is the array identifier
Arrays are expressed in combinations of keys and values. durations is a property of the nested object 0, and 0 is the key assigned to that object nested inside of the array fastest_intervention.
Is this the same as arrays are made in C++ it just seems slightly different.
I don't know C++, but as I understand it, PHP, Java, and C++ handle variables, arrays and objects in very similar ways. Look at my most recent edit for an easier to read visualization of the parent child hierarchy. Perhaps it will help make heads or tails of the whole business.
Thankyou very much I can see the nesting now clearly. So its an array that contains variables and objects and some of those objects then have arrays in them. A bit like that movie Inception with a dream inside a dream haha?
|
1

You need not to use foreach here but you can't just print $array

if you indexes of array you may print something like:

print 'Key is '.$array['key'].' but index is only'.$array['index']; 

Comments

0

You just access the variables using []. print $array['key']

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.