Solved

Stuck on iterating over an array 'while' a value is 'x'

Posted on 2016-08-21
5
43 Views
Last Modified: 2016-08-21
Hello,
I'm having trouble seeing how I could iterate through the object below.
I'm trying to echo out something like this crude example:

for each (object where ['department'] == 0){
echo open hours and closed hours
}

So essentially trying to put in a condition that must be met while iterating.
Array
(
    [0] => stdClass Object
        (
            [id] => 401
            [dealership_id] => ab633f68-a57b-f14b-59ba-55e4a2b00044
            [department] => 0
            [dow] => 1
            [open] => 09:00:00
            [closed] => 21:00:00
            [optional_text] => 
        )

    [1] => stdClass Object
        (
            [id] => 402
            [dealership_id] => ab633f68-a57b-f14b-59ba-55e4a2b00044
            [department] => 0
            [dow] => 2
            [open] => 09:00:00
            [closed] => 21:00:00
            [optional_text] => 
        )

    [2] => stdClass Object
        (
            [id] => 403
            [dealership_id] => ab633f68-a57b-f14b-59ba-55e4a2b00044
            [department] => 0
            [dow] => 3
            [open] => 09:00:00
            [closed] => 21:00:00
            [optional_text] => 
        )

    [3] => stdClass Object
        (
            [id] => 404
            [dealership_id] => ab633f68-a57b-f14b-59ba-55e4a2b00044
            [department] => 0
            [dow] => 4
            [open] => 09:00:00
            [closed] => 21:00:00
            [optional_text] => 
        )

    [4] => stdClass Object
        (
            [id] => 405
            [dealership_id] => ab633f68-a57b-f14b-59ba-55e4a2b00044
            [department] => 0
            [dow] => 5
            [open] => 09:00:00
            [closed] => 18:00:00
            [optional_text] => 
        )

    [5] => stdClass Object
        (
            [id] => 406
            [dealership_id] => ab633f68-a57b-f14b-59ba-55e4a2b00044
            [department] => 0
            [dow] => 6
            [open] => 09:00:00
            [closed] => 17:00:00
            [optional_text] => 
        )

    [6] => stdClass Object
        (
            [id] => 400
            [dealership_id] => ab633f68-a57b-f14b-59ba-55e4a2b00044
            [department] => 0
            [dow] => 7
            [open] => 00:00:00
            [closed] => 00:00:00
            [optional_text] => Closed
        )

    [7] => stdClass Object
        (
            [id] => 408
            [dealership_id] => ab633f68-a57b-f14b-59ba-55e4a2b00044
            [department] => 1
            [dow] => 1
            [open] => 09:00:00
            [closed] => 21:00:00
            [optional_text] => 
        )

    [8] => stdClass Object
        (
            [id] => 409
            [dealership_id] => ab633f68-a57b-f14b-59ba-55e4a2b00044
            [department] => 1
            [dow] => 2
            [open] => 09:00:00
            [closed] => 21:00:00
            [optional_text] => 
        )

    [9] => stdClass Object
        (
            [id] => 410
            [dealership_id] => ab633f68-a57b-f14b-59ba-55e4a2b00044
            [department] => 1
            [dow] => 3
            [open] => 09:00:00
            [closed] => 21:00:00
            [optional_text] => 
        )

    [10] => stdClass Object
        (
            [id] => 411
            [dealership_id] => ab633f68-a57b-f14b-59ba-55e4a2b00044
            [department] => 1
            [dow] => 4
            [open] => 09:00:00
            [closed] => 21:00:00
            [optional_text] => 
        )

    [11] => stdClass Object
        (
            [id] => 412
            [dealership_id] => ab633f68-a57b-f14b-59ba-55e4a2b00044
            [department] => 1
            [dow] => 5
            [open] => 09:00:00
            [closed] => 18:00:00
            [optional_text] => 
        )

    [12] => stdClass Object
        (
            [id] => 413
            [dealership_id] => ab633f68-a57b-f14b-59ba-55e4a2b00044
            [department] => 1
            [dow] => 6
            [open] => 09:00:00
            [closed] => 17:00:00
            [optional_text] => 
        )

    [13] => stdClass Object
        (
            [id] => 407
            [dealership_id] => ab633f68-a57b-f14b-59ba-55e4a2b00044
            [department] => 1
            [dow] => 7
            [open] => 00:00:00
            [closed] => 00:00:00
            [optional_text] => Closed
        )

    [14] => stdClass Object
        (
            [id] => 415
            [dealership_id] => ab633f68-a57b-f14b-59ba-55e4a2b00044
            [department] => 2
            [dow] => 1
            [open] => 09:00:00
            [closed] => 21:00:00
            [optional_text] => 
        )

    [15] => stdClass Object
        (
            [id] => 416
            [dealership_id] => ab633f68-a57b-f14b-59ba-55e4a2b00044
            [department] => 2
            [dow] => 2
            [open] => 09:00:00
            [closed] => 21:00:00
            [optional_text] => 
        )

    [16] => stdClass Object
        (
            [id] => 417
            [dealership_id] => ab633f68-a57b-f14b-59ba-55e4a2b00044
            [department] => 2
            [dow] => 3
            [open] => 09:00:00
            [closed] => 21:00:00
            [optional_text] => 
        )

    [17] => stdClass Object
        (
            [id] => 418
            [dealership_id] => ab633f68-a57b-f14b-59ba-55e4a2b00044
            [department] => 2
            [dow] => 4
            [open] => 09:00:00
            [closed] => 21:00:00
            [optional_text] => 
        )

    [18] => stdClass Object
        (
            [id] => 419
            [dealership_id] => ab633f68-a57b-f14b-59ba-55e4a2b00044
            [department] => 2
            [dow] => 5
            [open] => 09:00:00
            [closed] => 18:00:00
            [optional_text] => 
        )

    [19] => stdClass Object
        (
            [id] => 420
            [dealership_id] => ab633f68-a57b-f14b-59ba-55e4a2b00044
            [department] => 2
            [dow] => 6
            [open] => 09:00:00
            [closed] => 17:00:00
            [optional_text] => 
        )

    [20] => stdClass Object
        (
            [id] => 414
            [dealership_id] => ab633f68-a57b-f14b-59ba-55e4a2b00044
            [department] => 2
            [dow] => 7
            [open] => 00:00:00
            [closed] => 00:00:00
            [optional_text] => Closed
        )

)

Open in new window

0
Comment
Question by:tjyoung
  • 3
  • 2
5 Comments
 
LVL 83

Accepted Solution

by:
Dave Baldwin earned 500 total points
ID: 41764515
'foreach' is more simple minded than that.  You can't put a condition in the 'foreach' statement itself, you have to put it in the body of the 'foreach' code.

foreach ($arr as $value) {
    if($value['department'] == 0) {
        echo $value['open']." ".$value['closed']."<br />\n";
        }
}

Open in new window


http://php.net/manual/en/control-structures.foreach.php
0
 
LVL 1

Author Closing Comment

by:tjyoung
ID: 41764521
Thank you :)
0
 
LVL 83

Expert Comment

by:Dave Baldwin
ID: 41764530
You're welcome, glad to help.
0
 
LVL 1

Author Comment

by:tjyoung
ID: 41764536
HI, actually not sure that works.
Getting this:
"Cannot use object of type stdClass as array"

(array is called 'hours')

foreach ($hours as $value) {
    if($value['department'] == 0) {
        echo $value['open']." ".$value['closed']."<br />\n";
        }
}
0
 
LVL 83

Expert Comment

by:Dave Baldwin
ID: 41764578
'foreach' only works on arrays, not objects.  Supposedly you can convert an object to an array with this but I've never tried it:
// Cast to an array
$array = (array) $object;

Open in new window

0

Featured Post

Courses: Start Training Online With Pros, Today

Brush up on the basics or master the advanced techniques required to earn essential industry certifications, with Courses. Enroll in a course and start learning today. Training topics range from Android App Dev to the Xen Virtualization Platform.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Author Note: Since this E-E article was originally written, years ago, formal testing has come into common use in the world of PHP.  PHPUnit (http://en.wikipedia.org/wiki/PHPUnit) and similar technologies have enjoyed wide adoption, making it possib…
3 proven steps to speed up Magento powered sites. The article focus is on optimizing time to first byte (TTFB), full page caching and configuring server for optimal performance.
The viewer will learn how to count occurrences of each item in an array.
This tutorial will teach you the core code needed to finalize the addition of a watermark to your image. The viewer will use a small PHP class to learn and create a watermark.

776 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question