使用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];

}

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