1

I am trying to organize a set of results that obtained through a database query. Here is the code I'm using to do this:

$time_slots = $wpdb->get_results($query);
print_r($time_slots); 
echo ("<br/><br/><br/><br/>");
/*organize slots into array*/


$openings = array(); 
foreach($time_slots as $ts)
{
    if(empty($openings))
    {
        echo("Empty Array: ");
        echo ("<br/>");
        echo ("<br/>");
        echo("Inserting: ");
        print_r($ts); 
        echo ("<br/>");
        echo ("<br/>");
        $openings[$ts->route_date] = $ts; 
        echo("contents of Opening: ");
        echo ("<br/>");
        echo ("<br/>");
        print_r($openings); 
        echo ("<br/><br/><br/><br/>");

    }
    elseif (array_key_exists($ts->route_date, $openings)) 
    {
        echo("Same Day");
        echo ("<br/>");
        echo ("<br/>");
        echo("Inserting: ");
        print_r($ts); 
        echo ("<br/>");
        echo ("<br/>");
        array_push($openings[$ts->route_date][$ts->name], $ts); 
        echo("contents of Opening: ");
        echo ("<br/>");
        echo ("<br/>");
        print_r($openings); 
        echo ("<br/><br/><br/><br/>");
    }
    else
    {
        echo("New Day : ");
        echo ("<br/>");
        echo ("<br/>");
        echo("Inserting: ");
        print_r($ts); 
        echo ("<br/>");
        echo ("<br/>");
        $openings[$ts->route_date] = $ts; 
        echo("contents of Opening: ");
        echo ("<br/>");
        echo ("<br/>");
        print_r($openings); 
        echo ("<br/><br/><br/><br/>"); 
    }
}

/*return results*/
$result['openings'] = $openings; 
$result['time'] = $time_slots;  
$result['begin'] = $begin; 
$result['end'] = $end; 
$result['query'] = $query; 
$result['type'] = "success"; 
$result = json_encode($result);
print_r($openings); 

Here is what a single result looks like when I print_r the $ts:

 stdClass Object ( [route_date] => 2014-01-10 [name] => 2 [openings] => 1 [appointments] => 0 ) 

Here is what one loop looks like. You will notice that the logic works, and that everything is going where it should go, but the adding t:

> Empty Array: 

Inserting: stdClass Object ( [route_date] => 2014-01-10 [name] => 0 [openings] => 1 [appointments] => 0 ) 

contents of Opening: 

Array ( [2014-01-10] => stdClass Object ( [route_date] => 2014-01-10 [name] => 0 [openings] => 1 [appointments] => 0 ) ) 



Same Day

Inserting: stdClass Object ( [route_date] => 2014-01-10 [name] => 1 [openings] => 1 [appointments] => 0 ) 

contents of Opening: 

Array ( [2014-01-10] => stdClass Object ( [route_date] => 2014-01-10 [name] => 0 [openings] => 1 [appointments] => 0 ) ) 



Same Day

Inserting: stdClass Object ( [route_date] => 2014-01-10 [name] => 2 [openings] => 1 [appointments] => 0 ) 

contents of Opening: 

Array ( [2014-01-10] => stdClass Object ( [route_date] => 2014-01-10 [name] => 0 [openings] => 1 [appointments] => 0 ) ) 



Same Day

Inserting: stdClass Object ( [route_date] => 2014-01-10 [name] => 3 [openings] => 1 [appointments] => 0 ) 

contents of Opening: 

Array ( [2014-01-10] => stdClass Object ( [route_date] => 2014-01-10 [name] => 0 [openings] => 1 [appointments] => 0 ) ) 



Same Day

Inserting: stdClass Object ( [route_date] => 2014-01-10 [name] => 4 [openings] => 1 [appointments] => 0 ) 

contents of Opening: 

Array ( [2014-01-10] => stdClass Object ( [route_date] => 2014-01-10 [name] => 0 [openings] => 1 [appointments] => 0 ) ) 



New Day : 

Inserting: stdClass Object ( [route_date] => 2014-01-11 [name] => 0 [openings] => 1 [appointments] => 0 ) 

contents of Opening: 

Array ( [2014-01-10] => stdClass Object ( [route_date] => 2014-01-10 [name] => 0 [openings] => 1 [appointments] => 0 ) [2014-01-11] => stdClass Object ( [route_date] => 2014-01-11 [name] => 0 [openings] => 1 [appointments] => 0 ) ) 



Same Day

Inserting: stdClass Object ( [route_date] => 2014-01-11 [name] => 1 [openings] => 1 [appointments] => 0 ) 

contents of Opening: 

Array ( [2014-01-10] => stdClass Object ( [route_date] => 2014-01-10 [name] => 0 [openings] => 1 [appointments] => 0 ) [2014-01-11] => stdClass Object ( [route_date] => 2014-01-11 [name] => 0 [openings] => 1 [appointments] => 0 ) ) 

When it is all said and done, I get this result:

