1

This is interesting none the less, left me stumped. I have an array of 'order' objects called Workday, if I print_r the array I get the following;

Workday Object
(
    [workday] => Array
        (
            [0] => Order Object
                (
                    [id] => 3
                    [date] => 2013-08-02
                    [username] => userXYZ
                    [number] => 33333333 
                    [duration_start] => 20:51:00
                    [duration_end] => 21:51:00
                    [total] => $340.00
                )

            [1] => Order Object
                (
                    [id] => 3
                    [date] => 2013-08-03
                    [username] => userXXA
                    [number] => 87653212 
                    [duration_start] => 10:51:00
                    [duration_end] => 11:54:00
                    [total] => $20.00
                )

            [2] => Order Object
                (
                    [id] => 3
                    [date] => 2013-08-01
                    [username] => userXYD
                    [number] => 12345678 
                    [duration_start] => 08:37:00
                    [duration_end] => 10:01:00
                    [total] => $10.00
                )

        )

)

The issue seems to be when I try to iterate over the array it only outputs the first result. What am I missing?

$workday = new Workday();
$workday->Get($username, $duration_start, $duration_end);

$i = 0;
if($workday != null)
{
    foreach($workday as $orders)
    {
        echo "ID: " . $orders[$i]->number;
        $i++;
    }
}
3
  • Just tried, got Fatal error: Cannot use object of type Workday as array Commented Aug 5, 2013 at 4:41
  • Are you sure that is not your Get() method that returns only one result? Do you pass $username, $duration_start,$duration_end as search parameters because this might add up why you only receive one result. Commented Aug 5, 2013 at 4:46
  • 1
    @DaveChen your answer was correct, why did you delete it? Commented Aug 5, 2013 at 4:48

3 Answers 3

2

Try this

foreach($workday->workday as $orders)
    {
        echo "ID: " . $orders->number;
     }
Sign up to request clarification or add additional context in comments.

1 Comment

Bingo! Thank you... so is my array poorly designed or just a n00b dev oversight. :P
0

You need to iterate through the workday member variable of your object

if($workday != null)
{
    foreach($workday['workday'] as $orders)
    {
        echo "ID: " . $orders->number;
    }
}

Comments

0

The error you're getting is telling what's going on: you're trying to use an object as an array. Iterate over $workday->workday instead (which is the array inside your object).

foreach ($workday->workday as $orders) { ... }

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.