ฉันกำลังพยายามจัดระเบียบชุดผลลัพธ์ที่ได้รับผ่านการสืบค้นฐานข้อมูล นี่คือรหัสที่ฉันใช้เพื่อทำสิ่งนี้:
$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
ของคุณเป็นอาร์เรย์ ลองใช้: