3v4l.org

run code in 200+ php & hhvm versions
Bugs & Features
<?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);
based on bfbHn
Output for 5.6.30, hhvm-3.18.5 - 3.22.0, 7.0.30 - 7.3.0rc2
This is row 0 value: H This job number: This is row 0 value: D This is row 0 value: D This is row 0 value: D This is row 0 value: D This is row 0 value: H This job number: This is row 0 value: D This item number: itemB1 This is row 0 value: H This job number: Final Array: This job number: BB2 itemB1Array ( [BB2] => BB2 )