我有一个看起来像这样的数组......
Array
(
[0] => Array
(
[code] => AS34
[2014-12-10] => 32
)
[1] => Array
(
[code] => AS34
[2014-12-11] => 42
)
[2] => Array
(
[code] => AS34
[2014-12-12] => 40
)
[3] => Array
(
[code] => AS34
[2014-12-15] => 44
)
[4] => Array
(
[code] => AH98
[2014-12-10] => 1
)
[5] => Array
(
[code] => AT78
[2014-12-12] => 1
)
[6] => Array
(
[code] => AL44
[2014-12-10] => 23
)
[7] => Array
(
[code] => AL44
[2014-12-11] => 27
)
[8] => Array
(
[code] => AL44
[2014-12-13] => 25
)
[9] => Array
(
[code] => AL44
[2014-12-15] => 26
)
)
我试图把它变成一个看起来像这样的数组......
var $example_data = array(
array(
'ID' => 1,
'code' => 'AS34',
'09/12/14' => '0',
'10/12/14' => '32',
'11/12/14' => '42',
'12/12/14' => '40',
'13/12/14' => '0',
'14/12/14' => '0',
'15/12/14' => '44',
),
array(
'ID' => 2,
'code' => 'AH98',
'09/12/14' => '0',
'10/12/14' => '1',
'11/12/14' => '0',
'12/12/14' => '0',
'13/12/14' => '0',
'14/12/14' => '0',
'15/12/14' => '0',
),
array(
'ID' => 3,
'code' => 'AT78',
'09/12/14' => '0',
'10/12/14' => '0',
'11/12/14' => '0',
'12/12/14' => '1',
'13/12/14' => '0',
'14/12/14' => '0',
'15/12/14' => '0',
),
array(
'ID' => 4,
'code' => 'AL44',
'09/12/14' => '0',
'10/12/14' => '23',
'11/12/14' => '27',
'12/12/14' => '0',
'13/12/14' => '25',
'14/12/14' => '0',
'15/12/14' => '26',
),
);
所以基本上它为每个代码设置一个数组'然后是前7天。任何人都可以指向一个类似的例子的方向或者我正在考虑使用的正确方法的一些阅读吗?
答案 0 :(得分:1)
像(固定)的东西:
$example_data = array();
foreach($data as $id => $row) {
$code = $row['code'];
unset($row['code']);
$karr = array_keys($row);
$date = current($karr);
$example_data[$code]['ID'] = $id;
$example_data[$code]['code'] = $code;
$example_data[$code][$date] = $row[$date];
}
print_r($example_data);
答案 1 :(得分:1)
我想到了这个:
$data = Array
(
0 => Array
(
"id" => 1,
"code" => "AS34",
"2014-12-10" => 32
),
1 => Array
(
"id" => 1,
"code" => "AS34",
"2014-12-11" => 42
),
2 => Array
(
"id" => 1,
"code" => "AS34",
"2014-12-12" => 40
),
3 => Array
(
"id" => 1,
"code" => "AS34",
"2014-12-15" => 44
),
4 => Array
(
"id" => 1,
"code" => "AH98",
"2014-12-10" => 1
),
5 => Array
(
"id" => 1,
"code" => "AT78",
"2014-12-12" => 1
),
6 => Array
(
"id" => 1,
"code" => "AL44",
"2014-12-10" => 23
),
7 => Array
(
"id" => 1,
"code" => "AL44",
"2014-12-11" => 27
),
8 => Array
(
"id" => 1,
"code" => "AL44",
"2014-12-13" => 25
),
9 => Array
(
"id" => 1,
"code" => "AL44",
"2014-12-15" => 26
)
);
function in_array_r($needle, $haystack, $strict = false) { //taken from http://stackoverflow.com/a/4128377/4263082
foreach ($haystack as $item) {
if (($strict ? $item === $needle : $item == $needle) || (is_array($item) && in_array_r($needle, $item, $strict))) {
return true;
}
}
return false;
}
$codes = Array();
$counter = -1;
foreach($data as $key => $value) {
if(!in_array_r($data[$key]["code"], $codes, true)) {
$codes[++$counter] = Array();
foreach($data[$key] as $subkey => $subvalue) {$codes[$counter][$subkey] = $subvalue;}
}
else{
foreach($data[$key] as $subkey => $subvalue) {
if($subkey != "code" && $subkey != "id") {$codes[$counter][$subkey] = $subvalue;}
}
}
}
print_r($codes);
<强>输出强>
Array
(
[0] => Array
(
[id] => 1
[code] => AS34
[2014-12-10] => 32
[2014-12-11] => 42
[2014-12-12] => 40
[2014-12-15] => 44
)
[1] => Array
(
[id] => 1
[code] => AH98
[2014-12-10] => 1
)
[2] => Array
(
[id] => 1
[code] => AT78
[2014-12-12] => 1
)
[3] => Array
(
[id] => 1
[code] => AL44
[2014-12-10] => 23
[2014-12-11] => 27
[2014-12-13] => 25
[2014-12-15] => 26
)
)
答案 2 :(得分:0)
我有这个工作:
<?php
header ('Content-type: text/plain; charset=utf-8');
//source data from the question
$sourceArr = array(
array('id' => 1
,'code' => 'AS34'
,'2014-12-10' => '32'
)
,array('id' => 1
,'code' => 'AS34'
,'2014-12-11' => '42'
)
,array('id' => 1
,'code' => 'AS34'
,'2014-12-12' => '40'
)
,array('id' => 1
,'code' => 'AS34'
,'2014-12-15' => '44'
)
,array('id' => 1
,'code' => 'AH98'
,'2014-12-10' => '1'
)
,array('id' => 1
,'code' => 'AT78'
,'2014-12-12' => '1'
)
,array('id' => 1
,'code' => 'AL44'
,'2014-12-10' => '23'
)
,array('id' => 1
,'code' => 'AL44'
,'2014-12-11' => '27'
)
,array('id' => 1
,'code' => 'AL44'
,'2014-12-13' => '25'
)
,array('id' => 1
,'code' => 'AL44'
,'2014-12-15' => '26'
)
);
foreach($sourceArr as $k => $v)
{
//get dates for last seven days
$d0 = date('Y-m-d',time() - 60 * 60 * 24 * 0);
$d1 = date('Y-m-d',time() - 60 * 60 * 24 * 1);
$d2 = date('Y-m-d',time() - 60 * 60 * 24 * 2);
$d3 = date('Y-m-d',time() - 60 * 60 * 24 * 3);
$d4 = date('Y-m-d',time() - 60 * 60 * 24 * 4);
$d5 = date('Y-m-d',time() - 60 * 60 * 24 * 5);
$d6 = date('Y-m-d',time() - 60 * 60 * 24 * 6);
//if we have a valid element - then save it in temp array
if(array_key_exists($d0,$v)){ $tmpArr[$v['code']][$d0] = "'$v[$d0]'"; }
if(array_key_exists($d1,$v)){ $tmpArr[$v['code']][$d1] = "'$v[$d1]'"; }
if(array_key_exists($d2,$v)){ $tmpArr[$v['code']][$d2] = "'$v[$d2]'"; }
if(array_key_exists($d3,$v)){ $tmpArr[$v['code']][$d3] = "'$v[$d3]'"; }
if(array_key_exists($d4,$v)){ $tmpArr[$v['code']][$d4] = "'$v[$d4]'"; }
if(array_key_exists($d5,$v)){ $tmpArr[$v['code']][$d5] = "'$v[$d5]'"; }
if(array_key_exists($d6,$v)){ $tmpArr[$v['code']][$d6] = "'$v[$d6]'"; }
}
//create the result array
$cnt = 0;
foreach($tmpArr as $k => $v)
{
$resultArr[$cnt]['ID'] = $cnt;
$resultArr[$cnt]['code'] = "'$k'";
asort($v);
foreach($v as $k2 => $v2)
{
$resultArr[$cnt][$k2] = $v2;
}
$cnt++;
}
//output
echo "Old array: ".print_r($sourceArr,1);
echo "New array: ".print_r($resultArr,1);
?>