3v4l.org

run code in 200+ php & hhvm versions
Bugs & Features
<?php //$str = file_get_contents("file.csv"); $str = "sku,is_in_stock,warehouse_3,warehouse_4 AP-STYLUS,0,20,5 RC-3049,0,0,0 NFNC-FLAT-CAP,0,20,20 NFNC-HOOD14-ZIP-S,1,0,5"; $arr = explode("\n", $str); $result = array(); Foreach($arr as $line){ $linearr = explode(",", $line); If(is_numeric($linearr[2])){ if($linearr[2]+$linearr[3]>=1){ $linearr[1]="1"; $line = implode("," , $linearr); }else{ $linearr[1]="0"; $line = implode("," , $linearr); } } $result[]=$line; } $newstr = implode("\n", $result); //put_file_contents("file.csv", $newstr); Echo $newstr;
based on 4NbLS
Output for 5.5.0 - 7.2.6
sku,is_in_stock,warehouse_3,warehouse_4 AP-STYLUS,1,20,5 RC-3049,0,0,0 NFNC-FLAT-CAP,1,20,20 NFNC-HOOD14-ZIP-S,1,0,5