php - Foreach cycle return randly changed values -
i have .ini file can't change. looks like:
01code6=77 01name6=g 650 xcountry/xmoto/xchallenge (k15) [v] [77] 01type6=bike 01code7=e3 01name7=i3 od 07/13 (i01) [v] [s] [3d] [ire] [e3] 01type7=car 01code8=e8 01name8=i8 kupé od 03/14 (i12) [v] [3d] [e8] 01type8=car 01code9=80 01name9=k 100 - k 1200 rs / k1 od 04/84 [v] [80] 01type9=bike
and code parse looks like:
public function pasrsestring($path) { $ini = parse_ini_file($path, false, ini_scanner_raw); foreach(array_chunk($ini, 3, true) $data) { // $data array of 3 related $mcode = substr(array_keys($data)[0], 0, 2); $nameline = array_values($data)[1]; $typeline = array_values($data)[2]; $vehicle = array_values($data)[0]; echo $vehicle; echo "<br>"; echo $typeline; echo "<br>"; echo $nameline; echo "<br>"; echo $mcode; } }
as can see in .ini files tehre 3 lines 1 vehicle,
- first line code
- second line name
- third line vehicle_type
my script can correct values dispite fact cycle fail somehow because there wrong values in variabiles. example if run cycle on whole file echo $vehicle vehicles return vehicle data, vehicles return vehicle type, return mcode, dispite fact in file there order this.
i confuse in doing wrong in cycle, can find solution?
p.s. order of values changing if use bigger or smaller value in foreach(array_chunk($ini, 3, true) $data)
edit: found out there blanked data in code, example
01code0= 01name0=
so example code blanked values can looks like:
00code46=65 00name46=tt kupé od 07/14 (fv) [v] [s] [3d] [65] 00type46=car 00code47=45 00name47=tt od 08/06 (8j) [v] [s] [3d] [ire] [45] 00type47=car 01code0= 01name0= 01code1=m2 01name1=c 600 sport/c 650 gt od 01/12 (k18/0131) (k19/0133) [v] [m2] 01type1=bike 01code2=70 01name2=f 650 gs / f 800 gs od mod. ´08 [v] [70] 01type2=bike 01code3=84
there 5 blanked values in whole document not many
a bit of php magic later:
$vehicles = array_map(function($vs){ return array_combine(array('code','name','type'),$vs); }, array_chunk(parse_ini_string($x,false,ini_scanner_raw),3));
parse ini, chunkify, map on chunks converting positions names
full code:
$x = <<<eod 01code6=77 01name6=g 650 xcountry/xmoto/xchallenge (k15) [v] [77] 01type6=bike 01code7=e3 01name7=i3 od 07/13 (i01) [v] [s] [3d] [ire] [e3] 01type7=car 01code8=e8 01name8=i8 kupé od 03/14 (i12) [v] [3d] [e8] 01type8=car 01code9=80 01name9=k 100 - k 1200 rs / k1 od 04/84 [v] [80] 01type9=bike eod; $vehicles = array_map(function($vs){ return array_combine(array('code','name','type'),$vs); }, array_chunk(parse_ini_string($x,false,ini_scanner_raw),3)); var_dump($vehicles); // on can iterate produce output /* array(4) { [0]=> array(3) { ["code"]=> string(2) "77" ["name"]=> string(46) "g 650 xcountry/xmoto/xchallenge (k15) [v] [77]" ["type"]=> string(4) "bike" } [1]=> array(3) { ["code"]=> string(2) "e3" ["name"]=> string(41) "i3 od 07/13 (i01) [v] [s] [3d] [ire] [e3]" ["type"]=> string(3) "car" } [2]=> array(3) { ["code"]=> string(2) "e8" ["name"]=> string(37) "i8 kupé od 03/14 (i12) [v] [3d] [e8]" ["type"]=> string(3) "car" } [3]=> array(3) { ["code"]=> string(2) "80" ["name"]=> string(40) "k 100 - k 1200 rs / k1 od 04/84 [v] [80]" ["type"]=> string(4) "bike" } } */
Comments
Post a Comment