@ 2015-09-30T14:23:22Z <?php
$arr = array(
array('product_code' => 'battle-for-zendikar-token-1', 'product' => 'battle-for-zendikar-token-1'),
array('product_code' => 'battle-for-zendikar-token-2', 'product' => 'battle-for-zendikar-token-2'),
array('product_code' => 'battle-for-zendikar-token-3', 'product' => 'battle-for-zendikar-token-3'),
array('product_code' => 'battle-for-zendikar-token-4', 'product' => 'battle-for-zendikar-token-4'),
array('product_code' => 'battle-for-zendikar-token-5', 'product' => 'battle-for-zendikar-token-5'),
array('product_code' => 'battle-for-zendikar-token-6', 'product' => 'battle-for-zendikar-token-6'),
array('product_code' => 'battle-for-zendikar-token-7', 'product' => 'battle-for-zendikar-token-7'),
array('product_code' => 'battle-for-zendikar-token-8', 'product' => 'battle-for-zendikar-token-8'),
array('product_code' => 'battle-for-zendikar-token-9', 'product' => 'battle-for-zendikar-token-9'),
array('product_code' => 'battle-for-zendikar-token-10', 'product' => 'battle-for-zendikar-token-10'),
array('product_code' => 'battle-for-zendikar-token-11', 'product' => 'battle-for-zendikar-token-11'),
array('product_code' => 'battle-for-zendikar-token-12', 'product' => 'battle-for-zendikar-token-12'),
array('product_code' => 'battle-for-zendikar-token-13', 'product' => 'battle-for-zendikar-token-13'),
array('product_code' => 'battle-for-zendikar-token-14', 'product' => 'battle-for-zendikar-token-14'),
array('product_code' => 'battle-for-zendikar-1', 'product' => 'battle-for-zendikar-1'),
array('product_code' => 'battle-for-zendikar-2', 'product' => 'battle-for-zendikar-2'),
array('product_code' => 'battle-for-zendikar-3', 'product' => 'battle-for-zendikar-3'),
array('product_code' => 'battle-for-zendikar-4', 'product' => 'battle-for-zendikar-4'),
array('product_code' => 'battle-for-zendikar-5', 'product' => 'battle-for-zendikar-5'),
array('product_code' => 'battle-for-zendikar-6', 'product' => 'battle-for-zendikar-6'),
array('product_code' => 'battle-for-zendikar-7', 'product' => 'battle-for-zendikar-7'),
array('product_code' => 'battle-for-zendikar-8', 'product' => 'battle-for-zendikar-8'),
array('product_code' => 'battle-for-zendikar-9', 'product' => 'battle-for-zendikar-9'),
array('product_code' => 'battle-for-zendikar-10', 'product' => 'battle-for-zendikar-10'),
array('product_code' => 'battle-for-zendikar-11', 'product' => 'battle-for-zendikar-11'),
array('product_code' => 'battle-for-zendikar-12', 'product' => 'battle-for-zendikar-12'),
array('product_code' => 'battle-for-zendikar-13', 'product' => 'battle-for-zendikar-13'),
array('product_code' => 'battle-for-zendikar-14', 'product' => 'battle-for-zendikar-14'),
array('product_code' => 'battle-for-zendikar-15', 'product' => 'battle-for-zendikar-15'),
array('product_code' => 'battle-for-zendikar-16', 'product' => 'battle-for-zendikar-16'),
array('product_code' => 'battle-for-zendikar-17', 'product' => 'battle-for-zendikar-17'),
array('product_code' => 'battle-for-zendikar-18', 'product' => 'battle-for-zendikar-18'),
array('product_code' => 'battle-for-zendikar-19', 'product' => 'battle-for-zendikar-19'),
array('product_code' => 'battle-for-zendikar-20', 'product' => 'battle-for-zendikar-20')
);
function sortProducts($products)
{
$_sorted = array();
$_unsortable = array();
$_numbers = array();
if (is_array($products) && count($products)) {
foreach ($products as $product) {
preg_match('/[0-9]+$/', $product['product_code'], $_numbers);
$_number = $_numbers[0];
$initial_code_part = substr($product['product_code'],0,strrpos($product['product_code'],$_number));
if (!empty($_number)) {
$_sorted[$initial_code_part][$_number] = $product;
continue;
}
$_unsortable[$product['product_code']] = $product;
}
if (count($_sorted) > 1) {
$_newSortedData = array();
foreach ($_sorted as $_sortKey => $_sortedData) {
if (!count($_newSorteddata)) {
$_newSortedData = $_sortedData;
} else {
$params = $_sorteddata;
array_shift($params,$_newSortedData);
call_user_func_array('array_push',$params);
}
}
$_sorted = $_newSortedData;
}
ksort($_sorted);
ksort($_unsortable);
if (!count($_sorted)) {
return $_unsortable;
}
return $_sorted + $_unsortable;
}
return $products;
}
var_dump(sortProducts($arr));
Enable javascript to submit You have javascript disabled. You will not be able to edit any code.
Output for git.master , git.master_jit , rfc.property-hooks Warning: Undefined variable $_newSorteddata in /in/C8c9p on line 62
Fatal error: Uncaught TypeError: count(): Argument #1 ($value) must be of type Countable|array, null given in /in/C8c9p:62
Stack trace:
#0 /in/C8c9p(84): sortProducts(Array)
#1 {main}
thrown in /in/C8c9p on line 62
Process exited with code 255 . This tab shows result from various feature-branches currently under review by the php developers. Contact me to have additional branches featured.
Active branches Archived branches Once feature-branches are merged or declined, they are no longer available. Their functionality (when merged) can be viewed from the main output page
preferences:dark mode live preview
46.89 ms | 401 KiB | 8 Q