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));

preferences:
102.13 ms | 1360 KiB | 5 Q