Skip to content

Instantly share code, notes, and snippets.

@mattradford
Created August 28, 2015 10:34
Show Gist options
  • Save mattradford/4af927bcb71f8352692f to your computer and use it in GitHub Desktop.
Save mattradford/4af927bcb71f8352692f to your computer and use it in GitHub Desktop.
sort array of product variables
if(get_field('variations')) :
$product_variations = get_field('variations');
$product_variations_count = count( get_field( 'variations' ) );
echo $product_variations_count;
if($product_variations_count > 1) :
// vars
$order = array();
// populate order for length
foreach( $product_variations as $i => $row ) {
$order[ $i ] = $row['length'];
}
// multisort for longest
array_multisort( $order, SORT_DESC, $product_variations );
$longest = $product_variations[0];
// multisort for shortest
array_multisort( $order, SORT_ASC, $product_variations );
$shortest = $product_variations[0];
// populate order for width
foreach( $product_variations as $i => $row ) {
$order[ $i ] = $row['width'];
}
// multisort for widest
array_multisort( $order, SORT_DESC, $product_variations );
$widest = $product_variations[0];
// multisort for narrowest
array_multisort( $order, SORT_ASC, $product_variations );
$narrowest = $product_variations[0];
// populate order for price
foreach( $product_variations as $i => $row ) {
$order[ $i ] = $row['price'];
}
// multisort for most expensive
array_multisort( $order, SORT_DESC, $product_variations );
$costliest = $product_variations[0];
// multisort for least expensive
array_multisort( $order, SORT_ASC, $product_variations);
$cheapest = $product_variations[0];
else :
$single_length = $product_variations[0]['length'];
$single_width = $product_variations[0]['width'];
$single_price = $product_variations[0]['price'];
endif;
endif;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment