3v4l.org

run code in 300+ PHP versions simultaneously
<?php function checkIfJobAlreadyExistsAndUpdateCsv(array $csvRows) { $thisJob = false; $existingJobsAndItsDetails = []; $items = []; foreach ($csvRows as $index => $row) { echo PHP_EOL . 'This is row 0 value: '. $row[0]; if ($row[0] === 'H') { $thisJob = false; echo PHP_EOL . 'This job number: ' . $thisJob; if ($row[1] == 'BB2') { $thisJob = $row[1]; $existingJobsAndItsDetails[$thisJob]= $thisJob; unset($csvRows[$index]); } } if ($row[0] === 'D' && $thisJob) { echo PHP_EOL . 'This item number: ' . $row[1]; $items[$thisJob][] = $row; unset($csvRows[$index]); } } echo PHP_EOL . 'Final Array: '; foreach ($existingJobsAndItsDetails as $jobId => $thisJob) { echo PHP_EOL . 'This job number: ' . $thisJob; if (isset($items[$thisJob])) { foreach ($items[$thisJob] as $itemRow) { $productCode = $itemRow[1]; echo PHP_EOL . print_r($productCode, 1); } } } return $existingJobsAndItsDetails; } $myArray = [['H','AA1 ','598.12'], ['D','itemA1','PRAWN: KING - BAP - RAW PEELED DEVEINED 16-20 (700GM NETT PACK)'], ['D','itemA2','HADDOCK: FILLET 220-280GM SKINNED (KG)'], ['D','itemA3','SEABASS: WILD - WHOLE 2-3KG (KG)'], ['D','itemA4','MACKEREL: FILLETED LARGE (KG)'], ['H','BB2','68.93'], ['D','itemB1','TUNA : SASHIMI GRADE - LOIN ( KG )'], ['H','CC3','8.5']]; echo print_r(checkIfJobAlreadyExistsAndUpdateCsv($myArray), 1);

preferences:
50.7 ms | 402 KiB | 5 Q