在遍历具有变量的数组PHP时获取数据的问题

时间:2022-02-01 21:33:25

I asked a similar question earlier but I couldn't get a clear answer to my issue. I have a function "isParent" that gets 2 pieces of data. Each 1 of the 2 gets a string separating each value with a , or it just gets a plain int and then checks if the first value given is a parent of the second.

我之前问了一个类似的问题,但我无法对我的问题得到明确答案。我有一个函数“isParent”获取2个数据。 2中的每一个都得到一个字符串,用a分隔每个值,或者它只是得到一个普通的int,然后检查给定的第一个值是否是第二个的父。

I pull the 2 bits of data in and explode them but when I go through my nested for loop and try to test $toss = $arr1[$i]; print_r($toss); It comes up blank. I have no idea what the issue is: Here is the full code of the function...

我将2位数据拉入并爆炸但是当我通过我的嵌套for循环并尝试测试$ toss = $ arr1 [$ i];的print_r($折腾);它出现空白。我不知道问题是什么:这是函数的完整代码......

function isParent($parent, $child)
{

    $parentArr = explode(',', $parent);
    $childArr = explode(',',$child);

    //Explode by Comma here. If array length of EITHER parentArr or childArr > 1 Then throw to an Else
    if(count($parentArr) <= 1 && count($childArr) <= 1) //If explode of either is > 1 then ELSE
    {
       $loop = get_highest_slot(15);
       for($i = $loop; $i > 0; $i--)
       {
         $temp = get_membership_from_slot($i,'id_parent','id_child');
         if($temp['id_parent'] == $parent && $temp['id_child'] == $child)
         {
           return 1;
         }
       }
    }
    else //set up a for loop in here so that you traverse each parentArr value and for each iteration check all child values
    {
        $i = count($parentArr);
        $c = count($childArr);
        for(;$i >=0;$i--) //Loop through every parent
        {
            for(;$c >=0;$c--)
            {
                echo '<br>$i = ';
                print_r($i);
                echo '<br><br>Parent Arr at $i:';
                $toss = $parentArr[$i];
                echo $toss;
                echo '<br>';
                print_r($childArr);
                echo '<br><br>';
                if(isParent($parentArr[$i],$childArr[$c])) //THIS CAUSES AN INFINITE YES! Learn how to pull an array from slot
                {   
                    return 1;
                }
            }
        }
    }
  return 0;
}

2 个解决方案

#1


You are missing some code for the slot procedures. Apart from that, you probably need to use a different variable for the inner for loop. because $c will be 0 after the first iteration of $i.

您缺少一些插槽过程的代码。除此之外,您可能需要为内部for循环使用不同的变量。因为在$ i的第一次迭代后$ c将为0。

#2


Thanks for the help! The issue was in the recursive call back to the top of the function. It was tossed empty slots and when comparing 2 empty slots it returned a false positive. A quick !empty() check fixed it.

谢谢您的帮助!问题在于递归回调函数的顶部。它被抛空槽,当比较2个空槽时,它返回了误报。快速!空()检查修复它。

#1


You are missing some code for the slot procedures. Apart from that, you probably need to use a different variable for the inner for loop. because $c will be 0 after the first iteration of $i.

您缺少一些插槽过程的代码。除此之外,您可能需要为内部for循环使用不同的变量。因为在$ i的第一次迭代后$ c将为0。

#2


Thanks for the help! The issue was in the recursive call back to the top of the function. It was tossed empty slots and when comparing 2 empty slots it returned a false positive. A quick !empty() check fixed it.

谢谢您的帮助!问题在于递归回调函数的顶部。它被抛空槽,当比较2个空槽时,它返回了误报。快速!空()检查修复它。