3v4l.org

run code in 200+ php & hhvm versions
Bugs & Features
<?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); var_dump($params); 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));
based on r7k5v
Output for 7.2.0
Notice: Undefined variable: _newSorteddata in /in/k01n4 on line 62 Warning: count(): Parameter must be an array or an object that implements Countable in /in/k01n4 on line 62 Notice: Undefined variable: _newSorteddata in /in/k01n4 on line 62 Warning: count(): Parameter must be an array or an object that implements Countable in /in/k01n4 on line 62 array(20) { [1]=> array(2) { ["product_code"]=> string(21) "battle-for-zendikar-1" ["product"]=> string(21) "battle-for-zendikar-1" } [2]=> array(2) { ["product_code"]=> string(21) "battle-for-zendikar-2" ["product"]=> string(21) "battle-for-zendikar-2" } [3]=> array(2) { ["product_code"]=> string(21) "battle-for-zendikar-3" ["product"]=> string(21) "battle-for-zendikar-3" } [4]=> array(2) { ["product_code"]=> string(21) "battle-for-zendikar-4" ["product"]=> string(21) "battle-for-zendikar-4" } [5]=> array(2) { ["product_code"]=> string(21) "battle-for-zendikar-5" ["product"]=> string(21) "battle-for-zendikar-5" } [6]=> array(2) { ["product_code"]=> string(21) "battle-for-zendikar-6" ["product"]=> string(21) "battle-for-zendikar-6" } [7]=> array(2) { ["product_code"]=> string(21) "battle-for-zendikar-7" ["product"]=> string(21) "battle-for-zendikar-7" } [8]=> array(2) { ["product_code"]=> string(21) "battle-for-zendikar-8" ["product"]=> string(21) "battle-for-zendikar-8" } [9]=> array(2) { ["product_code"]=> string(21) "battle-for-zendikar-9" ["product"]=> string(21) "battle-for-zendikar-9" } [10]=> array(2) { ["product_code"]=> string(22) "battle-for-zendikar-10" ["product"]=> string(22) "battle-for-zendikar-10" } [11]=> array(2) { ["product_code"]=> string(22) "battle-for-zendikar-11" ["product"]=> string(22) "battle-for-zendikar-11" } [12]=> array(2) { ["product_code"]=> string(22) "battle-for-zendikar-12" ["product"]=> string(22) "battle-for-zendikar-12" } [13]=> array(2) { ["product_code"]=> string(22) "battle-for-zendikar-13" ["product"]=> string(22) "battle-for-zendikar-13" } [14]=> array(2) { ["product_code"]=> string(22) "battle-for-zendikar-14" ["product"]=> string(22) "battle-for-zendikar-14" } [15]=> array(2) { ["product_code"]=> string(22) "battle-for-zendikar-15" ["product"]=> string(22) "battle-for-zendikar-15" } [16]=> array(2) { ["product_code"]=> string(22) "battle-for-zendikar-16" ["product"]=> string(22) "battle-for-zendikar-16" } [17]=> array(2) { ["product_code"]=> string(22) "battle-for-zendikar-17" ["product"]=> string(22) "battle-for-zendikar-17" } [18]=> array(2) { ["product_code"]=> string(22) "battle-for-zendikar-18" ["product"]=> string(22) "battle-for-zendikar-18" } [19]=> array(2) { ["product_code"]=> string(22) "battle-for-zendikar-19" ["product"]=> string(22) "battle-for-zendikar-19" } [20]=> array(2) { ["product_code"]=> string(22) "battle-for-zendikar-20" ["product"]=> string(22) "battle-for-zendikar-20" } }
Output for 5.3.18 - 7.1.10
Notice: Undefined variable: _newSorteddata in /in/k01n4 on line 62 Notice: Undefined variable: _newSorteddata in /in/k01n4 on line 62 array(20) { [1]=> array(2) { ["product_code"]=> string(21) "battle-for-zendikar-1" ["product"]=> string(21) "battle-for-zendikar-1" } [2]=> array(2) { ["product_code"]=> string(21) "battle-for-zendikar-2" ["product"]=> string(21) "battle-for-zendikar-2" } [3]=> array(2) { ["product_code"]=> string(21) "battle-for-zendikar-3" ["product"]=> string(21) "battle-for-zendikar-3" } [4]=> array(2) { ["product_code"]=> string(21) "battle-for-zendikar-4" ["product"]=> string(21) "battle-for-zendikar-4" } [5]=> array(2) { ["product_code"]=> string(21) "battle-for-zendikar-5" ["product"]=> string(21) "battle-for-zendikar-5" } [6]=> array(2) { ["product_code"]=> string(21) "battle-for-zendikar-6" ["product"]=> string(21) "battle-for-zendikar-6" } [7]=> array(2) { ["product_code"]=> string(21) "battle-for-zendikar-7" ["product"]=> string(21) "battle-for-zendikar-7" } [8]=> array(2) { ["product_code"]=> string(21) "battle-for-zendikar-8" ["product"]=> string(21) "battle-for-zendikar-8" } [9]=> array(2) { ["product_code"]=> string(21) "battle-for-zendikar-9" ["product"]=> string(21) "battle-for-zendikar-9" } [10]=> array(2) { ["product_code"]=> string(22) "battle-for-zendikar-10" ["product"]=> string(22) "battle-for-zendikar-10" } [11]=> array(2) { ["product_code"]=> string(22) "battle-for-zendikar-11" ["product"]=> string(22) "battle-for-zendikar-11" } [12]=> array(2) { ["product_code"]=> string(22) "battle-for-zendikar-12" ["product"]=> string(22) "battle-for-zendikar-12" } [13]=> array(2) { ["product_code"]=> string(22) "battle-for-zendikar-13" ["product"]=> string(22) "battle-for-zendikar-13" } [14]=> array(2) { ["product_code"]=> string(22) "battle-for-zendikar-14" ["product"]=> string(22) "battle-for-zendikar-14" } [15]=> array(2) { ["product_code"]=> string(22) "battle-for-zendikar-15" ["product"]=> string(22) "battle-for-zendikar-15" } [16]=> array(2) { ["product_code"]=> string(22) "battle-for-zendikar-16" ["product"]=> string(22) "battle-for-zendikar-16" } [17]=> array(2) { ["product_code"]=> string(22) "battle-for-zendikar-17" ["product"]=> string(22) "battle-for-zendikar-17" } [18]=> array(2) { ["product_code"]=> string(22) "battle-for-zendikar-18" ["product"]=> string(22) "battle-for-zendikar-18" } [19]=> array(2) { ["product_code"]=> string(22) "battle-for-zendikar-19" ["product"]=> string(22) "battle-for-zendikar-19" } [20]=> array(2) { ["product_code"]=> string(22) "battle-for-zendikar-20" ["product"]=> string(22) "battle-for-zendikar-20" } }