用不同的键总结多个数组

我一直在尝试总结多个数组的值,但没有成功。我觉得我的大脑受不了了,因此我需要一些帮助或提示。


这是我迄今为止最好的:


$bani= array();

$iduri=array();

$total=0;

foreach($qp as $luna) {


    $nsd_id=$luna['nsd_id'];  // different ids every foreach

    $nsd_amount=$luna['nsd_amount']; // different values every foreach


    $nsd_id=explode("@",$nsd_id); // this makes them arrays

   $nsd_amount=explode("@",$nsd_amount); // same , it makes them arrays


    $iduri=array_merge($iduri,$nsd_id); // i tried getting making an array of all the ids it finds

    for($x=0;$x<count($iduri);$x++){

            $bani[$iduri[$x]] += intval($nsd_amount[$iduri[$x]]);

            $total += intval($nsd_amount[$x]);

        }


}

$iduri=array_unique($iduri,SORT_REGULAR);

print_r($iduri);

我试过为它找到的每个 ID 创建一个数组,然后根据它的位置,将它与数组相加。


它确实总结了一些东西,但我没有得到正确的值以及大量未定义的偏移量。


我觉得有比这更简单的方法,我真的很感激一些建议。

如果结果可能是以下数组,我将不胜感激:


Array ( 

[1] => 0 

[2] => 2500

[3] => 62000 

[4] => etc 

[5] => etc 

[12] => etc 

[14] => etc )

(id-s 作为键,汇总值作为对应 id 的值)


qp 的 var_export


Mysqli_result::__set_state(Array( 'Current_field' => NULL, 'Field_count' => NULL, 'Lengths' => NULL, 'Num_rows' => NULL, 'Type' => NULL, ))

foreach 中 $value 的 var_export(部分)


Array ( 'Id' => '329', 'Month' => 'May', 'Year' => '2019', 'Empid' => '124', 'Addedby' => 'PMSCLT10002', 'Nsd_id' => '1@2@3@4@5@12@14@15', 'Nsd_amount' => '0@0@0@0@0@0@4000@0', )

Array ( 'Id' => '303', 'Month' => 'April', 'Year' => '2019', 'Empid' => '124', 'Addedby' => 'PMSCLT10002', 'Nsd_id' => '1@2@3@4@5@12@14@15', 'Nsd_amount' => '0@0@2000@0@0@500@0@0', )

.. 等等


12345678_0001
浏览 149回答 2
2回答

LEATH

这是你可以做的片段,$nsdIds = [];$result = [];foreach ($qp as $key => $value) {&nbsp; &nbsp; // exploding Nsd_ids&nbsp; &nbsp; $nsdTemp = explode("@", $value['Nsd_id']);&nbsp; &nbsp; // exploding nsd_amounts&nbsp; &nbsp; $nsdAmount = explode("@", $value['Nsd_amount']);&nbsp; &nbsp; // saving all new nsd ids and only keep unique nsd ids&nbsp; &nbsp; $nsdIds = array_unique(array_merge($nsdIds, $nsdTemp));&nbsp; &nbsp; // I am now combining keeping nsd id as key and nsd amount as its value&nbsp; &nbsp; $temp1 = array_combine($nsdTemp, $nsdAmount);&nbsp; &nbsp; foreach ($nsdIds as $value1) {&nbsp; &nbsp; &nbsp; &nbsp; // for latest php version&nbsp; &nbsp; &nbsp; &nbsp; // $temp1 contains keys of nsd ids&nbsp; &nbsp; &nbsp; &nbsp; //&nbsp; value1 contains nsdids we are sure that it will be in nsdids as we are already merging those ids&nbsp; &nbsp; &nbsp; &nbsp; //&nbsp; then fetching amounf for that nsdids&nbsp; &nbsp; &nbsp; &nbsp; //$result[$value1] = ($result[$value1] ?? 0) + $temp1[$value1];&nbsp; &nbsp; &nbsp; &nbsp; // for php version less than 7&nbsp; &nbsp; &nbsp; &nbsp; $result[$value1] = (!empty($result[$value1]) ? $result[$value1] : 0) +&nbsp;&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; (!empty($temp1[$value1]) ? $temp1[$value1] : 0);&nbsp; &nbsp; }}print_r($result);die;演示为PHP 7和演示为<PHP 7。

叮当猫咪

只要nsd_id和nsd_amount在同一行中具有相同数量的值,@那么这应该可以工作。如果没有,那么您需要isset检查$nsd_amount[$key]:foreach($qp as $luna) {&nbsp; &nbsp; $nsd_id = explode("@", $luna['nsd_id']);&nbsp; &nbsp; $nsd_amount = explode("@", $luna['nsd_amount']);&nbsp; &nbsp; foreach($nsd_id as $key => $id) {&nbsp; &nbsp; &nbsp; &nbsp; if(isset($result[$id])) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; $result[$id] += $nsd_amount[$key];&nbsp; &nbsp; &nbsp; &nbsp; } else {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; $result[$id]&nbsp; = $nsd_amount[$key];&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; }&nbsp; &nbsp;}
打开App,查看更多内容
随时随地看视频慕课网APP