从一个关联数组,其主要存在在另一个数组中值显示值 [英] Display values from one associative array whose key exists as the value in another array

查看:102
本文介绍了从一个关联数组,其主要存在在另一个数组中值显示值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有两个数组,一个是OperID,另一个是OperSums。该OperID数组包含ID号,并且OperSums数组包含附连到总看起来像这样的ID:

阵列1(OperID)

  {[0] => OPER1 [1] => oper2 [2] => 3 [3] => oper4 [4] => oper5 [5] => oper6 [6] => oper7 [7] => oper8 [8] => oper9}

阵列2(OperSums)

  {[oper3] =>浮动(17498.5)OPER1] =>浮动(10383.5)oper2] =>浮动(6277)[oper4] =>浮动(10224.67)[oper6] =>浮动(3955.65)[oper5] =>浮动(4997.78)[oper8] =>浮动(11382)[oper9] =>浮动(5072.1)oper7] =>浮动(14759)OPER-nb3n0hah-1tueubqo] =>浮动(1033.45)OPER-50f6e4ad-9effbec7] =>浮动(3058)[OPER-4f05a90b-03b379f9] =>浮动(12112.5)OPER-4db82d0b-796a3081] =>浮动(621)[OPER-qxr9ryex-bsmm0g6f] =>串(4)0.00[OPER-qtgjvw8y-1uqtw058] =>浮动(10023)OPER-487b885e-dbbae536] =>字符串(6)340.00[OPER-shcuaee2-yldfdxsd] =>浮动(467)[OPER-416fd551-da6937eb] =>浮动(6563)[OPER-50564d75-f1da98ec] =>串(4)0.00[OPER-l65tf5ex-w5qfinca] =>浮动(1746)[OPER-52657816-3d6516e2] =>浮动(3495)[OPER-4a82c3be-bccc185d] =>浮动(0)OPER-1f2mnwry-nfywuasi] =>串(6)255.95}

我想只收集了在两个数组操作值并显示每个运营商这是每一个漂浮在阵列2内的总和,并截断所有额外的数据。我真的不是关联数组很大,所以在正确的方向的指针或提示将不胜AP preciated。这是PHP的方式。

下面是code,我迄今的工作:

  $ operSums =阵列();$ operearnedArray [] =数组(
      '量'=> $行['ChargeAmount'],
      'ID'=> $行['OperatorID']);
的foreach($ operearnedArray为$值){
      如果($值['身份证'] ==''|| $值['身份证'] == NULL){
      继续;
      }
      如果(array_key_exists($值['身份证'],$ operSums)){
        $ operSums [$值['身份证'] + = $值['量'];
      }其他{
        $ operSums [$值['身份证'] = $值['量'];
      }
}


解决方案

如果我理解正确的话,你只需要通过$步骤OperSums(数组2),如果钥匙$ OperID(阵列1)是否存在显示值?

 的foreach($ OperSums为的$ id => $值){
    如果(in_array($ ID,$ OperID)){
        回声$ ID。 = GT; 。'$价值;
    }
}

如果你确实需要你去(或创建一个新的数组)截断所有额外的数据,然后取消设置阵列,尽管这似乎有必要不,如果你只是显示的结果?

I have two arrays, one is "OperID" the other is "OperSums". The OperID array contains ID numbers, and the OperSums array contains IDs attached to a total which looks like this:

Array 1 (OperID)

{[0] => oper1 [1] => oper2 [2] => 3 [3] => oper4 [4] => oper5 [5] => oper6 [6] => oper7 [7] => oper8 [8] => oper9}

Array 2 (OperSums)

{["oper3"]=> float(17498.5) ["oper1"]=> float(10383.5) ["oper2"]=> float(6277) ["oper4"]=> float(10224.67) ["oper6"]=> float(3955.65) ["oper5"]=> float(4997.78) ["oper8"]=> float(11382) ["oper9"]=> float(5072.1) ["oper7"]=> float(14759) ["oper-nb3n0hah-1tueubqo"]=> float(1033.45) ["oper-50f6e4ad-9effbec7"]=> float(3058) ["oper-4f05a90b-03b379f9"]=> float(12112.5) ["oper-4db82d0b-796a3081"]=> float(621) ["oper-qxr9ryex-bsmm0g6f"]=> string(4) "0.00" ["oper-qtgjvw8y-1uqtw058"]=> float(10023) ["oper-487b885e-dbbae536"]=> string(6) "340.00" ["oper-shcuaee2-yldfdxsd"]=> float(467) ["oper-416fd551-da6937eb"]=> float(6563) ["oper-50564d75-f1da98ec"]=> string(4) "0.00" ["oper-l65tf5ex-w5qfinca"]=> float(1746) ["oper-52657816-3d6516e2"]=> float(3495) ["oper-4a82c3be-bccc185d"]=> float(0) ["oper-1f2mnwry-nfywuasi"]=> string(6) "255.95"}

I'm wanting to gather only the operator values that are in both arrays and display the totals for each operator which are within each float in Array 2, and truncate all extra data. I'm really not that great with associative arrays, so any pointers or tips in the right direction would be greatly appreciated. This is in PHP by the way.

Here is the code I'm working with so far:

$operSums = array();

$operearnedArray[] = array(
      'amount' => $row['ChargeAmount'], 
      'id' => $row['OperatorID']);


foreach ($operearnedArray as $value) {
      if($value['id'] == '' || $value['id'] == null) {
      continue;
      }
      if(array_key_exists($value['id'], $operSums)) {
        $operSums[$value['id']] += $value['amount'];
      } else {
        $operSums[$value['id']] = $value['amount'];
      }
}

解决方案

If I understand correctly, you just need to step through $OperSums (Array 2) and display the value if the key exists in $OperID (Array 1)?

foreach ($OperSums as $id => $value) {
    if (in_array($id,$OperID)) {
        echo $id.' => '.$value;
    }
}

If you actually need to "truncate all extra data" then unset the arrays as you go (or create a new array), although that does not seem necessary if you are just displaying the results?

这篇关于从一个关联数组,其主要存在在另一个数组中值显示值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