dsux90368 2017-08-13 14:14
浏览 40
已采纳

使用array_column搜索数组数组,在(n)列返回相应的值。 怎么样?

The following code works. I have the following array of arrays that I would like to search with array_column and return corresponding value at (n) column. At the moment my code only returns a column to the right or a column to the left.

Existing working code

function getField_UnitPrice($xmc) {
    // Key values MUST ALWAYS BE UNIQUE.
    // Values on the left.
    $xmpd_db = array(
    1 => array('EA_WTRESRVD', '122.67'),
    2 => array('EA_WTRESRV', '184.00'),
    3 => array('EA_FEEASBT', '300.00'),
    4 => array('GL_WTREXTWP', '0.64'),
    5 => array('GL_WTREXTWPA', '0.96')

  );

  $search_result = array_column($xmpd_db, 1, 0);
  return $search_result[$xmc];

}

As you can see, this function returns a price for the code you look up but what if the array of arrays has more than 2 columns?

I think the answer lies in the line of code below but no matter how I change these indexes nothing more than the code column displays..

$search_result = array_column($xmpd_db, 1, 0);

Goal

function getField_Target($xmc, $column_index)
    {
    // Key values MUST ALWAYS BE UNIQUE.
    // Values on the left.
    $xmpd_db = array(
    1 => array('EA', 'WTRESRVD', 'EMERGENCY SERVICE CALL', '122.67'),
    2 => array('EA', 'WTRESRV', 'EMERGENCY SERVICE CALL AFTER HOURS', '184.00')

  );

  $search_result = array_column($xmpd_db, 0, 1);
  return $search_result[$xmc];

    }

Solution

/* $field, 1, 0 = EA */
$unit_of_measure = getField_Target($field, 1, 0);
/* $field, 1, 1 = WTRESRVD*/
$code = getField_Target($field, 1, 1);
/* $field, 1, 2 = EMERGENCY SERVICE CALL*/
$code_desc = getField_Target($field, 1, 2);
/* $field, 1, 3 = 000.00*/
$code_price = getField_Target($field, 1, 3);

// Call
echo $unit_of_measure . " - " . $code . " - " . $code_desc . " - " . $code_price . "<br>";

function getField_Target($xmc, $column_index = 1, $column_value = 3) {
    // Key values MUST ALWAYS BE UNIQUE.
    // Values on the left.
    $xmpd_db = array(
    1 => array('EA', 'EA_WTRESRVD', 'EMERGENCY SERVICE CALL', '000.10'),
    2 => array('EA', 'EA_WTRESRV', 'EMERGENCY SERVICE CALL AFTER HOURS', '000.20')

    );

  $search_result = array_column($xmpd_db, $column_value, $column_index);

  return $search_result[$xmc];

}
  • 写回答

1条回答 默认 最新

  • douyan2970 2017-08-13 14:31
    关注

    The line you need is...

    $search_result = array_column($xmpd_db, 3, 1);
    

    What this is doing is taking the 3rd(actually the 4th as it's zero based) field (the price field) and indexing it by the 1st(i.e. the 2nd) column.

    So $search_result ends up as

    Array
    
        (
            [WTRESRVD] => 122.67
            [WTRESRV] => 184.00
        )
    

    To expand on the code...

    function getField_Target($xmc, $column_index = 1, $column_value = 3)
        {
        // Key values MUST ALWAYS BE UNIQUE.
        // Values on the left.
        $xmpd_db = array(
        1 => array('EA', 'WTRESRVD', 'EMERGENCY SERVICE CALL', '122.67'),
        2 => array('EA', 'WTRESRV', 'EMERGENCY SERVICE CALL AFTER HOURS', '184.00')
    
      );
    
      $search_result = array_column($xmpd_db, $column_value, $column_index);
      return $search_result[$xmc];
    
        }
    

    So,

    echo getField_Target('WTRESRV');        // 184.00
    echo getField_Target('WTRESRV', 1, 2);  // EMERGENCY SERVICE CALL AFTER HOURS
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 求差集那个函数有问题,有无佬可以解决
  • ¥15 【提问】基于Invest的水源涵养
  • ¥20 微信网友居然可以通过vx号找到我绑的手机号
  • ¥15 寻一个支付宝扫码远程授权登录的软件助手app
  • ¥15 解riccati方程组
  • ¥15 display:none;样式在嵌套结构中的已设置了display样式的元素上不起作用?
  • ¥15 使用rabbitMQ 消息队列作为url源进行多线程爬取时,总有几个url没有处理的问题。
  • ¥15 Ubuntu在安装序列比对软件STAR时出现报错如何解决
  • ¥50 树莓派安卓APK系统签名
  • ¥65 汇编语言除法溢出问题