【发布时间】:2014-08-09 13:52:35
【问题描述】:
您好,我正在尝试解决以下问题。我有一些不言自明的代码,但我需要添加几行代码 我想通过键值 [1] 过滤由键值(在本例中为 [2])定义的较低值数组。因此,如果我有 3 个数组,其中包含值为 100 的键 [1],则应通过键 [2] 过滤这些数组。
到目前为止我的代码示例:
foreach($data as $line) {
if(substr($line,0,1)=="A") {
if(!$first) {
$parts = explode(chr(9), $line);
list($num1, $num2) = explode('_', $parts[1]); //code comes first / tested and works
$parts[2] = isset($num2) ? $num2 : $parts[2]; //it replaces key[2] with _* (1,2,3)
//then this will follow
$pos = strpos($parts[1], '_'); // this will remove all _* from key [1] if they exist
if($pos !== false) $parts[1] = substr($parts[1], 0, $pos); // tested and works
//echo "<pre>"; print_r($parts); echo "</pre>";
//need code to filter the arrays defined by key [1] via key [2] here?
例如,如果在我的一段代码之后有多个数组,如下所示:
Array
(
[0] => A
[1] => 100
[2] => 1
[3] => 1184
[4] => 0
)
Array
(
[0] => A
[1] => 100
[2] => 2
[3] => 1185
[4] => 0
)
Array
(
[0] => A
[1] => 100
[2] => 3
[3] => 1186
[4] => 0
)
Array
(
[0] => A
[1] => 101
[2] => 1
[3] => 1187
[4] => 0
)
Array
(
[0] => A
[1] => 101
[2] => 2
[3] => 1188
[4] => 0
)
Array
(
[0] => A
[1] => 302
[2] => 0
[3] => 1161
[4] => 0
)
经过一些代码过滤数组,最终的示例结果将是:
Array
(
[0] => A
[1] => 100
[2] => 3
[3] => 1186
[4] => 0
)
Array
(
[0] => A
[1] => 101
[2] => 2
[3] => 1188
[4] => 0
)
Array
(
[0] => A
[1] => 302
[2] => 0
[3] => 1161
[4] => 0
)
请我帮忙,它只需要几行,我不是程序员,但我想完成这个项目。
【问题讨论】:
-
一旦你过滤掉了“100_”,然后做一个数组排序,把前面的 100_ 返回到最大值。