我不擅长玩数组。所以我真的需要你的帮助。 我有一个像这样的数组:
array(1) {
[0]=>
array(5) {
["first_name"]=>
string(3) "Bob"
["last_name"]=>
string(6) "Gorsky"
["date_of_birth"]=>
string(10) "1954-04-27"
["assignedlessons"]=>
string(16) "222|69|308|50|91"
["lesson"]=>
array(5) {
[0]=>
string(3) "222"
[1]=>
string(2) "69"
[2]=>
string(3) "308"
[3]=>
string(2) "50"
[4]=>
string(2) "91"
}
}
}
我想要做的是编写一个函数将其转换为(删除['assignedlessons']并根据课程拆分数组:
array(5) {
[0]=>
array(4) {
["first_name"]=>
string(3) "Bob"
["last_name"]=>
string(6) "Gorsky"
["date_of_birth"]=>
string(10) "1954-04-27"
["lesson"]=>
array(1) {
[0]=>
string(3) "222"
}
}
[1]=>
array(4) {
["first_name"]=>
string(3) "Bob"
["last_name"]=>
string(6) "Gorsky"
["date_of_birth"]=>
string(10) "1954-04-27"
["lesson"]=>
array(1) {
[0]=>
string(2) "69"
}
}
[2]=>
array(4) {
["first_name"]=>
string(3) "Bob"
["last_name"]=>
string(6) "Gorsky"
["date_of_birth"]=>
string(10) "1954-04-27"
["lesson"]=>
array(1) {
[0]=>
string(3) "308"
}
}
[3]=>
array(4) {
["first_name"]=>
string(3) "Bob"
["last_name"]=>
string(6) "Gorsky"
["date_of_birth"]=>
string(10) "1954-04-27"
["lesson"]=>
array(1) {
[0]=>
string(2) "50"
}
}
[4]=>
array(4) {
["first_name"]=>
string(3) "Bob"
["last_name"]=>
string(6) "Gorsky"
["date_of_birth"]=>
string(10) "1954-04-27"
["lesson"]=>
array(1) {
[0]=>
string(2) "91"
}
}
}
答案 0 :(得分:2)
$result = array();
foreach ($array['lesson'] as $lesson) {
$result[] = array(
'first_name' => $array['first_name'],
'last_name' => $array['last_name'],
'date_of_birth' => $array['date_of_birth'],
'lesson' => $lesson
);
}