Alternative to foreach() PHP?

11,486

Solution 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"
        }
    }
} 

Solution 2

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.

Solution 3

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'];
Share:
11,486
sqlmole
Author by

sqlmole

Updated on June 04, 2022

Comments

  • sqlmole
    sqlmole almost 2 years

    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" } } } 
    

    Thanks

  • sqlmole
    sqlmole over 12 years
    what does the ['0'] mean at the end? is that its position in the array? what is the array identifier
  • 65Fbef05
    65Fbef05 over 12 years
    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.
  • sqlmole
    sqlmole over 12 years
    Is this the same as arrays are made in C++ it just seems slightly different.
  • 65Fbef05
    65Fbef05 over 12 years
    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.
  • sqlmole
    sqlmole over 12 years
    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?
  • 65Fbef05
    65Fbef05 over 12 years
    This is exactly like Inception. :)