3v4l.org

run code in 500+ PHP versions simultaneously
<?php $products = [ [ 'item_number' => 15678, 'description' => 'Tophat', 'size' => 'small', 'shelf' => 5, 'aisle' => 12, 'quantity' => 10, 'price' => 5.99 ], [ 'item_number' => 15644, 'description' => 'Something Else', 'size' => 'Large', 'shelf' => 6, 'aisle' => 10, 'quantity' => 99, 'price' => 8.99 ], [ 'item_number' => 15662, 'description' => 'T-shirt', 'size' => 'large', 'shelf' => 9, 'aisle' => 13, 'quantity' => 15, 'price' => null ] ]; function is_valid($product): bool { foreach ($product as $value) { if (!is_int($value) && !is_string($value) && !is_float($value)) { return false; } } return true; } function create_inventory(array $products): array { $inventory = []; // loop over your product array using the index to create an array if the product is valid. foreach ($products as $index => $product) { if (is_valid($product)) { // Create an empty array when the product is valid. $inventory[$index] = []; // Loop over the product values and add them to the newly created array. foreach ($product as $key => $value) { $inventory[$index][$key] = $value; } } } return $inventory; } var_dump(create_inventory($products));
Output for 7.4.0 - 7.4.33, 8.0.1 - 8.0.30, 8.1.0 - 8.1.34, 8.2.0 - 8.2.30, 8.3.0 - 8.3.30, 8.4.1 - 8.4.21, 8.5.0 - 8.5.7
array(2) { [0]=> array(7) { ["item_number"]=> int(15678) ["description"]=> string(6) "Tophat" ["size"]=> string(5) "small" ["shelf"]=> int(5) ["aisle"]=> int(12) ["quantity"]=> int(10) ["price"]=> float(5.99) } [1]=> array(7) { ["item_number"]=> int(15644) ["description"]=> string(14) "Something Else" ["size"]=> string(5) "Large" ["shelf"]=> int(6) ["aisle"]=> int(10) ["quantity"]=> int(99) ["price"]=> float(8.99) } }

preferences:
109.54 ms | 1361 KiB | 4 Q