在php中安排数组

时间:2016-11-10 10:36:38

标签: php

我的问题是我无法在我想要的结构中安排数组。 array1和array2是动态生成的。正如您在数组2中看到的那样,它有一个在array1中相同的subjectid,也就是说,该元素位于主题cpe 305下。数组2中id为5的所有元素都在cpe的主题下305.与cpe 304相同的逻辑。

ARRAY1:

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

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

)

数组2:

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(我想要的结构)

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
                 )

我尝试了什么

这是我测试的代码。我的测试代码中只包含少量数据。

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

3 个答案:

答案 0 :(得分:1)

不是运行m x n次的循环/迭代,而是你可以做的是首先将subjectid作为$arrray1的键,并让自然的PHP数组键处理工作。

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

这样您只需进行m + n次迭代。

答案 1 :(得分:1)

您只需要将subjectid作为返回数组中的键传递,

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

答案 2 :(得分:1)

首先,您的输出数组不正确。数组不能有多个相同的索引,如“subjectid”,“subjectcode”。我们需要有不同的索引。将subjectid用作外部数组的索引是明智的。

首先准备你的外部数组,因为它是array1。

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

现在迭代你的sodetails数组。

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

还有其他方法。但这是我的风格,我相信它会解决你的问题。