tags:

views:

22

answers:

2

Is there an equivalent build-in function to that one? (even without the test capability)

/**
 * 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  A: 

If there were you would most probably find it in http://docs.php.net/ref.array, but there isn't.

VolkerK
+1  A: 

I agree with VolkerK, but you can use something like this to extract a particular column from a 2D array

// 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);
halocursed