PHP Array_Push не работает

Я пытаюсь организовать набор результатов, полученных с помощью запроса к базе данных. Вот код, который я использую для этого:

$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); 

Вот как выглядит один результат, когда я print_r $ts:

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

Вот как выглядит одна петля. Вы заметите, что логика работает, и все идет туда, куда должно идти, но добавление 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 ) ) 

Когда все сказано и сделано, я получаю такой результат:

Вы заметите, что в новый массив $openings добавляется только первый экземпляр первого объекта.

ОБНОВИТЬ:

Я только что понял, что для этого мне нужен многомерный массив, но когда я пытаюсь добавить второе измерение, я получаю следующую ошибку:

Что я здесь делаю неправильно? Мне нужен один массив $openings[route_date][number], но я не могу заставить его работать. Любая помощь будет здорово.

 Cannot use object of type stdClass as array

Это потому, что

$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); 
работает с массивами, а не с объектами. Вы должны преобразовать свой print_r в массив. Попробуй использовать:


person BlackHatSamurai    schedule 11.01.2014    source источник
comment
Я не знал, что ты так умеешь! Это так здорово!!!! Спасибо!!!!   -  person Anshu Dwibhashi    schedule 11.01.2014
comment
Пожалуйста, просто совет, о котором вы должны знать .. :)   -  person BlackHatSamurai    schedule 11.01.2014
comment
Массив ( [2014-01-10] => stdClass Object ( [route_date] => 2014-01-10 [имя] => 0 [открытия] => 1 [назначения] => 0 ) [2014-01-11] => Объект stdClass ( [route_date] => 2014-01-11 [имя] => 0 [вакансии] => 1 [назначения] => 0 ) [2014-01-12] => Объект stdClass ( [route_date] = > 2014-01-12 [имя] => 0 [вакансии] => 1 [назначения] => 0 ) [2014-01-13] => stdClass Object ( [route_date] => 2014-01-13 [имя] => 0 [вакансии] => 1 [встречи] => 0 ) [2014-01-14] => stdClass Object ( [route_date] => 2014-01-14 [имя] => 0 [вакансии] => 1 [назначения] => 0 ) [2014-01-15] => объект stdClass ( [route_date] => 2014-01-15 [имя] => 0 [вакансии] => 1 [назначения] => 0 ) [2014 -01-16] => Объект stdClass ( [route_date] => 2014-01-16 [имя] => 0 [вакансии] => 1 [назначения] => 0 ) [2014-01-17] => Объект stdClass ( [route_date] => 2014-01-17 [имя] => 0 [открытия] => 1 [назначения] => 0 ) [2014-01-18] => stdClass Object ( [route_date] => 2014-01 -18 [имя] => 0 [вакансии] => 1 [назначения] => 0 ) [2014-01-19] => stdClass Object ( [route_date] => 2014-01-19 [имя] => 0 [вакансии] => 1 [назначения] => 0 ) [2014-01- 20] => Объект stdClass ( [route_date] => 2014-01-20 [имя] => 0 [вакансии] => 1 [назначения] => 0 ) [2014-01-21] => Объект stdClass ( [route_date ] => 2014-01-21 [имя] => 0 [вакансии] => 1 [назначения] => 0 ) [2014-01-22] => stdClass Object ( [route_date] => 2014-01-22 [ name] => 0 [вакансии] => 1 [назначения] => 0 ) [2014-01-23] => stdClass Object ( [route_date] => 2014-01-23 [имя] => 0 [вакансии] = > 1 [назначения] => 0 ) [2014-01-24] => объект stdClass ( [route_date] => 2014-01-24 [имя] => 0 [вакансии] => 1 [назначения] => 0 ) [2014-01-25] => Объект stdClass ( [route_date] => 2014-01-25 [имя] => 0 [вакансии] => 1 [назначения] => 0 ) [2014-01-26] => Объект stdClass ( [route_date] => 2014-01-26 [имя] => 0 [вакансии] => 1 [назначения] => 0 ) [2014-01-27] => объект stdClass ( [route_date] => 2014 -01-27 [имя] => 0 [вакансии] => 1 [назначения] = > 0 ) [2014-01-28] => stdClass Object ( [route_date] => 2014-01-28 [имя] => 0 [открытия] => 1 [назначения] => 0 ) [2014-01-29 ] => Объект stdClass ( [route_date] => 2014-01-29 [имя] => 0 [вакансии] => 1 [назначения] => 0 ) [2014-01-30] => Объект stdClass ( [route_date] => 2014-01-30 [имя] => 0 [вакансии] => 1 [назначения] => 0 ) [2014-01-31] ​​=> stdClass Object ( [route_date] => 2014-01-31 [имя ] => 0 [вакансии] => 1 [встречи] => 0 ) [2014-02-01] => stdClass Object ( [route_date] => 2014-02-01 [имя] => 0 [вакансии] => 1 [назначения] => 0 ) [2014-02-02] => stdClass Object ( [route_date] => 2014-02-02 [имя] => 0 [вакансии] => 1 [назначения] => 0 ) [ 2014-02-03] => stdClass Object ( [route_date] => 2014-02-03 [имя] => 0 [открытия] => 1 [назначения] => 0 ) [2014-02-04] => stdClass Объект ( [route_date] => 2014-02-04 [имя] => 0 [открытия] => 1 [назначения] => 0 ) [2014-02-05] => stdClass Object ( [route_date] => 2014- 02-05 [имя] => 0 [вакансии] => 1 [назначения] => 0 ) [2014-02-06] => stdClass Object ( [route_date] => 2014-02-06 [имя] => 0 [открытия] => 1 [назначения] => 0 ) [2014-02-07] => Объект stdClass ( [route_date] => 2014-02-07 [имя] => 0 [вакансии] => 1 [назначения] => 0 ) [2014-02-08] => Объект stdClass ( [route_date] = > 2014-02-08 [имя] => 0 [вакансии] => 1 [назначения] => 0 ) [2014-02-09] => stdClass Object ( [route_date] => 2014-02-09 [имя] => 0 [вакансии] => 1 [встречи] => 0 ) [2014-02-10] => stdClass Object ( [route_date] => 2014-02-10 [имя] => 0 [вакансии] => 1 [назначения] => 0 ) [2014-02-11] => объект stdClass ( [route_date] => 2014-02-11 [имя] => 0 [вакансии] => 1 [назначения] => 0 ) [2014 -02-12] => объект stdClass ( [route_date] => 2014-02-12 [имя] => 0 [вакансии] => 1 [назначения] => 0 ) [2014-02-13] => объект stdClass ( [route_date] => 2014-02-13 [имя] => 0 [открытия] => 1 [назначения] => 0 ) [2014-02-14] => stdClass Object ( [route_date] => 2014-02 -14 [имя] => 0 [вакансии] => 1 [назначения] => 0 ) [2014-02-15] => stdClass Object ( [route_date] => 2014-02-15 [имя] => 0 [открытия] => 1 [назначения] => 0 ) [2014-02-16] => Объект stdClass ( [route_date] => 2014-02-16 [имя] => 0 [вакансии] => 1 [назначения] => 0 ) [2014-02-17] => Объект stdClass ( [route_date] = > 2014-02-17 [имя] => 0 [вакансии] => 1 [назначения] => 0 ) [2014-02-18] => stdClass Object ( [route_date] => 2014-02-18 [имя] => 0 [вакансии] => 1 [встречи] => 0 ) [2014-02-19] => stdClass Object ( [route_date] => 2014-02-19 [имя] => 0 [вакансии] => 1 [назначения] => 0 ) [2014-02-20] => объект stdClass ( [route_date] => 2014-02-20 [имя] => 0 [вакансии] => 1 [назначения] => 0 ) [2014 -02-21] => объект stdClass ( [route_date] => 2014-02-21 [имя] => 0 [вакансии] => 1 [назначения] => 0 ) [2014-02-22] => объект stdClass ( [route_date] => 2014-02-22 [имя] => 0 [открытия] => 1 [назначения] => 0 ) [2014-02-23] => stdClass Object ( [route_date] => 2014-02 -23 [имя] => 0 [отверстия] => 1   -  person Anshu Dwibhashi    schedule 11.01.2014


Ответы (3)


В вашем array_push() первый аргумент не кажется массивом. array_push() выдаст предупреждение, если первый аргумент не является массивом.

$ts = (array) $ts;
person Alex Pliutau    schedule 11.01.2014

Я сделал пример, который отлично работает.

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

Оказывается, мне нужно было сделать пару вещей. Вот код, который мне был нужен:

//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
        )

)
person RaviRokkam    schedule 11.01.2014

кстати, печатайте такие массивы красиво, добавляя

    $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>");
    }
перед выводом и _2_ после него..

    $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>");
    }
person BlackHatSamurai    schedule 11.01.2014