dongtong2021 2019-03-11 18:24 采纳率: 0%
浏览 27
已采纳

PHP - 合并超过2个数组

I'm trying to make the values in the EVENTNAME array the key of the arrays below it.

I want to be able to do a foreach on on a php variable and get the EVENTNAME and the count for its IMMIGRATIONS, EFFECTS and SUPPLYCHAINS.

$keyevents = oci_parse($conn, "SELECT EVENTNAME FROM KEYEVENTS GROUP BY EVENTNAME");
oci_execute($keyevents);
oci_fetch_all($keyevents, $keyevent);

$immigrations = oci_parse($conn, "SELECT COUNT(*) IMMIGRATIONS FROM KEYEVENTS LEFT JOIN IMMIGRATION ON KEYEVENTS.EVENTID = IMMIGRATION.EVENTID GROUP BY EVENTNAME");
oci_execute($immigrations);
oci_fetch_all($immigrations, $imm);

$effects = oci_parse($conn, "SELECT COUNT(*) EFFECTS FROM KEYEVENTS LEFT JOIN EFFECT ON KEYEVENTS.EVENTID = EFFECT.EVENTID GROUP BY EVENTNAME");
oci_execute($effects);
oci_fetch_all($effects, $eff);

$supplychains = oci_parse($conn, "SELECT COUNT(*) SUPPLYCHAINS FROM KEYEVENTS LEFT JOIN SUPPLYCHAINS ON KEYEVENTS.EVENTID = SUPPLYCHAINS.EVENTID GROUP BY EVENTNAME");
oci_execute($supplychains);
oci_fetch_all($supplychains, $supp);

$stats = array_merge($keyevent, $imm, $eff, $supp);

highlight_string("<?php
\$stats =
" . var_export($stats, true) . ";
?>");

var_export

array (
  'EVENTNAME' => 
  array (
    0 => 'Brexit',
    1 => 'leave date set',
    2 => 'leave date ',
    3 => 'deal or no deal',
  ),
  'IMMIGRATIONS' => 
  array (
    0 => '1',
    1 => '1',
    2 => '1',
    3 => '1',
  ),
  'EFFECTS' => 
  array (
    0 => '2',
    1 => '1',
    2 => '1',
    3 => '2',
  ),
  'SUPPLYCHAINS' => 
  array (
    0 => '1',
    1 => '1',
    2 => '1',
    3 => '1',
  ),
)

Output of $Stats

  • 写回答

1条回答 默认 最新

  • dongpin1059 2019-03-11 20:24
    关注

    I assume you don't know the key names and use array_keys to figure it out.
    Then I loop the first subarray and use the key in array_column to get the other values.
    Your expected output does not include associative keys in the subarray, but if you need it use the second code.

    $arr = array ( 'EVENTNAME' => array ( 0 => 'Brexit', 1 => 'leave date set', 2 => 'leave date ', 3 => 'deal or no deal', ), 'IMMIGRATIONS' => array ( 0 => '1', 1 => '1', 2 => '1', 3 => '1', ), 'EFFECTS' => array ( 0 => '2', 1 => '1', 2 => '1', 3 => '2', ), 'SUPPLYCHAINS' => array ( 0 => '1', 1 => '1', 2 => '1', 3 => '1', ), );
    $keys = array_keys($arr);
    
    foreach($arr[$keys[0]] as $key => $val){
        $result[$val] = array_column(array_slice($arr,1), $key);
    }
    
    var_export($result)
    

    Output:

    array (
      'Brexit' => 
      array (
        0 => '1',
        1 => '2',
        2 => '1',
      ),
      'leave date set' => 
      array (
        0 => '1',
        1 => '1',
        2 => '1',
      ),
      'leave date ' => 
      array (
        0 => '1',
        1 => '1',
        2 => '1',
      ),
      'deal or no deal' => 
      array (
        0 => '1',
        1 => '2',
        2 => '1',
      ),
    )
    

    https://3v4l.org/Seteg


    $keys = array_keys($arr);
    
    foreach($arr[$keys[0]] as $key => $val){
        $result[$val] = array_combine(array_slice($keys, 1), array_column(array_slice($arr,1), $key));
    }
    
    var_export($result);
    

    Output:

    array (
      'Brexit' => 
      array (
        'IMMIGRATIONS' => '1',
        'EFFECTS' => '2',
        'SUPPLYCHAINS' => '1',
      ),
      'leave date set' => 
      array (
        'IMMIGRATIONS' => '1',
        'EFFECTS' => '1',
        'SUPPLYCHAINS' => '1',
      ),
      'leave date ' => 
      array (
        'IMMIGRATIONS' => '1',
        'EFFECTS' => '1',
        'SUPPLYCHAINS' => '1',
      ),
      'deal or no deal' => 
      array (
        'IMMIGRATIONS' => '1',
        'EFFECTS' => '2',
        'SUPPLYCHAINS' => '1',
      ),
    )
    

    https://3v4l.org/aPCub

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 ansys fluent计算闪退
  • ¥15 有关wireshark抓包的问题
  • ¥15 需要写计算过程,不要写代码,求解答,数据都在图上
  • ¥15 向数据表用newid方式插入GUID问题
  • ¥15 multisim电路设计
  • ¥20 用keil,写代码解决两个问题,用库函数
  • ¥50 ID中开关量采样信号通道、以及程序流程的设计
  • ¥15 U-Mamba/nnunetv2固定随机数种子
  • ¥15 vba使用jmail发送邮件正文里面怎么加图片
  • ¥15 vb6.0如何向数据库中添加自动生成的字段数据。