встроенный extractColFromArray? - PullRequest
       4

встроенный extractColFromArray?

0 голосов
/ 07 октября 2009

Есть ли эквивалентная встроенная функция к этому? (даже без возможности test )

/**
 * extracts a column from a 2D associative array, with an optional selection over another column
 *
 * @param $aArray     array to extract from
 * @param $aColName   name of the column to extract, ex. 'O_NAME'
 * @param $aColTest   (optional) name of the column to make the test on, ex. 'O_ID'
 * @param $aTest      (optional) string for the test ex. ">= 10", "=='".$toto."'"
 * @return            1D array with only the extracted column
 * @access public
 */

 function extractColFromArray($aArray, $aColName, $aColTest="", $aTest="") {
  $mRes = array();
  foreach($aArray as $row) {
   if (($aColTest == "") || (eval("return " . $row[$aColTest] . $aTest . ";" )) ) {
    $mRes[] = $row[$aColName];
   }
  }
  return $mRes;
 } // extractColFromArray

Alex

Ответы [ 2 ]

2 голосов
/ 07 октября 2009

Если бы вы были, вы, скорее всего, нашли бы его в http://docs.php.net/ref.array,, но его нет.

1 голос
/ 07 октября 2009

Я согласен с VolkerK, но вы можете использовать что-то подобное для извлечения определенного столбца из 2D-массива

// set up a small test environment
$test_subject[] = array("a", "b", "c");
$test_subject[] = array("d", "e", "f");

//Select the column to extract(In this case the 1st column)
$column=0;

// do the actual work
$result = array_map('array_slice', $test_subject,
    array_fill(0, count($test_subject), $column),
    array_fill(0, count($test_subject), 1)
);

// and the end result
print_r($result);
Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...