[英]Reorganizing a PHP array structure (arrays within arrays)
I have this array within array which contains a lot of values: 我在包含很多值的数组中有这个数组:
183 =>
array (size=3)
0 => string 'DE' (length=2)
1 => string '2015-06-09' (length=10)
2 => string 'GK' (length=2)
184 =>
array (size=3)
0 => string 'DE' (length=2)
1 => string '2015-06-08' (length=10)
2 => string 'GL' (length=2)
185 =>
array (size=3)
0 => string 'FR' (length=2)
1 => string '2015-06-09' (length=10)
2 => string 'GN' (length=2)
186 =>
array (size=3)
0 => string 'FR' (length=2)
1 => string '2015-09-08' (length=10)
2 => string 'GO' (length=2)
0 is the country code. 0是国家代码。 1 is a date.
1是日期。 2 is a column on an Excel file.
2是Excel文件上的一列。
I want to organize it in this way: 我想以这种方式组织它:
2015-06-09 =>
array (size=3)
DE =>
array (size=2)
column => GK
download => 666
FR =>
array (size=2)
column => GN
download => 777
2015-06-08 =>
array (size=3)
DE =>
array (size=2)
column => GL
download => 666
FR =>
array (size=2)
column => GO
download => 777
So the same date can show up more than once. 因此,同一日期可能会显示多次。 if it gets to an array value with the same date - it inserts in it the country code with and its' column.
如果它获得具有相同日期的数组值-它将在国家/地区代码中插入带有及其列的列。
if it has more than 1 country - it adds a new country. 如果有多个国家/地区-则会添加一个新国家/地区。 (with the 'download' and column values).
(带有“下载”和列值)。
I have this function: 我有这个功能:
function get_cols_to_array_by_date($array) {
$mainarr = array();
$last_in_arr = count($array);
for ($i=0; $i<$last_in_arr; $i++){
$mainarr[$array[$i][1]] = array( $array[$i][0]=> array('downloads'=> 666, 'col'=>$array[$i][2]) );
}
return $mainarr;
}
which outputs an array that runs over the country when it gets to the same date and doesn't give me an array of countries. 当到达同一日期时,它会输出一个在全国范围内运行的数组,而不会给我一组国家/地区。
What part am I missing in my code? 我的代码中缺少哪一部分? Is there a simpler way to do it?
有更简单的方法吗? ( PHP syntax shortcuts ;) )
(PHP语法快捷方式;))
Assuming that the downloads
is the key of the initial array, and each element has 3 elements(date and 2 countries): 假设
downloads
是初始数组的键,并且每个元素有3个元素(日期和2个国家/地区):
//demo array
$old = array(
555=>array(
0 => 'DE',
1 => '2015-06-09',
2 => 'GK'),
234=>array(
0 => 'DE',
1 => '2015-06-08',
2 => 'GL'),
123=>array(
0 => 'FR',
1 => '2015-06-09',
2 => 'GN')
);
$new = array();
foreach($old as $key=>$arrayValues)
{
if(!array_key_exists($arrayValues[1], $new)){ //check if there is already a key by date
$new[$arrayValues[1]] = array();
}
$new[$arrayValues[1]][$arrayValues[0]] = array('column'=>$arrayValues[2], 'downloads'=>$key); //append formated array
}
echo "<pre>";
var_dump($new);
echo "</pre>";
array(2) {
["2015-06-09"]=>
array(2) {
["DE"]=>
array(2) {
["column"]=>
string(2) "GK"
["downloads"]=>
int(555)
}
["FR"]=>
array(2) {
["column"]=>
string(2) "GN"
["downloads"]=>
int(123)
}
}
["2015-06-08"]=>
array(1) {
["DE"]=>
array(2) {
["column"]=>
string(2) "GL"
["downloads"]=>
int(234)
}
}
}
Try looping and checking if element exists, if not - add it. 尝试循环并检查元素是否存在(如果不存在)-添加它。
$result = [];
foreach ($myArray as $key => $values) {
if (!isset($result[$values[1]])) {
$result[$values[1]] = [
$values[0] => [
'column' => $values[2],
'download' => $key,
]
];
} elseif (!isset($result[$values[1]][$values[0]])) {
$result[$values[1]][$values[0]] = [
'column' => $values[2],
'download' => $key,
];
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.