PHP: Exploding array elements -
i have 2d array have exploded string. once has exploded output:
---> 0 - 16~4~0.0~~~~false~~~~ ---> 1 - 1000.0~21.75~l~1~2.0~2.0~l~2~ ---> 2 - ---> 0 - 2~5~951.3~6.4~~~false~~~~ ---> 1 - 1000.0~11.77~l~1~ ---> 2 - ---> 0 - 3~6~1269.02~5.1~~~false~~~~ ---> 1 - 5.0~213.66~l~1~4.9~2.56~l~2~4.6~19.5~l~3~ ---> 2 - 5.1~53.44~b~1~5.4~8.48~b~2~5.5~15.53~b~3~
i want make each position in array takes first value before ~. unsure how this. code have far:
$test = explode(":", $string); foreach($test &$value) $value = explode('|', $value);
just in case need original string input:
1~1~828.32~12.5~~~false~~~~|1000.0~41.73~l~1~2.0~2.0~l~2~|:4~2~4.16~12.5~~~false~~~~|1000.0~21.75~l~1~2.0~2.0~l~2~|:9~3~0.16~24.0~~~false~~~~|1000.0~21.75~l~1~2.0~2.0~l~2~|:16~4~0.0~~~~false~~~~|1000.0~21.75~l~1~2.0~2.0~l~2~|:2~5~951.3~6.4~~~false~~~~|1000.0~11.77~l~1~|:3~6~1269.02~5.1~~~false~~~~|5.0~213.66~l~1~4.9~2.56~l~2~4.6~19.5~l~3~|5.1~53.44~b~1~5.4~8.48~b~2~5.5~15.53~b~3~:8~7~111.92~7.0~~~false~~~~|6.8~6.78~l~1~6.6~148.39~l~2~6.4~3.7~l~3~|7.6~128.0'...
i output be:
---> 0 - 16 ---> 1 - 1000.0 ---> 2 - ---> 0 - 2 ---> 1 - 1000.0 ---> 2 - ---> 0 - 3 ---> 1 - 5.0 ---> 2 - 5.1
if understand correctly, want take each element of array , trim off after first ~ character. building on code:
$test = explode(":", $string); foreach($test &$value) { $value = explode('|', $value); foreach($value &$inner_value) { $inner_value = substr($inner_value, 0, strpos($inner_value, '~')); } }
all added inner foreach loop inspects each value , removes rest of string after ~ character.
best of coding!
Comments
Post a Comment