PHP使用自定义键将字符串数组转换为二维数组

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了PHP使用自定义键将字符串数组转换为二维数组相关的知识,希望对你有一定的参考价值。

我是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.
答案

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

<?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);

Demo

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

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

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)

以上是关于PHP使用自定义键将字符串数组转换为二维数组的主要内容,如果未能解决你的问题,请参考以下文章

php将一维数组转换为字符串并自定义间隔符号

PHP二维数组转换成HTML

php 二维数组自定义排序

PHP二维数组如何去掉重复值

js如何定义二维数组,一维的key为动态值。就是要处理二级菜单显示。

c语言二维数组里的数据如何让两行字符转换为一行字符