开发者

How do I convert an array according to one of its value

开发者 https://www.devze.com 2023-04-13 01:31 出处:网络
I am not good at playing with array. So I really need your help. I got an array like this one: array(1) {

I am not good at playing with array. So I really need your help. I got an array like this one:

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"
}
}
}

What I want to do is to write a function to convert it to(remove ['assignedlessons'] and split the array according to lesson:

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"
 }
} 
}


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

精彩评论

暂无评论...
验证码 换一张
取 消