Array ( [2014-01-10] => stdClass Object ( [route_date] => 2014-01-10 [name] => 0 [openings] => 1 [appointments] => 0 ) [2014-01-11] => stdClass Object ( [route_date] => 2014-01-11 [name] => 0 [openings] => 1 [appointments] => 0 ) [2014-01-12] => stdClass Object ( [route_date] => 2014-01-12 [name] => 0 [openings] => 1 [appointments] => 0 ) [2014-01-13] => stdClass Object ( [route_date] => 2014-01-13 [name] => 0 [openings] => 1 [appointments] => 0 ) [2014-01-14] => stdClass Object ( [route_date] => 2014-01-14 [name] => 0 [openings] => 1 [appointments] => 0 ) [2014-01-15] => stdClass Object ( [route_date] => 2014-01-15 [name] => 0 [openings] => 1 [appointments] => 0 ) [2014-01-16] => stdClass Object ( [route_date] => 2014-01-16 [name] => 0 [openings] => 1 [appointments] => 0 ) [2014-01-17] => stdClass Object ( [route_date] => 2014-01-17 [name] => 0 [openings] => 1 [appointments] => 0 ) [2014-01-18] => stdClass Object ( [route_date] => 2014-01-18 [name] => 0 [openings] => 1 [appointments] => 0 ) [2014-01-19] => stdClass Object ( [route_date] => 2014-01-19 [name] => 0 [openings] => 1 [appointments] => 0 ) [2014-01-20] => stdClass Object ( [route_date] => 2014-01-20 [name] => 0 [openings] => 1 [appointments] => 0 ) [2014-01-21] => stdClass Object ( [route_date] => 2014-01-21 [name] => 0 [openings] => 1 [appointments] => 0 ) [2014-01-22] => stdClass Object ( [route_date] => 2014-01-22 [name] => 0 [openings] => 1 [appointments] => 0 ) [2014-01-23] => stdClass Object ( [route_date] => 2014-01-23 [name] => 0 [openings] => 1 [appointments] => 0 ) [2014-01-24] => stdClass Object ( [route_date] => 2014-01-24 [name] => 0 [openings] => 1 [appointments] => 0 ) [2014-01-25] => stdClass Object ( [route_date] => 2014-01-25 [name] => 0 [openings] => 1 [appointments] => 0 ) [2014-01-26] => stdClass Object ( [route_date] => 2014-01-26 [name] => 0 [openings] => 1 [appointments] => 0 ) [2014-01-27] => stdClass Object ( [route_date] => 2014-01-27 [name] => 0 [openings] => 1 [appointments] => 0 ) [2014-01-28] => stdClass Object ( [route_date] => 2014-01-28 [name] => 0 [openings] => 1 [appointments] => 0 ) [2014-01-29] => stdClass Object ( [route_date] => 2014-01-29 [name] => 0 [openings] => 1 [appointments] => 0 ) [2014-01-30] => stdClass Object ( [route_date] => 2014-01-30 [name] => 0 [openings] => 1 [appointments] => 0 ) [2014-01-31] => stdClass Object ( [route_date] => 2014-01-31 [name] => 0 [openings] => 1 [appointments] => 0 ) [2014-02-01] => stdClass Object ( [route_date] => 2014-02-01 [name] => 0 [openings] => 1 [appointments] => 0 ) [2014-02-02] => stdClass Object ( [route_date] => 2014-02-02 [name] => 0 [openings] => 1 [appointments] => 0 ) [2014-02-03] => stdClass Object ( [route_date] => 2014-02-03 [name] => 0 [openings] => 1 [appointments] => 0 ) [2014-02-04] => stdClass Object ( [route_date] => 2014-02-04 [name] => 0 [openings] => 1 [appointments] => 0 ) [2014-02-05] => stdClass Object ( [route_date] => 2014-02-05 [name] => 0 [openings] => 1 [appointments] => 0 ) [2014-02-06] => stdClass Object ( [route_date] => 2014-02-06 [name] => 0 [openings] => 1 [appointments] => 0 ) [2014-02-07] => stdClass Object ( [route_date] => 2014-02-07 [name] => 0 [openings] => 1 [appointments] => 0 ) [2014-02-08] => stdClass Object ( [route_date] => 2014-02-08 [name] => 0 [openings] => 1 [appointments] => 0 ) [2014-02-09] => stdClass Object ( [route_date] => 2014-02-09 [name] => 0 [openings] => 1 [appointments] => 0 ) [2014-02-10] => stdClass Object ( [route_date] => 2014-02-10 [name] => 0 [openings] => 1 [appointments] => 0 ) [2014-02-11] => stdClass Object ( [route_date] => 2014-02-11 [name] => 0 [openings] => 1 [appointments] => 0 ) [2014-02-12] => stdClass Object ( [route_date] => 2014-02-12 [name] => 0 [openings] => 1 [appointments] => 0 ) [2014-02-13] => stdClass Object ( [route_date] => 2014-02-13 [name] => 0 [openings] => 1 [appointments] => 0 ) [2014-02-14] => stdClass Object ( [route_date] => 2014-02-14 [name] => 0 [openings] => 1 [appointments] => 0 ) [2014-02-15] => stdClass Object ( [route_date] => 2014-02-15 [name] => 0 [openings] => 1 [appointments] => 0 ) [2014-02-16] => stdClass Object ( [route_date] => 2014-02-16 [name] => 0 [openings] => 1 [appointments] => 0 ) [2014-02-17] => stdClass Object ( [route_date] => 2014-02-17 [name] => 0 [openings] => 1 [appointments] => 0 ) [2014-02-18] => stdClass Object ( [route_date] => 2014-02-18 [name] => 0 [openings] => 1 [appointments] => 0 ) [2014-02-19] => stdClass Object ( [route_date] => 2014-02-19 [name] => 0 [openings] => 1 [appointments] => 0 ) [2014-02-20] => stdClass Object ( [route_date] => 2014-02-20 [name] => 0 [openings] => 1 [appointments] => 0 ) [2014-02-21] => stdClass Object ( [route_date] => 2014-02-21 [name] => 0 [openings] => 1 [appointments] => 0 ) [2014-02-22] => stdClass Object ( [route_date] => 2014-02-22 [name] => 0 [openings] => 1 [appointments] => 0 ) [2014-02-23] => stdClass Object ( [route_date] => 2014-02-23 [name] => 0 [openings] => 1 [appointments] => 0 ) [2014-02-24] => stdClass Object ( [route_date] => 2014-02-24 [name] => 0 [openings] => 1 [appointments] => 0 ) [2014-02-25] => stdClass Object ( [route_date] => 2014-02-25 [name] => 0 [openings] => 1 [appointments] => 0 ) [2014-02-26] => stdClass Object ( [route_date] => 2014-02-26 [name] => 0 [openings] => 1 [appointments] => 0 ) [2014-02-27] => stdClass Object ( [route_date] => 2014-02-27 [name] => 0 [openings] => 1 [appointments] => 0 ) )

