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
Solved

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

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

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering 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
Read a remote csv file from a https URL 8 44
MVC - procedural PHP 10 34
Is there a way to bind the ID to the text value of the search result? 28 51
PHP Mail error 3 26
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 …
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 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…
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 …

790 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