繁体   English   中英

PHP-重新排列多维数组以使值成为关键

[英]PHP - Rearrange multi-dimensional array to value become key

使用此PHP代码:

 function parseCSV($csvfile) {
        $csv = array();
        $rowcount = 0;
        if (($handle = fopen($csvfile, "r")) !== FALSE) {
            $max_line_length = defined('MAX_LINE_LENGTH') ? MAX_LINE_LENGTH : 10000;
            $header = fgetcsv($handle, $max_line_length);
            $header_colcount = count($header);
            while (($row = fgetcsv($handle, $max_line_length)) !== FALSE) {
                $row_colcount = count($row);
                if ($row_colcount == $header_colcount) {
                    $entry = array_combine($header, $row);
                    $csv[] = $entry;
                }
                else {
                    error_log("csvreader: Invalid number of columns at line " . ($rowcount + 2) . " (row " . ($rowcount + 1) . "). Expected=$header_colcount Got=$row_colcount");
                    return null;
                }
                $rowcount++;
            }
            fclose($handle);
        }
        else {
            error_log("csvreader: Could not read CSV \"$csvfile\"");
            return null;
        }

        return $csv;
    }

解析CSV并获取此值,这真的是件好事:

array(2) {
  [0]=>
  array(6) {
    ["keyword"]=>
    string(7) "TEST"
    ["tag1"]=>
    string(5) "TEST1"
    ["tag2"]=>
    string(5) "TEST2"
    ["tag3"]=>
    string(5) "TEST3"
    ["tag4"]=>
    string(5) "TEST4"
    ["tag5"]=>
    string(5) "TEST5"
  }
  [1]=>
  array(6) {
    ["keyword"]=>
    string(9) "BadCredit"
    ["tag1"]=>
    string(7) "Credit1"
    ["tag2"]=>
    string(7) "Credit2"
    ["tag3"]=>
    string(7) "Credit3"
    ["tag4"]=>
    string(7) "Credit4"
    ["tag5"]=>
    string(7) "Credit5"
  }
}

现在,我需要在代码的第二部分中重新排序该值,以使其看起来像这样:

array(2) {
  ["TEST"]=>
  array(5) {
    ["tag1"]=>
    string(5) "TEST1"
    ["tag2"]=>
    string(5) "TEST2"
    ["tag3"]=>
    string(5) "TEST3"
    ["tag4"]=>
    string(5) "TEST4"
    ["tag5"]=>
    string(5) "TEST5"
  }
  ["BadCredit"]=>
  array(5) {
    ["tag1"]=>
    string(7) "Credit1"
    ["tag2"]=>
    string(7) "Credit2"
    ["tag3"]=>
    string(7) "Credit3"
    ["tag4"]=>
    string(7) "Credit4"
    ["tag5"]=>
    string(7) "Credit5"
  }
}

我需要获取“关键字”值,并将其放置在数组的键之类的位置,以使其他对象可以成为代码的一部分。 怎么做?

这应该做。

foreach ($csv as $item) {
    $new_array[$item['keyword']] = array_diff_key($item, ['keyword' => 0]);
}

array_diff_key将使用keyword作为关键字将关键字附加到新数组之前,从每个元素中删除关键字。

使用array_reduce函数:

// $array with values

$newArray = array_reduce($array, function($carry, $item) {
    $keyword = $item['keyword'];
    unset($item['keyword']);
    $carry[$keyword] = $item;
    return $carry;
}, array());

例如,尝试下面的代码:

// sample code

$array = array(
  array(
      "keyword"=> "TEST",
      "tag1"=>"TEST1"
  ),
  array(
      "keyword" => "BadCredit",
        "tag1"=> "Credit1"
    )
);


$newArray = array_reduce($array, function($carry, $item) {
    $keyword = $item['keyword'];
    unset($item['keyword']);
    $carry[$keyword] = $item;
    return $carry;
}, array());

输出将是:

array(2) {
  ["TEST"]=>
  array(1) {
    ["tag1"]=>
    string(5) "TEST1"
  }
  ["BadCredit"]=>
  array(1) {
    ["tag1"]=>
    string(7) "Credit1"
  }
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM