我正在尝试使EVENTNAME
数组中的值成为其下方数组的键。
我希望能够在php变量上进行foreach并获取EVENTNAME
及其迁移,效果和供应链的计数。
$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\n\$stats =\n" . var_export($stats, true) . ";\n?>");
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',
),
)
答案 0 :(得分:0)
我假设您不知道键名,并使用array_keys来找出键名。
然后,我循环第一个子数组,并使用array_column中的键获取其他值。
预期的输出在子数组中不包含关联键,但是如果需要,请使用第二个代码。
$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)
输出:
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',
),
)
$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);
输出:
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',
),
)