Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/237.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,我有很多这样的记录: 2011-01-01, a, 1 2011-01-01, c, 5 2011-01-01, d, 3 2011-01-02, a, ... 第一个值是日期,第二个值是字符串(可以是a或b或c或d),第三个值是数字 如何在PHP中将所有记录添加到一个数组中,该数组的结构如下: array('2011-01-01' => array('a' => '1','b' => '0','c' => '5','d' => '3'), '2011-01-02

我有很多这样的记录:

2011-01-01, a, 1
2011-01-01, c, 5
2011-01-01, d, 3
2011-01-02, a, ...
第一个值是日期,第二个值是字符串(可以是
a
b
c
d
),第三个值是数字

如何在PHP中将所有记录添加到一个数组中,该数组的结构如下:

array('2011-01-01' => array('a' => '1','b' => '0','c' => '5','d' => '3'), '2011-01-02' => ... ,)

因此,日期变量是一个索引,键是一个四元素数组,每个元素都是相应的记录编号(第三个值)?

假设这是一个大字符串,将其在行上拆分,然后在逗号上拆分。对于每一行,测试日期是否已经作为键存在,如果已经存在,则添加到其中,否则,创建它:

$string = "2011-01-01, a, 1
2011-01-01, c, 5
2011-01-01, d, 3
2011-01-02, a, 1";

// Array to hold it all...
$array = array();
$lines = explode("\n", $string);
foreach ($lines as $line) {
  // Explode the line on the comma into 3 variables
  list($date, $key, $num) = explode(",", $line);
  $date = trim($date);
  $key = trim($key);
  $num = intval($num);

  // Create an array key for the current $date if it doesn't exist
  // as a new array
  if (!isset($array[$date])) $array[$date] = array();

  // And assign the number with the corresponding letter key to the array
  // for this date.
  $array[$date][$key] = $num;
}

假设这是一个大字符串,在行上拆分,然后在逗号上拆分。对于每一行,测试日期是否已经作为键存在,如果已经存在,则添加到其中,否则,创建它:

$string = "2011-01-01, a, 1
2011-01-01, c, 5
2011-01-01, d, 3
2011-01-02, a, 1";

// Array to hold it all...
$array = array();
$lines = explode("\n", $string);
foreach ($lines as $line) {
  // Explode the line on the comma into 3 variables
  list($date, $key, $num) = explode(",", $line);
  $date = trim($date);
  $key = trim($key);
  $num = intval($num);

  // Create an array key for the current $date if it doesn't exist
  // as a new array
  if (!isset($array[$date])) $array[$date] = array();

  // And assign the number with the corresponding letter key to the array
  // for this date.
  $array[$date][$key] = $num;
}
这样如何(假设输入数据的格式):

这样如何(假设输入数据的格式):


如果您的数据位于文本文件(.txt)中:

$records=file\u get\u contents(“filename.txt”);
//$records将等于以下值:(在windows上,它将是\r\n而不仅仅是\r\n)
//$records=“2011-01-01,a,1\n2011-01-01,c,5\n2011-01-01,d,3\n 2011-01-02,d,3”;
$records=explode(“\n”,$records);
$output=array();
对于($i=0;$i
如果您的数据位于文本文件(.txt)中:

$records=file\u get\u contents(“filename.txt”);
//$records将等于以下值:(在windows上,它将是\r\n而不仅仅是\r\n)
//$records=“2011-01-01,a,1\n2011-01-01,c,5\n2011-01-01,d,3\n 2011-01-02,d,3”;
$records=explode(“\n”,$records);
$output=array();
对于($i=0;$i
好的,下面是您要做的

$records = array(
    0 => array('2011-01-01', 'a', '1'),
    1 => array('2011-01-01', 'c', '5'),
    2 => array('2011-01-01', 'd', '3'),
    3 => array('2012-01-01', 'a', '1')
);

$dateArray = array();
$baseArray = array('a' => '0','b' => '0','c' => '0','d' = '0');

foreach($records as $record)
{
     $dateArray[$record[0]][$record[1]] = $record[2];  //You could actually do this while pulling in the info from the DB
}

foreach($dateArray as $date => $array)
{
     $missingArray = array_diff_key($array,$baseArray);  //Determine what keys are missing using the base array, get keys and default values
     $dateArray[$date] = array_merge($array,$missingArray);  //Merge the missing parts with the current array
}

请注意,部分内容是从其他帖子中复制的。

好的,下面是您要做的

$records = array(
    0 => array('2011-01-01', 'a', '1'),
    1 => array('2011-01-01', 'c', '5'),
    2 => array('2011-01-01', 'd', '3'),
    3 => array('2012-01-01', 'a', '1')
);

$dateArray = array();
$baseArray = array('a' => '0','b' => '0','c' => '0','d' = '0');

foreach($records as $record)
{
     $dateArray[$record[0]][$record[1]] = $record[2];  //You could actually do this while pulling in the info from the DB
}

foreach($dateArray as $date => $array)
{
     $missingArray = array_diff_key($array,$baseArray);  //Determine what keys are missing using the base array, get keys and default values
     $dateArray[$date] = array_merge($array,$missingArray);  //Merge the missing parts with the current array
}

注意,部分内容是从其他帖子复制的。

初始记录实际上是如何存储的?作为数组结构,在文件中还是在数据库中?从数据库读取,存储为数组初始记录实际上是如何存储的?作为数组结构,在文件中,或在数据库中?从数据库读取,存储为数组?我认为这忽略了将
“b”:“0”
作为输出的一部分的要求(即所有可能的字符串a、b、c、d应显示为默认数字0)?我认为这忽略了将
“b”:“0”
作为输出的一部分的要求(即,所有可能的字符串a、b、c、d应显示默认数字0)?json_编码输出的数组不是大括号吗?请参阅;数组是关联的,因此将使用大括号。在任何情况下,问题中的json表示似乎只是描述结构的附带内容,而不是预期的输出,但我还是包括了它。json_编码输出的数组不是大括号吗?请参阅;数组是关联的,因此将使用大括号。在任何情况下,问题中的JSON表示似乎只是描述结构的附带内容,而不是预期的输出,但我还是将其包括在内。与之一样,OP似乎建议每个日期数组本身应该是一个4元素数组,如果该行为输入中不存在。与之一样,OP似乎建议每个日期数组本身应该是一个4元素数组,如果输入中不存在该行,则默认值为0。哦,我刚刚在您的一条评论中读到,您正在从db读取并存储在数组中,否则,您最初发布的格式看起来像一个文本流:)如果您在将上述代码转换为从数组而不是文本文件读取时遇到问题,请告诉我。哦,我刚刚在您的一条评论中读到,您正在从数据库读取并存储在数组中,否则,您最初发布的格式看起来像文本流:)如果您在将上述代码翻译为从数组而不是文本文件读取时遇到问题,请告诉我。
$records = array(
    0 => array('2011-01-01', 'a', '1'),
    1 => array('2011-01-01', 'c', '5'),
    2 => array('2011-01-01', 'd', '3'),
    3 => array('2012-01-01', 'a', '1')
);

$dateArray = array();
$baseArray = array('a' => '0','b' => '0','c' => '0','d' = '0');

foreach($records as $record)
{
     $dateArray[$record[0]][$record[1]] = $record[2];  //You could actually do this while pulling in the info from the DB
}

foreach($dateArray as $date => $array)
{
     $missingArray = array_diff_key($array,$baseArray);  //Determine what keys are missing using the base array, get keys and default values
     $dateArray[$date] = array_merge($array,$missingArray);  //Merge the missing parts with the current array
}