Php 在数组中分隔$value以创建$key=>$值数组
我可能对标题不够清楚,但这是我的问题 我有一根这样的绳子Php 在数组中分隔$value以创建$key=>$值数组,php,arrays,key,explode,Php,Arrays,Key,Explode,我可能对标题不够清楚,但这是我的问题 我有一根这样的绳子 $chain = "id:20604#*#user_id:32444#*#session_id:0#*#version:142#*#modified:1438610605#*#name:recrutement#*#push:0#*#last_push_execution:0#*#allowempty:1"; array:9 [ "id" => "20604" "user_id" => "32444" "sessi
$chain = "id:20604#*#user_id:32444#*#session_id:0#*#version:142#*#modified:1438610605#*#name:recrutement#*#push:0#*#last_push_execution:0#*#allowempty:1";
array:9 [
"id" => "20604"
"user_id" => "32444"
"session_id" => "0"
"version" => "142"
"modified" => "1438610605"
"name" => "recrutement"
"push"=> "0"
"last_push_execution"=> "0"
]
我做了一个分解(“#*#”,$chain)代码>
现在我有了这个:
array:9 [
0 => "id:20604"
1 => "user_id:32444"
2 => "session_id:0"
3 => "version:142"
4 => "modified:1438610605"
5 => "name:recrutement"
6 => "push:0"
7 => "last_push_execution:0"
8
]
但我想要这样的东西
$chain = "id:20604#*#user_id:32444#*#session_id:0#*#version:142#*#modified:1438610605#*#name:recrutement#*#push:0#*#last_push_execution:0#*#allowempty:1";
array:9 [
"id" => "20604"
"user_id" => "32444"
"session_id" => "0"
"version" => "142"
"modified" => "1438610605"
"name" => "recrutement"
"push"=> "0"
"last_push_execution"=> "0"
]
有人能告诉我怎么做吗
谢谢您可以使用PHP的以下方法来实现:
$arr = [
0 => "id:20604",
1 => "user_id:32444",
2 => "session_id:0",
3 => "version:142",
4 => "modified:1438610605",
5 => "name:recrutement",
6 => "push:0",
7 => "last_push_execution:0",
];
$final_arr = [];
foreach ($arr as $key => $val) {
$a = explode(':', $val);
$final_arr[$a[0]] = $a[1];
}
最后的产出将是:
$final_arr = array:8 [
"id" => "20604"
"user_id" => "32444"
"session_id" => "0"
"version" => "142"
"modified" => "1438610605"
"name" => "recrutement"
"push" => "0"
"last_push_execution" => "0"
]
希望这有帮助 您可以使用PHP的如下方法:
$arr = [
0 => "id:20604",
1 => "user_id:32444",
2 => "session_id:0",
3 => "version:142",
4 => "modified:1438610605",
5 => "name:recrutement",
6 => "push:0",
7 => "last_push_execution:0",
];
$final_arr = [];
foreach ($arr as $key => $val) {
$a = explode(':', $val);
$final_arr[$a[0]] = $a[1];
}
最后的产出将是:
$final_arr = array:8 [
"id" => "20604"
"user_id" => "32444"
"session_id" => "0"
"version" => "142"
"modified" => "1438610605"
"name" => "recrutement"
"push" => "0"
"last_push_execution" => "0"
]
希望这有帮助 虽然array\u map()
是一个更优雅的解决方案,但这是一个替代方案:
$outArray = array();
$tempArray = explode("#*#", $chain);
foreach ($tempArray as $chainValue) {
$split = explode(':',$chainValue);
$key = $split[0];
$value = $split[1];
$outArray[$key] = $value;
}
$outArray=array();
$tempArray=explode(“#*#”,$chain);
foreach($tempArray作为$chainValue){
$split=explode(“:”,$chainValue);
$key=$split[0];
$value=$split[1];
$outArray[$key]=$value;
}
虽然array\u map()
是一种更优雅的解决方案,但这是一种替代方案:
$outArray = array();
$tempArray = explode("#*#", $chain);
foreach ($tempArray as $chainValue) {
$split = explode(':',$chainValue);
$key = $split[0];
$value = $split[1];
$outArray[$key] = $value;
}
$outArray=array();
$tempArray=explode(“#*#”,$chain);
foreach($tempArray作为$chainValue){
$split=explode(“:”,$chainValue);
$key=$split[0];
$value=$split[1];
$outArray[$key]=$value;
}
1) 使用explode
功能的简单解决方案:
$chain = "id:20604#*#user_id:32444#*#session_id:0#*#version:142#*#modified:1438610605#*#name:recrutement#*#push:0#*#last_push_execution:0#*#allowempty:1";
$result = [];
foreach (explode("#*#", $chain) as $c) {
$pair = explode(":", $c);
$result[$pair[0]] = $pair[1];
}
2) 使用preg\u match\u all
和array\u组合功能的替代解决方案:
$chain = "id:20604#*#user_id:32444#*#session_id:0#*#version:142#*#modified:1438610605#*#name:recrutement#*#push:0#*#last_push_execution:0#*#allowempty:1";
preg_match_all("/\b(\w+):(\w+)\b/", $chain, $matches);
$result = array_combine($matches[1], $matches[2]);
这两种方法都将使用explode
函数提供所需的结果1)简单的解决方案:
$chain = "id:20604#*#user_id:32444#*#session_id:0#*#version:142#*#modified:1438610605#*#name:recrutement#*#push:0#*#last_push_execution:0#*#allowempty:1";
$result = [];
foreach (explode("#*#", $chain) as $c) {
$pair = explode(":", $c);
$result[$pair[0]] = $pair[1];
}
2) 使用preg\u match\u all
和array\u组合功能的替代解决方案:
$chain = "id:20604#*#user_id:32444#*#session_id:0#*#version:142#*#modified:1438610605#*#name:recrutement#*#push:0#*#last_push_execution:0#*#allowempty:1";
preg_match_all("/\b(\w+):(\w+)\b/", $chain, $matches);
$result = array_combine($matches[1], $matches[2]);
这两种方法都将产生所需的结果通往罗马的一条路
$final=array();
array_map(
function($a) use (&$final){
list($k,$v)=explode(':',$a);
$final[$k]=$v;
},
explode("#*#", $chain)
);
var_export($final);
单程去罗马
$final=array();
array_map(
function($a) use (&$final){
list($k,$v)=explode(':',$a);
$final[$k]=$v;
},
explode("#*#", $chain)
);
var_export($final);
您应该使用标准化的数据格式,如json或serialize。然后,您不必手动解析它,也不必考虑所有可能带来的问题。您应该使用标准化的数据格式,如json或serialize。然后,您就不必手动解析它,因为它会带来所有潜在的问题。谢谢您的ReplyTanks for您的ReplyTanks for您的ReplyTanks for您的reply@BarbeBleue,你是welcome@BarbeBleue,不客气