PHP使用自定义键

时间:2018-03-04 16:09:25

标签: 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\",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.

1 个答案:

答案 0 :(得分:1)

将密钥存储在一个数组中,然后使用str_getcsv()将每个元素分解为一个数组,最后使用array_combine()将密钥和值配对:

array_walk($workArray, function(&$el) use($keys) {
    $values = str_getcsv($el);
    $el = array_combine($keys, $values);
});

Demo

请注意,每个元素都是passed by reference,以便修改每个元素本身而不是副本。

或者,更优雅一点,使用array_walk()将函数应用于数组中的每个元素。同样,元素通过引用传递,use()用于将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) 数组引入匿名函数的范围:

df1 = pd.read_csv("some_data1.csv")
df2 = pd.read_csv("some_data2.csv")
df3 = pd.read_csv("some_data3.csv")


# DF1
         CITY  COUNT        DAY  TIME_BIN
0     ATLANTA    514     Friday  19:30:00
1     ATLANTA    398     Monday  01:20:00
2     CHICAGO    591   Saturday  12:20:00
3     ATLANTA    339     Friday  21:10:00

# DF2
         CITY  COUNT        DAY  TIME_BIN
0      DENVER     98  Wednesday  18:10:00
1       MIAMI    666  Wednesday  13:20:00
2     CHICAGO    165   Saturday  12:40:00

# DF3
         CITY  COUNT        DAY  TIME_BIN
0     CHICAGO    457   Thursday  06:00:00
1     CHICAGO    590   Saturday  19:00:00
2     PHOENIX    301   Saturday  20:30:00

结果

         CITY  COUNT        DAY  TIME_BIN
0     ATLANTA    514     Friday  19:30:00
1     ATLANTA    398     Monday  01:20:00
2     CHICAGO    591   Saturday  12:20:00
3     ATLANTA    339     Friday  21:10:00
4      DENVER     98  Wednesday  18:10:00
5       MIAMI    666  Wednesday  13:20:00
6     CHICAGO    165   Saturday  12:40:00
7     CHICAGO    457   Thursday  06:00:00
8     CHICAGO    590   Saturday  19:00:00
9     PHOENIX    301   Saturday  20:30:00