how to format array from string in php? -
i want if first number in string 2 output 2 array. how explode each array string.
my code
<?php $str = "2,2;2;,1;1;,07-09-2016;07-09-2016;,08-09-2016;10-09-2016;,1;3;,100.00;450.00;"; $data = explode(',',$str); $out = array(); for($i=1;$i < count($data)-1;$i++){ $out[]= explode(';',$data[$i]); } $i = $out[0][0]; foreach ($out $key => $value) { for($a=0;$a < $i; $a++){ echo $value[$a]. "<br/>"; } } ?>
i result 221107-09-201607-09-201608-09-201610-09-201613 want format
<?php $str = "2,2;2;,1;1;,07-09-2016;07-09-2016;,08-09-2016;10-09-2016;,1;3;,100.00;450.00;"; //format split semicomma ; $arr1 = array('2','1','07-09-2016','08-09-2016','1','100.00'); $arr2 = array('2','1','07-09-2016','10-09-2016','3','450.00'); ?>
the php function array_column
come in handy here. here short code example should output looking for.
<?php //your original input $str = "2,2;2;,1;1;,07-09-2016;07-09-2016;,08-09-2016;10-09-2016;,1;3;,100.00;450.00"; //explode array sub-arrays $arrs = explode(",", $str); //remove first element sets how many elements in each array $numarrs = array_shift($arrs); //convert strings wanted sub-arrays array_walk($arrs, function(&$val, $key) { $val = explode(';',$val); }); //make answer need $ans = array(); for($i=0; $i<$numarrs; $i++) { //array_column work want, making life easy $ans[] = array_column($arrs, $i); } var_dump($ans);
this process assume string formatted looking - fail horribly if not case.
Comments
Post a Comment