簡體   English   中英

PHP 將字符串數組轉換為帶有自定義鍵的二維數組

[英]PHP converting an array of strings to a two-dimensional array with custom keys

我是 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);

演示

請注意,每個元素都是通過引用傳遞的,以便修改每個元素本身而不是副本。

或者,更優雅一點,使用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)

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM