Reputation: 18009
I am having this array. Last few hours I have been trying to loop through and make another which will arrange the question in respective subjects.
[23] => Array
(
[right] => A list of station and network addresses with corresponding gateway IP address.
[id] => 23
[level] => Professional
[subject] => Array
(
[0] => Array
(
[tid] => 1
)
[1] => Array
(
[tid] => 6
)
)
[question] => What is an IP routing table?
[answer] => A list of host names and corresponding IP addresses.
[correct] => 0
)
[22] => Array
(
[right] => Session hijacking attack
[id] => 22
[level] => Professional
[subject] => Array
(
[0] => Array
(
[tid] => 1
)
[1] => Array
(
[tid] => 6
)
)
[question] => How would an IP spoofing attack be best classified?
[answer] => Session hijacking attack
[correct] => 1
)
[21] => Array
(
[right] => Repeater
[id] => 21
[level] => Intermediate
[subject] => Array
(
[0] => Array
(
[tid] => 1
)
[1] => Array
(
[tid] => 6
)
)
I want to make something like below.
array ( [tid1] => array (
[0] array ( [question] => something )
[1] array ( [question] => somethingelse )
)
[tid2] => array (
[0] array ( [question] => something )
[1] array ( [question] => somethingelse )
)
But I'm not able to do it. What is the best way to do this?
Upvotes: 0
Views: 151
Reputation: 3273
Assuming that your data is in an array called $data
and that since each question can have multiple subjects, each question may appear multiple times in the final array:
$final = array();
foreach($data as $datum){
$subjects = $datum['subject'];
foreach($subjects as $subject){
$tid = 'tid' . $subject['tid'];
$final[$tid][] = array('question' => $datum['question']);
}
}
Upvotes: 1