我在php中有一个多维数组,如下所示:
Array
(
[0] => Array
(
[Year_id] => 29
[Make_id] => 7
[Model_id] => 34
[Engine_id] => 19
)
[1] => Array
(
[Year_id] => 30
[Make_id] => 7
[Model_id] => 34
[Engine_id] => 19
)
[2] => Array
(
[Year_id] => 21
[Make_id] => 7
[Model_id] => 34
[Engine_id] => 19
)
[3] => Array
(
[Year_id] => 1
[Make_id] => 7
[Model_id] => 34
[Engine_id] => 19
)
[4] => Array
(
[Year_id] => 2
[Make_id] => 7
[Model_id] => 34
[Engine_id] => 2
)
[5] => Array
(
[Year_id] => 3
[Make_id] => 7
[Model_id] => 34
[Engine_id] => 2
)
[6] => Array
(
[Year_id] => 4
[Make_id] => 7
[Model_id] => 34
[Engine_id] => 2
)
)
我的代码如下:
<?php
$cars = array
(
array("Year_id"=>29, "Make_id"=>7, "Model_id"=>34, "Engine_id"=>19),
array("Year_id"=>30, "Make_id"=>7, "Model_id"=>34, "Engine_id"=>19),
array("Year_id"=>21, "Make_id"=>7, "Model_id"=>34, "Engine_id"=>19),
array("Year_id"=>1, "Make_id"=>7, "Model_id"=>34, "Engine_id"=>19),
array("Year_id"=>2, "Make_id"=>7, "Model_id"=>34, "Engine_id"=>2),
array("Year_id"=>3, "Make_id"=>7, "Model_id"=>34, "Engine_id"=>2),
array("Year_id"=>4, "Make_id"=>7, "Model_id"=>34, "Engine_id"=>2)
);
echo '<pre>';
print_r($cars);
?>
在上面的结果中,我有四个名为Year_id
,Make_id
,Model_id
和Engine_id
的ID。 Year_id
可能随时更改,但当剩余ID中的任何值发生更改时,我希望将这些数组存储在单独的数组中,并再次检查剩余数组,并且除Year_id
之外的任何ID值发生更改时,存储那些数组在一个单独的数组中。
现在它应该将前四个数组存储在一个单独的数组中,因为Engine_id
值会发生变化。怎么解决?我准备好在2天后获得赏金。
答案 0 :(得分:2)
$i = 0;
$sepr = 0;
$arr = array();
foreach($cars as $car) {
$k = $i; $k = ($k > 0 ? $k-1 : 0);
foreach($car as $key => $value) {
if($key != "Year_id" && $value != $cars[$k][$key]) {
$sepr++;
}
}
$arr[$sepr][] = $car;
$i++;
}
print_r($arr);
答案 1 :(得分:2)
在这里,我将如何解决这个问题。我会给你一种方法,我认为这种方法效率很高。你有这种形式的输入数组:
$cars = array
(
array("Year_id"=>29, "Make_id"=>7, "Model_id"=>34, "Engine_id"=>19),
array("Year_id"=>30, "Make_id"=>7, "Model_id"=>34, "Engine_id"=>19),
array("Year_id"=>21, "Make_id"=>7, "Model_id"=>34, "Engine_id"=>19),
array("Year_id"=>1, "Make_id"=>7, "Model_id"=>34, "Engine_id"=>19),
array("Year_id"=>2, "Make_id"=>7, "Model_id"=>34, "Engine_id"=>2),
array("Year_id"=>3, "Make_id"=>7, "Model_id"=>34, "Engine_id"=>2),
array("Year_id"=>4, "Make_id"=>7, "Model_id"=>34, "Engine_id"=>2)
);
据我所知,你并不关心&#34; Year_id&#34;但是如果任何其他ID发生变化,您希望将这些元素分开。首先定义输出关联数组,让我们称之为$output
。
所以你会这样做:
$output = array();
$key = $cars[0]["Make_id"].'-'.$cars[0]["Model_id"].'-'.$cars[0]["Engine_id"];
$output[$key] = $cars[0];
让我试着解释一下我刚刚做了什么。我基本上开始以三元组的形式索引输出数组(包含有关&#39; Make_id&#39;,&#39; Model_id&#39;和#39; Engine_id&#39;)的信息,用于{{1}中的每个数组数组。所以你可以想象$cars
数组的索引/键是这样的:
$ouput
我希望您了解我的目标,以及我如何计划现在构建输出。现在只需要检查$ cars数组的每个元素,将连字符(&#39; - &#39;)连接起来的三个ID连接在上面显示的格式中,然后查找 $ 键输出$ output数组。如果它存在,只需将其推到该位置或创建一个新元素。
代码可能是我沿着这些方向(未经测试):
$output{
["7-34-19"]=> array("Year_id"=>29, "Make_id"=>7, "Model_id"=>34, "Engine_id"=>19)
...
}
所以最后 //Just for the sake of clarity I start from 1 here as we have already taken care of the first element
for($i = 1; $i < count($cars); $i++) {
$key = $cars[i]["Make_id"].'-'.$cars[i]["Model_id"].'-'.$cars[i]["Engine_id"];
if (array_key_exists($key,$output)){
//That means we have found another car whose make_id, model_id and engine_id match
//Simply push that array into the associative array like so:
array_push($output[$key],$cars[$i]);
}
else{
//We have found a unique car as the key doesn't exist, simply add it to the array
$output[$key] = $cars[$i];
}
}
数组可能看起来像这样:
$output
我理解 $output{
["7-34-19"]=> {
[0] => array("Year_id"=>29, "Make_id"=>7, "Model_id"=>34, "Engine_id"=>19),
[1] => array("Year_id"=>30, "Make_id"=>7, "Model_id"=>34, "Engine_id"=>19),
[2] => array("Year_id"=>21, "Make_id"=>7, "Model_id"=>34, "Engine_id"=>19),
[3] => array("Year_id"=>1, "Make_id"=>7, "Model_id"=>34, "Engine_id"=>19)
}
["7-34-2"]=> {
[0] => array("Year_id"=>2, "Make_id"=>7, "Model_id"=>34, "Engine_id"=>2),
[1] => array("Year_id"=>3, "Make_id"=>7, "Model_id"=>34, "Engine_id"=>2),
[2] => array("Year_id"=>4, "Make_id"=>7, "Model_id"=>34, "Engine_id"=>2)
}
}
数组的结构不是很简单,它可能会根据你最终想要实现的内容而有所不同,但它可以根据三个键分离和组合数组元素。只有一个$output
循环。因此该算法在时间效率上是线性的,即 O(N)。
你显然不必将键与连字符组合在一起,你可以使用任何分隔符,但想法是以独特的方式组合三个关键的键并使它们成为我们输出数组的索引提高了整个算法的效率。无论如何,我希望我能澄清我的思考过程,让你开始朝着正确的方向前进。