You will notice that only the first instance of the first object is added to the new array $openings.

UPDATE:

I just realized that I need a multidimensional array for this, but when I try add the second dimension, I get the following error:

 Cannot use object of type stdClass as array

What Am I doing wrong here? I want a single array $openings[route_date][number] but I can't get it work. Any help would be great.

3
  • 1
    by the way, please pretty print such arrays, by adding echo "<pre>" before the output and echo "</pre>" after it.. Commented Jan 11, 2014 at 9:05
  • I didn't know you could do that! That is so awesome!!!! Thank you!!!! Commented Jan 11, 2014 at 10:45
  • you're welcome, just a tip you should be aware of.. :) Commented Jan 11, 2014 at 14:33

3 Answers 3

2

It's because array_push() works with arrays, not objects. You should convert your stdClass to array. Try to use:

$ts = (array) $ts;
Sign up to request clarification or add additional context in comments.

Comments

0

In your array_push($openings[$ts->route_date][$ts->name], $ts); the first argument doesn't seem to be an array. array_push() will raise a warning if the first argument is not an array.

This should be array_push($openings, $ts);

I've made an example which works fine.

//I'm using mysql and I will switch to  MySQLi or PDO_MySQL later) 

$query = "SELECT * FROM events";
$result = mysql_query($query);
$openings = array();
while ($row = mysql_fetch_object($result)) {
    array_push($openings, $row); 
}
print_r($openings);

//output

Array
(
    [0] => stdClass Object
        (
            [idevent] => 1
            [event] => Event1
            [event_date] => 2014-01-06
        )

    [1] => stdClass Object
        (
            [idevent] => 2
            [event] => Event2
            [event_date] => 2014-01-07
        )

)

Comments

0

It turns out I needed to do a couple of things. Here is the code I needed:

    $time_slots = $wpdb->get_results($query);
echo("<pre>"); 
//print_r($time_slots); 

echo ("<br/><br/><br/><br/>");
/*organize slots into array*/


$openings = array(); 
foreach($time_slots as $ts)
{
    if(empty($openings))
    {
        echo("Empty Array: ");
        echo ("<br/>");

        echo("Inserting: ");
        echo ("<pre>");
        print_r($ts); 
        echo("</pre>");
        echo ("<pre>");
        $openings[$ts->route_date][$ts->name] = $ts; <--- Needed to add the [$ts->name]
        echo("</pre>");
        echo("contents of Opening: ");
        echo ("<pre>");
        print_r($openings); 
        echo ("</pre>");

    }
    elseif (array_key_exists($ts->route_date, $openings)) 
    {
        echo("Same Day");
        echo("Inserting: ");
        echo ("<pre>");
        print_r($ts); 
        echo("</pre>");
        $openings[$ts->route_date][$ts->name]=$ts; 
        echo("contents of Opening: ");
        echo ("<br/>");
        echo ("<br/>");
        print_r($openings); 
        echo ("<br/><br/><br/><br/>");
    }
    else
    {
        echo("New Day : ");
        echo("Inserting: ");
        echo ("<pre>");
        print_r($ts); 
        echo("</pre>");
        echo ("<pre>");
        $openings[$ts->route_date][$ts->name] = $ts; 
        echo("</pre>");
        echo("contents of Opening: ");
        echo ("<pre>");
        print_r($openings); 
        echo ("</pre>");
    }

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.