Francis Empleo Francis Empleo - 1 month ago 8
PHP Question

Arranging array in php

my problem is i can't arrange the array in the structure i want.
array1 and array2 are generated dynamically. as you can see in the array 2 it has a subjectid which is the same in array1, that means, that element is under the subject cpe 305. all elements in the array 2 which has the id of 5 is under the the subject of cpe 305. same logic with cpe 304.

array1:

Array
(
[0] => Array
(
[subjectid] => 5
[subjectcode] => Cpe 305
)

[1] => Array
(
[subjectid] => 4
[subjectcode] => Cpe 304
)

)


array2:

Array
(
[0] => Array
(
[subjectid] => 5
[soid] => 1
[socode] => A
[sodesc] => Ability to apply knowledge of mathematics and science to solve engineering problems
)

[1] => Array
(
[subjectid] => 5
[soid] => 3
[socode] => C
[sodesc] => Ability to design a system, component, or process to meet the desired needs within realistic constraints such as economic, environmental, social, political, ethical, health and safety, manufacturability, and sustainability, in accordance to standards
)

[2] => Array
(
[subjectid] => 5
[soid] => 4
[socode] => D
[sodesc] => Ability to function on multidisciplinary teams
)

[3] => Array
(
[subjectid] => 5
[soid] => 5
[socode] => E
[sodesc] => Ability to identify, formulate, and solve engineering problems
)

[4] => Array
(
[subjectid] => 5
[soid] => 9
[socode] => I
[sodesc] => Recognition of the need for, and an ability to engage in life-long learning
)

[5] => Array
(
[subjectid] => 4
[soid] => 10
[socode] => J
[sodesc] => Knowledge of contemporary issues
)


)

OUTPUT (my desired structure)

Array(
[subjectid] => 5
[subjectcode] => Cpe 305
[sodetails] => array(
[0]=>Array ([soid]=>1
[socode]=>A)
[1]=>Array([soid]=>3
[socode]=>C .....until the last
)
[subjectid] => 4
[subjectcode] => Cpe 305
[sodetails] => array(
[0]=>Array ([soid]=>10
[socode]=>J)
.......until the last
)


what i've tried

this is the code that im testing. i only include few data here in my test code.

$so = array();
$exist = array();

foreach ($this->subject as $key => $value) {
foreach ($this->sodetails as $key2 => $value2) {
if($value['subjectid'] === $value2['subjectid']){

$exist = array(
"subjectid" => $value['subjectid'],
"details" =>array(
"soid" => $value2['soid']
)
);
array_push($so, $exist);
}
}
}

Answer

First of all your output array is incorrect. An array cannot have more than one same indexes like "subjectid", "subjectcode". We need to have a different index. It will be wise to use subjectid as index for outer array.

First prepare your outer array as it is array1.

foreach($this->subject as $key => $val){
    $myArray[$val['subjectid']]['subjectid'] => $val['subjectid'];
    $myArray[$val['subjectid']]['subjectcode'] => $val['subjectcode'];
}

Now iterate your sodetails array.

foreach($this->sodetails as $key => $val){
    $temp['soid'] = $val['soid'];
    $temp['socode'] = $val['socode'];
    array_push($myArray[$val['subjectid']]['sodetails'], $temp);
}

There can be other ways too. But it is my style and I believe it will solve your problem.