Last active
June 30, 2017 00:40
-
-
Save neatstudio/4405542 to your computer and use it in GitHub Desktop.
explode string to array
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
$str = "101|1;0|4;1|0;0|0;0|0"; | |
//第一种 | |
$array = explode(';', $str); | |
$items = []; | |
foreach($array as $v){ | |
list($k, $v) = explode('|', $v); | |
if(empty($v)){ | |
continue; | |
} | |
$items[$v] = $k; | |
} | |
echo "<pre>"; | |
print_r($items); | |
echo "</pre>"; | |
//第二种 | |
parse_str(preg_replace(['/\d+\|0/', '/;/', '/\|/'], ["", "&", "="], $str), $result); | |
echo "<pre>"; | |
print_r($result); | |
echo "</pre>"; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment