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 DS18B20内部ADC模数转换器
  • ¥15 做个有关计算的小程序
  • ¥15 MPI读取tif文件无法正常给各进程分配路径
  • ¥15 如何用MATLAB实现以下三个公式(有相互嵌套)
  • ¥30 关于#算法#的问题:运用EViews第九版本进行一系列计量经济学的时间数列数据回归分析预测问题 求各位帮我解答一下
  • ¥15 setInterval 页面闪烁,怎么解决
  • ¥15 如何让企业微信机器人实现消息汇总整合
  • ¥50 关于#ui#的问题:做yolov8的ui界面出现的问题
  • ¥15 如何用Python爬取各高校教师公开的教育和工作经历
  • ¥15 TLE9879QXA40 电机驱动