如何使用array_merge

时间:2019-04-02 22:59:26

标签: php array-merge

我正在尝试将2个数组合并成一个键(idhprim),但是我不知道该怎么做。我尝试了许多数组功能,但没有办法! 请你能帮我吗?

我有这个:

    $libelle = [
          'Hématies' => [0 =>  2, 1 =>  413, 2 =>  526, 3 =>  1481, 4 =>  1551],
          'Hémoglobine' => [0 =>  3, 1 =>  245, 2 =>  1552],
          'Hématocrite' => [0 =>  4, 1 =>  246, 2 =>  414, 3 =>  1553]
    ];

    $resultats = [
          '0' => [
              'id' =>  '880621',
              'id_hprim' =>  '413',
              'id_apicrypt' =>  '26818',
              'res_1' =>  '12',
              'normalite' =>  'N',
              'res_2' =>  '0'
          ],
          1 => [
              'id' =>  '880622',
              'id_hprim' =>  '526',
              'id_apicrypt' =>  '26818',
              'res_1' =>  '3.2',
              'normalite' =>  'N',
              'res_2' =>  '0'
          ],
          2 => [
              'id' =>  '880623',
              'id_hprim' =>  '3',
              'id_apicrypt' =>  '26818',
              'res_1' =>  '13',
              'normalite' =>  'N',
              'res_2' =>  '0'
          ],
          3 => [
              'id' =>  '880624',
              'id_hprim' =>  '414',
              'id_apicrypt' =>  '26818',
              'res_1' =>  '35',
              'normalite' =>  'N',
              'res_2' =>  '0'
          ],
          4 => [
              'id' =>  '880625',
              'id_hprim' =>  '245',
              'id_apicrypt' =>  '26818',
              'res_1' =>  '11.7',
              'normalite' =>  'N',
              'res_2' =>  '0'
          ]
    ];

,我希望拥有:

    $libelle = [
          'Hématies' => [
              0 => [
                  'id' =>  '880621',
                  'id_hprim' =>  '413',
                  'id_apicrypt' =>  '26818',
                  'res_1' =>  '12',
                  'normalite' =>  'N',
                  'res_2' =>  '0'  
              ],
              1 => [
                  'id' =>  '880622',
                  'id_hprim' =>  '526',
                  'id_apicrypt' =>  '26818',
                  'res_1' =>  '3.2',
                  'normalite' =>  'N',
                  'res_2' =>  '0'
              ]
          ],
          'Hémoglobine' => [
              0 = [
                  'id' =>  '880623',
                  'id_hprim' =>  '3',
                  'id_apicrypt' =>  '26818',
                  'res_1' =>  '13',
                  'normalite' =>  'N',
                  'res_2' =>  '0'  
              ]
          ],
          'Hématocrite' => [
            0 = [
                  'id' =>  '880624'
                  'id_hprim' =>  '414'
                  'id_apicrypt' =>  '26818'
                  'res_1' =>  '14/03/19'
                  'normalite' =>  'N'
                  'res_2' =>  '0'
            ]  
          ],
          ....
    ]

所以我尝试了这种方法,但是没有用:

foreach ($libelle as $key=>$val) {
foreach ($val as $v) {
    if (($k = array_search($v, array_column($resultats, 'id_hprim'))) !== false) {
        $val = array_merge($val, $resultats[$k]);
    }
}}var_dump($libelle);

0 个答案:

没有答案