Convert String to Array in php? -
i want convert string array in php, not cant, me, please ?
$str = "a,b battery_counter,step1 battery_explanation,step2 calc_coupon,step3 broadband_standard,step4";
i want return:
return array( 'a' => 'b', 'battery_counter' => 'step1', 'battery_explanation' => 'step2', 'calc_coupon' => 'step3', 'broadband_standard' => 'step4' )
without knowing more limits of software:
$return_array = array(); $exploded_var = explode("\n",$str); foreach($exploded_var $pair_var){ $tmp = explode(",",$pair_var); $return_array[$tmp[0]] = $tmp[1]; } return $return_array;
you need explode string twice, once "pair strings" (e.g., "a,b") , second time split pair strings apart.
warning: solution 100% specific how presented $str
variable. not expandable arbitrary string.
Comments
Post a Comment