Arranging array in php -
my problem can't arrange array in structure want. array1 , array2 generated dynamically. can see in array 2 has subjectid same in array1, means, element under subject cpe 305. elements in array 2 has id of 5 under the subject of cpe 305. same logic 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] => [sodesc] => ability apply knowledge of mathematics , science solve engineering problems ) [1] => array ( [subjectid] => 5 [soid] => 3 [socode] => c [sodesc] => ability design system, component, or process meet desired needs within realistic constraints such economic, environmental, social, political, ethical, health , safety, manufacturability, , sustainability, in accordance standards ) [2] => array ( [subjectid] => 5 [soid] => 4 [socode] => d [sodesc] => ability function on multidisciplinary teams ) [3] => array ( [subjectid] => 5 [soid] => 5 [socode] => e [sodesc] => ability identify, formulate, , solve engineering problems ) [4] => array ( [subjectid] => 5 [soid] => 9 [socode] => [sodesc] => recognition of need for, , ability 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 last ) [subjectid] => 4 [subjectcode] => cpe 305 [sodetails] => array( [0]=>array ([soid]=>10 [socode]=>j) .......until last )
what i've tried
this code im testing. include few data here in test code.
$so = array(); $exist = array(); foreach ($this->subject $key => $value) { foreach ($this->sodetails $key2 => $value2) { if($value['subjectid'] === $value2['subjectid']){ $exist = array( "subjectid" => $value['subjectid'], "details" =>array( "soid" => $value2['soid'] ) ); array_push($so, $exist); } } }
first of output array incorrect. array cannot have more 1 same indexes "subjectid", "subjectcode". need have different index. wise use subjectid index outer array.
first prepare outer array array1.
foreach($this->subject $key => $val){ $myarray[$val['subjectid']]['subjectid'] => $val['subjectid']; $myarray[$val['subjectid']]['subjectcode'] => $val['subjectcode']; }
now iterate sodetails array.
foreach($this->sodetails $key => $val){ $temp['soid'] = $val['soid']; $temp['socode'] = $val['socode']; array_push($myarray[$val['subjectid']]['sodetails'], $temp); }
there can other ways too. style , believe solve problem.
Comments
Post a Comment