Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/256.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php 重新创建可读数组_Php_Arrays - Fatal编程技术网

Php 重新创建可读数组

Php 重新创建可读数组,php,arrays,Php,Arrays,我有一个数组,通过CSV显示如下: Array ( [0] => Array ( [0] => Agent Name [1] => Total Calls [2] => Customer Services [3] => Voicemail ) [1] => Array (

我有一个数组,通过CSV显示如下:

Array
(
    [0] => Array
        (
            [0] => Agent Name
            [1] => Total Calls
            [2] => Customer Services 
            [3] => Voicemail 
        )

    [1] => Array
        (
            [0] => Paul
            [1] => 53
            [2] => 0
            [3] => 0
        )

    [2] => Array
        (
            [0] => Sarah
            [1] => 51
            [2] => 0
            [3] => 0
        )
)
我想整理一个数组,使它更可读和易于使用

我希望数组看起来像这样:

Array
(
    [Paul] => Array
        (
            [Agent Name] => Paul
            [Total Calls] => 53
            [Customer Services ] => 30
            [Voicemail ] => 0
        )

    [Sarah] => Array
        (
            [Agent Name] => Sarah
            [Total Calls] => 51
            [Customer Services ] => 0
            [Voicemail ] => 0
        )

)
$f = array_shift($report);

$a = array_combine(array_column($t = array_map(function ($r) use ($f)
{
  return array_combine($f, $r);
}
, $report), 'Agent Name'), $t);
我已经提出了这个解决方案,它可以工作:

$fields = $report[0];
array_shift($report);

$data = array();

foreach($report as $row) {
      $data[$row[0]] = array();
      foreach($row as $k => $val) {
        $data[$row[0]][$fields[$k]] = $val;
      }
}

return $data;
有没有更好的短一点的方法呢?

试试看

$result =array();
for($i=1;$i<count($arr);$i++){
   $result[$arr[$i][0]] = array_combine(array_values($arr[0]),$arr[$i]);
 }
$result=array();

对于($i=1;$i您也许可以这样做:

Array
(
    [Paul] => Array
        (
            [Agent Name] => Paul
            [Total Calls] => 53
            [Customer Services ] => 30
            [Voicemail ] => 0
        )

    [Sarah] => Array
        (
            [Agent Name] => Sarah
            [Total Calls] => 51
            [Customer Services ] => 0
            [Voicemail ] => 0
        )

)
$f = array_shift($report);

$a = array_combine(array_column($t = array_map(function ($r) use ($f)
{
  return array_combine($f, $r);
}
, $report), 'Agent Name'), $t);

定义你所说的更好。你的意思是更高效吗?你的意思是更可读吗?你的意思是其他吗?@crush我刚刚完成的php代码,有没有其他方法可以让它更短,更可读。常见的是,你有一个5行的解决方案,你想要“更好”.Why?不管怎样,堆栈溢出的主题已关闭。请尝试将前两行合并为一行:
$fields=array\u shift($report);