我有一个我要解析的mysql查询对象结果。让我们说阵列看起来像这样。
$superheroes = array(
[0] => array(
"name" => "Peter Parker",
"email" => "peterparker@mail.com",
"age"=>"33",
"sex"=>"male",
),
[1] => array(
"name" => "jLaw",
"email" => "jlaw@mail.com",
"age"=>"22",
"sex"=>"female",
),
[2] => array(
"name" => "Clark Kent",
"email" => "clarkkent@mail.com",
"age"=>"36",
"sex"=>"male",
),
[3] => array(
"name" => "Gal Gadot",
"email" => "gal@mail.com",
"age"=>"22",
"sex"=>"female",
)
);
我想遍历这个数组,当我在每个数组时,我想在下一个数组中向前看,并找出当前男性英雄与列表中下一个直接女性之间的年龄差异。我找到了很多关于它的帖子 数组_keys 2.缓存迭代器, 3.上一页,下一页等。
但他们所有人都在谈论一维数组。这是我试过的
foreach ($superheroes as $key => $list){
if($list['sex']=="male"){
$currentHerosAge=$list['age'];
while($next=next($superheroes)){
if($next['sex']=="female"){
$diff=$currentHerosAge -$next['age'];
echo "Age diff: ".$diff;
break;
}
}
}
}
但是当我尝试这个时,对于数组[0],接下来错过了数组[1],然后选择了数组[3]。不知道如何解决这个问题。
答案 0 :(得分:0)
通常,在遍历数组时,您不会使用#34; lookahead"。常见的方法是存储最后一次迭代的项目并对此元素进行比较
$last_hero = false;
foreach ($superheroes as $hero) {
if ($last_hero) {
// do some stuff..
}
$last_hero = $hero;
}
如果你真的需要一个超过一个项目的前瞻,你就不会使用foreach循环。
for ($i = 0; $i < count($superheroes); $i++) {
// do sth. with $superheroes[$i]
if (...) {
for ($j = $i + 1; $j < count($superheroes); $j++) {
// do sth. with $superheroes[$j]
}
}
}
答案 1 :(得分:0)
此代码有效,这里是您解决该问题的方法(我将更新解释原因)+编辑Clark - Gal部分,再一次比较。
Execute it here如果你愿意的话。这是一个花哨的版本,echo只是为了说明运行逻辑和循环内发生的事情:)当然最终的结果可以简单得多。
<?php
$superheroes = [
[
"name" = "Peter Parker",
"email" = "peterparker@mail.com",
"age"="33",
"sex"="male",
],[
"name" = "jLaw",
"email" = "jlaw@mail.com",
"age"="22",
"sex"="female",
],[
"name" = "Clark Kent",
"email" = "clarkkent@mail.com",
"age"="36",
"sex"="male",
],[
"name" = "Gal Gadot",
"email" = "gal@mail.com",
"age"="22",
"sex"="female",
]
];
$length = count($superheroes);
$counterReset = 0;
while ($current = current($superheroes) )
{
$length -= 1;
if($current['sex']=="male"){
$currentHerosAge = $current['age'];
while($next = next($superheroes)){
$counterReset += 1;
if($next['sex']=="female"){
$diff=$currentHerosAge - $next['age'];
echo "\n C: ".$current['name']." to ".$next['name']." Age diff: ".$diff."\n";
break;
}
}
}
if($counterReset 0){
for($i = 0; $i < $counterReset; $i++){
prev($superheroes);
}
$counterReset = 0;
}
if($length == 0){
break;
}
next($superheroes);
}
current() next() prev()
都充当指针。这意味着每次你打电话给他们,你就可以控制假设&#34; head&#34;您正在调用的数组或元素。因此,每次使用next()
移动时,必须确保也返回到所需位置以继续循环常规循环:)
参考next(),current(),prev()和reset()
执行逻辑
你声明一个或多个数组带来了一个PHP Warning PHP Warning: Illegal offset type in /home/ on line 21
并且是空的。出于这个原因,我重新宣布了这一点。
答案 2 :(得分:0)
您可以使用array_slice从下一个关键索引中切割数组,然后寻找第一个女性年龄
$diff = [];
foreach($superheroes as $key => $male) {
if ($male['sex'] === 'female' ) continue;
// get the next key index
$nextKey = $key + 1;
if ( isset( $superheroes[ $nextKey ] ) ) {
// slice from the next key index
$nextSuperHeros = array_slice($superheroes, $nextKey);
foreach($nextSuperHeros as $k => $female) {
if ($female['sex'] === 'female') {
$diff[] = $male['age'] - $female['age'];
break;
}
}
}
}
工作Example
希望这有帮助