Skip to content

Instantly share code, notes, and snippets.

@neatstudio
Last active June 30, 2017 00:40
Show Gist options
  • Save neatstudio/4405542 to your computer and use it in GitHub Desktop.
Save neatstudio/4405542 to your computer and use it in GitHub Desktop.
explode string to array
<?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