Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

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

Posted on 2016-08-21
5
Medium Priority
?
60 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 84

Accepted Solution

by:
Dave Baldwin earned 2000 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 84

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 84

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

How to Use the Help Bell

Need to boost the visibility of your question for solutions? Use the Experts Exchange Help Bell to confirm priority levels and contact subject-matter experts for question attention.  Check out this how-to article for more information.

Question has a verified solution.

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

Developers of all skill levels should learn to use current best practices when developing websites. However many developers, new and old, fall into the trap of using deprecated features because this is what so many tutorials and books tell them to u…
Introduction This article is intended for those who are new to PHP error handling (https://www.experts-exchange.com/articles/11769/And-by-the-way-I-am-New-to-PHP.html).  It addresses one of the most common problems that plague beginning PHP develop…
The viewer will learn how to count occurrences of each item in an array.
The viewer will learn how to create and use a small PHP class to apply a watermark to an image. This video shows the viewer the setup for the PHP watermark as well as important coding language. Continue to Part 2 to learn the core code used in creat…
Suggested Courses

876 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