Solved

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

Posted on 2016-08-21
5
46 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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

Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
$_SERVER Variable question 31 56
A Request to Review a (hopefully) simple Wordpress "if-or-else" php code line. 5 48
$_GET call between URL 3 45
while read ID; do 4 59
Generating table dynamically is the most common issue faced by php developers.... So it seems there is a need of an article that explains the basic concept of generating tables dynamically. It just requires a basic knowledge of html and little maths…
These days socially coordinated efforts have turned into a critical requirement for enterprises.
The viewer will learn how to dynamically set the form action using jQuery.
The viewer will learn how to look for a specific file type in a local or remote server directory using PHP.

730 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