PHP使用自定义键将字符串数组转换为二维数组
我是PHP新手,必须将从文本文件读取的JS数组中的数据转换为PHP数组。 到目前为止,在文件读取和一些“清理”和排序之后,我有以下字符串数组:PHP使用自定义键将字符串数组转换为二维数组,php,arrays,multidimensional-array,Php,Arrays,Multidimensional Array,我是PHP新手,必须将从文本文件读取的JS数组中的数据转换为PHP数组。 到目前为止,在文件读取和一些“清理”和排序之后,我有以下字符串数组: $workArray[0] = "\"20180125_0363\",\"363\",\"25.01.2018\",\"Some long text here\",false,\"\""; $workArray[1] = "\"20180125_0364\",\"364\",\"25.01.2018\",\"Some long text here\",t
$workArray[0] = "\"20180125_0363\",\"363\",\"25.01.2018\",\"Some long text here\",false,\"\"";
$workArray[1] = "\"20180125_0364\",\"364\",\"25.01.2018\",\"Some long text here\",true,\"Some short text here\"";
$workArray[2] = "\"20180125_0365\",\"365\",\"25.01.2018\",\"Some long text here\",true,\"Some short text here\"";
...
...
etc.
我需要以下任务的帮助:如何将$workArray转换为二维$dataArray数组,其元素是从上述字符串中提取的具有自定义键和值的数组
$dataArray[0] = array(
"uid" => "20180125_0363",
"number" => "363",
"date" => "25.01.2018",
"title" => "Some long text here",
"docFlag" => false,
"docTitle" => ""
);
$dataArray[1] = array(
"uid" => "20180125_0364",
"number" => "364",
"date" => "25.01.2018",
"title" => "Some long text here",
"docFlag" => true,
"docTitle" => "Some short text here"
);
$dataArray[2] = array(
"uid" => "20180125_0365",
"number" => "365",
"date" => "25.01.2018",
"title" => "Some long text here",
"docFlag" => true,
"docTitle" => "Some short text here"
);
...
...
etc.
将键存储在数组中,然后使用将每个元素分解为一个数组,最后使用将键与值配对:
<?php
$keys = [
"uid",
"number",
"date",
"title",
"docFlag",
"docTitle",
];
$workArray[0] = "\"20180125_0364\",\"363\",\"25.01.2018\",\"Some long text here\",false,\"\"";
$workArray[1] = "\"20180125_0363\",\"364\",\"25.01.2018\",\"Some long text here\",true,\"Some short text here\"";
$workArray[2] = "\"20180125_0358\",\"365\",\"25.01.2018\",\"Some long text here\",true,\"Some short text here\"";
foreach ($workArray as &$el) {
$values = str_getcsv($el);
$el = array_combine($keys, $values);
}
var_dump($workArray);
结果
你是自己试的还是请别人帮你做的?作为提示,您可以使用
for
和foreach
迭代数组。@MikeVelazco我在过去4小时内尝试了几种不同的方法,但没有成功。我需要一个正确方向的指导,而不是有人帮我。
array_walk($workArray, function(&$el) use($keys) {
$values = str_getcsv($el);
$el = array_combine($keys, $values);
});
array (size=3)
0 =>
array (size=6)
'uid' => string '20180125_0364' (length=13)
'number' => string '363' (length=3)
'date' => string '25.01.2018' (length=10)
'title' => string 'Some long text here' (length=19)
'docFlag' => string 'false' (length=5)
'docTitle' => string '' (length=0)
1 =>
array (size=6)
'uid' => string '20180125_0363' (length=13)
'number' => string '364' (length=3)
'date' => string '25.01.2018' (length=10)
'title' => string 'Some long text here' (length=19)
'docFlag' => string 'true' (length=4)
'docTitle' => string 'Some short text here' (length=20)
2 =>
array (size=6)
'uid' => string '20180125_0358' (length=13)
'number' => string '365' (length=3)
'date' => string '25.01.2018' (length=10)
'title' => string 'Some long text here' (length=19)
'docFlag' => string 'true' (length=4)
'docTitle' => string 'Some short text here' (length=20)