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

时间:2016-03-15 21:26:23

标签: php arrays

使用此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"
  }
}

我需要获得"关键字"像数组到泡沫的键的值和位置可以为代码的其他部分创建对象。怎么做?

2 个答案:

答案 0 :(得分:2)

这应该这样做。

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

array_diff_key会删除每个元素中的关键字,然后使用keyword作为关键字将其附加到新数组。

答案 1 :(得分:1)

使用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"
  }
}