Để chuyển đổi một mảng thành đối tượng, stdClass[] được sử dụng. stdClass[] là một lớp trống, được sử dụng để chuyển các loại khác thành đối tượng. Nếu một đối tượng được chuyển đổi thành đối tượng, thì nó không bị sửa đổi. Tuy nhiên, nếu kiểu đối tượng được chuyển đổi/ép kiểu thì một thể hiện của stdClass được tạo, nếu nó không phải là NULL. Nếu nó là NULL, phiên bản mới sẽ trống
________số 8
$A = array[TRUE, FALSE, TRUE, TRUE];
while[current[$A] !== FALSE] {
var_dump[current[$A]];
next[$A];
}
?>
stdClass Object [ [user] => Array [ [0] => Array [ [name] => Henry [age] => 24 ] ] [students] => stdClass Object [ [math] => stdClass Object [ [name] => John [id] => 123 ] [science] => stdClass Object [ [name] => Peter [id] => 321 ] ] ]0
stdClass Object [ [user] => Array [ [0] => Array [ [name] => Henry [age] => 24 ] ] [students] => stdClass Object [ [math] => stdClass Object [ [name] => John [id] => 123 ] [science] => stdClass Object [ [name] => Peter [id] => 321 ] ] ]1
Hãy hiểu với mã ví dụ sau
Tạo đối tượng stdClass đa chiều cho mục đích trình diễn
$student1 = new stdClass[]; $student1->name = "John"; $student1->id = 123; $student2 = new stdClass[]; $student2->name = "Peter"; $student2->id = 321; $students = array[ "math" => $student1, "science" => $student2 ]; $objects = [object] $students; $mixedArray = array[ "user" => array[ array[ 'name' => 'Henry', 'age' => '24' ] ], "students" => $objects ]; $mixedObjects = [object] $mixedArray;
Mã trên sẽ tạo đối tượng stdClass là
stdClass Object [ [user] => Array [ [0] => Array [ [name] => Henry [age] => 24 ] ] [students] => stdClass Object [ [math] => stdClass Object [ [name] => John [id] => 123 ] [science] => stdClass Object [ [name] => Peter [id] => 321 ] ] ]
Lặp qua đối tượng stdClass $mixedObjects->students và hiển thị các giá trị như bên dưới
foreach [$mixedObjects->students as $obj => $student] { echo "Department : " . $obj . " => Name: " . $student->name . " | id:" . $student->id; echo "
"; }
đầu ra
Department : math => Name: John | id:123 Department : science => Name: Peter | id:321
Tôi mới làm quen với thế giới PHP, đã từng là một người làm việc ở mặt trước - và mặc dù tôi không có kiến thức cơ bản, nhưng phần này dường như làm tôi bối rối
get["shifts", array[
"location_id" => "106857",
"start_time" => "2014-03-05",
"user_id" => "702524"
]];
print_r[$shift_result];
Điều này in ra
stdClass Object
[
[start] => Wed, 02 Apr 2014 15:03:05 -0400
[end] => Sat, 05 Apr 2014 15:03:05 -0400
[shifts] => Array
[
[0] => stdClass Object
[
[id] => 31577385
[account_id] => 66357
[user_id] => 702524
[location_id] => 106857
[position_id] => 160296
[site_id] => 65765
[start_time] => Wed, 02 Apr 2014 09:00:00 -0400
[end_time] => Wed, 02 Apr 2014 19:00:00 -0400
[break_time] => 1
[color] => 999999
[notes] => Website 9-12
Game 12-1
Break 1 - 1:30
Web Site 1:30 - 3:00
Game 3:00-4:00
Break 4-4:30
Website 4:30 - 5:30
Game 5:30 - 6:30
Answer emails, plan what to tackle on website next for last 30 min
[alerted] => 1
[linked_users] =>
[shiftchain_key] =>
[published] => 1
[published_date] => Fri, 28 Mar 2014 10:16:34 -0400
[notified_at] => Fri, 28 Mar 2014 10:16:35 -0400
[created_at] => Fri, 28 Mar 2014 09:56:36 -0400
[updated_at] => Wed, 02 Apr 2014 07:01:26 -0400
[acknowledged] => 0
[is_open] =>
[actionable] =>
[block_id] => 0
]
]
[users] => Array
[
[0] => stdClass Object
[
[id] => 702524
[account_id] => 66357
[role] => 3
[first_name] => First
[last_name] => Name
[avatar] => stdClass Object
[
[url] => avatarurl
[size] => %s
]
[is_hidden] =>
[is_private] => 1
[is_deleted] =>
[has_email] => 1
[positions] => Array
[
[0] => 160296
]
[locations] => Array
[
[0] => 106857
]
[position_rates] => Array
[
]
[position_quality] => stdClass Object
[
[160296] => 3
]
[sort] => stdClass Object
[
[106857] => 0
]
]
]
[locations] => Array
[
[0] => stdClass Object
[
[id] => 106857
[account_id] => 66357
[is_default] => 0
[name] => Place of Employment
[sort] => 0
[max_hours] => 0
[address] => Address of Employment
[coordinates] => Array
[
]
[latitude] => 0
[longitude] => 0
[ip_address] =>
[created_at] => Thu, 13 Mar 2014 07:22:28 -0400
[updated_at] => Thu, 13 Mar 2014 12:22:28 -0400
[is_deleted] =>
]
]
[positions] => Array
[
[0] => stdClass Object
[
[id] => 160296
[account_id] => 66357
[name] => Team Member
[color] =>
[sort] => 0
[created_at] => Fri, 28 Feb 2014 14:52:58 -0500
[updated_at] => Fri, 28 Feb 2014 14:52:58 -0500
[is_deleted] =>
]
]
[sites] => Array
[
[0] => stdClass Object
[
[id] => 65765
[account_id] => 66357
[location_id] => 0
[name] => Place of Employment
[color] =>
[description] =>
[address] =>
[coordinates] => Array
[
]
[latitude] => 0
[longitude] => 0
[created_at] => Fri, 28 Feb 2014 14:59:34 -0500
[updated_at] => Fri, 28 Feb 2014 14:59:45 -0500
[is_deleted] =>
]
]
]
Tôi có thể cá nhân có thể vị trí, thời gian bắt đầu, v.v. mà không gặp vấn đề gì bằng cách làm điều gì đó như
$location = $shift_result->shifts[0]->location_id;
nhưng tôi không có kiến thức để biến nó thành "tự động. "
cuối cùng tôi muốn nó giống như
username
start time
vân vân
Nhưng điều này sẽ dành cho nhiều nhân viên, nhiều ca, nhiều ngày, v.v. Tôi hơi quá đầu