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

    评论
    解决 无用
    打赏 举报

相关推荐 更多相似问题