Solved

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

Posted on 2016-08-21
5
30 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 82

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 82

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 82

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

Enabling OSINT in Activity Based Intelligence

Activity based intelligence (ABI) requires access to all available sources of data. Recorded Future allows analysts to observe structured data on the open, deep, and dark web.

Join & Write a Comment

This article will explain how to display the first page of your Microsoft Word documents (e.g. .doc, .docx, etc...) as images in a web page programatically. I have scoured the web on a way to do this unsuccessfully. The goal is to produce something …
These days socially coordinated efforts have turned into a critical requirement for enterprises.
Learn how to match and substitute tagged data using PHP regular expressions. Demonstrated on Windows 7, but also applies to other operating systems. Demonstrated technique applies to PHP (all versions) and Firefox, but very similar techniques will w…
The viewer will learn how to create a basic form using some HTML5 and PHP for later processing. Set up your basic HTML file. Open your form tag and set the method and action attributes.: (CODE) Set up your first few inputs one for the name and …

744 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

Need Help in Real-Time?

Connect with top rated Experts

11 Experts available now in Live!

Get 1:1 Help Now