* {use $productsArray} * {var $priceArray = array_extract_by_properties( $productsArray, array( "price" ) )} * {debug_dump( $priceArray )} * * * The first line of the code above imports an array with product objects. * A product object has at least one property price. Meaning that: * * * {$productArray[0]->price} * * * returns the price of the first product in the array. * The function array_extract_by_properties goes through the whole array of products and stores the * price in the array. The output can be something like: * * * array * ( * [0] => 200 * [1] => 199.24 * [2] => 50.20 * ) * * * @param array(mixed) $array * @param array(string) $properties * @return array(mixed) */ public static function array_extract_by_properties( $array, $properties ) { $list = array(); foreach ( $array as $item ) { foreach ( $properties as $property ) { $list[] = $item->$property; } } return $list; } /** * Returns an array that contains $length times the input $array. * * @param array(mixed) $array * @param int $length * @return array(mixed) */ public static function array_repeat( $array, $length ) { $out = array(); for( $i = 0; $i < $length; ++$i) { $out = array_merge( $out, $array ); } return $out; } /** * Returns a sorted array. * * @param array(mixed) $array * @param int $flags Contains a PHP array sort flag. Default set to SORT_REGULAR. * @return array(mixed) */ public static function array_sort( $array, $flags = SORT_REGULAR ) { $tmp = $array; sort( $tmp, $flags ); return $tmp; } /** * Returns a reversed sorted array. * * @param array(mixed) $array * @param int $flags Contains a PHP array sort flag. Default set to SORT_REGULAR. * @return array(mixed) */ public static function array_sort_reverse( $array, $flags = SORT_REGULAR ) { $tmp = $array; rsort( $tmp, $flags ); return $tmp; } /** * Returns a sorted hash, sorted on the values. * * @param array(mixed) $array * @param int $flags Contains a PHP array sort flag. Default set to SORT_REGULAR. * @return array(mixed) */ public static function hash_sort( $array, $flags = SORT_REGULAR ) { $tmp = $array; asort( $tmp, $flags ); return $tmp; } /** * Returns a reversed sorted hash, sorted on the values. * * @param array(mixed) $array * @param int $flags Contains a PHP array sort flag. Default set to SORT_REGULAR. * @return array(mixed) */ public static function hash_sort_reverse( $array, $flags = SORT_REGULAR ) { $tmp = $array; arsort( $tmp, $flags ); return $tmp; } /** * Returns a sorted hash, sorted on the keys. * * @param array(mixed) $array * @param int $flags Contains a PHP array sort flag. Default set to SORT_REGULAR. * @return array(mixed) */ public static function hash_sort_keys( $array, $flags = SORT_REGULAR ) { $tmp = $array; ksort( $tmp, $flags ); return $tmp; } /** * Returns a reversed sorted hash, sorted on the keys. * * @param array(mixed) $array * @param int $flags Contains a PHP array sort flag. Default set to SORT_REGULAR. * @return array(mixed) */ public static function hash_sort_keys_reverse( $array, $flags = SORT_REGULAR ) { $tmp = $array; krsort( $tmp, $flags ); return $tmp; } } ?>