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 the $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 [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] = > 12-01-2557 [ชื่อ] => 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 Object ( [route_date] => 2014-01-15 [name] => 0 [เปิด] => 1 [การนัดหมาย] => 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 [ชื่อ] => 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 Object ( [route_date] => 2014-01-20 [ชื่อ] => 0 [เปิด] => 1 [การนัดหมาย] = > 0 ) [21-01-2557] => stdClass Object ( [route_date] => 2014-01-21 [ชื่อ] => 0 [เปิด] => 1 [การนัดหมาย] => 0 ) [2014-01-22 ] => stdClass Object ( [route_date] => 2014-01-22 [name] => 0 [openings] => 1 [appointments] => 0 ) [2014-01-23] => stdClass Object ( [route_date] => 23-01-2557 [ชื่อ] => 0 [เปิด] => 1 [การนัดหมาย] => 0 ) [24-01-2557] => stdClass Object ( [route_date] => 2014-01-24 [ชื่อ ] => 0 [เปิด] => 1 [การนัดหมาย] => 0 ) [2014-01-25] => stdClass Object ( [route_date] => 2557-01-25 [ชื่อ] => 0 [เปิด] => 1 [การนัดหมาย] => 0 ) [2014-01-26] => stdClass Object ( [route_date] => 2014-01-26 [name] => 0 [เปิด] => 1 [การนัดหมาย] => 0 ) [ 27-01-2557] => stdClass Object ( [route_date] => 27-01-2557 [ชื่อ] => 0 [เปิด] => 1 [การนัดหมาย] => 0 ) [28-01-2557] => stdClass วัตถุ ( [route_date] => 28-01-2014-01-28 [ชื่อ] => 0 [เปิด] => 1 [การนัดหมาย] => 0 ) [29-01-2014-01-29] => stdClass Object ( [route_date] => 2014- 01-29 [ชื่อ] => 0 [เปิด] => 1 [การนัดหมาย] => 0 ) [2014-01-30] => stdClass Object ( [route_date] => 30-01-01-2014 [ชื่อ] => 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 [ชื่อ] => 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 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 [การนัดหมาย] => 0 ) [2014-02-06] => stdClass Object ( [route_date] => 2014-02-06 [ชื่อ] => 0 [เปิด] => 1 [การนัดหมาย] => 0 ) [2014-02-07] => stdClass Object ( [route_date] => 2014-02-07 [ชื่อ] => 0 [เปิด] => 1 [การนัดหมาย] => 0 ) [2014-02-08] => stdClass Object ( [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 Object ( [route_date] => 2014-02-11 [ชื่อ] => 0 [เปิด] => 1 [การนัดหมาย ] => 0 ) [2014-02-12] => stdClass Object ( [route_date] => 2014-02-12 [ชื่อ] => 0 [เปิด] => 1 [การนัดหมาย] => 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 [ชื่อ] => 0 [เปิด] => 1 [การนัดหมาย] => 0 ) [2014-02-15] => stdClass Object ( [route_date] => 2014-02-15 [ชื่อ] => 0 [เปิด] => 1 [การนัดหมาย] => 0 ) [2014-02-16] => stdClass Object ( [route_date] => 2014-02-16 [ชื่อ] => 0 [เปิด] => 1 [การนัดหมาย] => 0 ) [2014-02-17] => stdClass Object ( [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 Object ( [route_date] => 20-02-2557 [ชื่อ] => 0 [เปิด] => 1 [การนัดหมาย] => 0 ) [21-02-2557] => stdClass Object ( [route_date] => 2014-02-21 [ชื่อ] = > 0 [เปิด] => 1 [การนัดหมาย] => 0 ) [2014-02-22] => stdClass Object ( [route_date] => 2014-02-22 [ชื่อ] => 0 [เปิด] => 1 [ การนัดหมาย] => 0 ) [23-02-2014-02] => 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