1

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);
            }
          }
        }
2
  • Have you tried writing code? Show us your effort in solving this problem Commented Nov 10, 2016 at 10:39
  • Show us your tries. Commented Nov 10, 2016 at 10:39

3 Answers 3

1

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.

Sign up to request clarification or add additional context in comments.

Comments

1

You just need to pass subjectid as key in your return array,

 $exist = array();
            foreach ($this->subject as $key => $value) {
              foreach ($this->sodetails as $key2 => $value2) {
                if($value['subjectid'] === $value2['subjectid']){
                    $exist[$value['subjectid']]['subjectid'] = $value['subjectid'];
                    $exist[$value['subjectid']]['subjectcode'] = $value['subjectcode'];
                    $exist[$value['subjectid']]['sodetails'] = array(array('soid'=>$value2['soid']),array('socode'=>$value2['socode']));
                }
              }
            }

Comments

1

Instead of running the loop/iterations for m x n times, what you could do is first have the subjectid as the key for the $arrray1 and let the natural PHP array key handling do the job.

$new_array1 = array();
foreach($arrray1 as $item){
 $new_array1[$item['subjectcode']] = $item;
}

foreach($array2 as $desc){
 if(array_key_exists($desc['subjectid'],$new_array1){
  $new_array1[$desc['subjectid']]['desc'][] = $desc;
 }
}

This way you only have to make m + n iterations.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.