检查从foreach循环值是一个数组 [英] check if value from a foreach loop is in an array

查看:100
本文介绍了检查从foreach循环值是一个数组的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

好吧,我认真地卡住了,我已经工作一个导航菜单,我只是不能上的功能如何我想它,所以我已经改变了方向,但我现在又卡住所以任何帮助将是非常美联社preciated和迫切需要的!

我有以下code和我尝试做如下 - 我有一个适用于网站的所有页面的信息,然后保存是子页面的页面的ID另一个数组的数组。我想要做的就是使用foreach循环遍历第一个数组的所有页面,并检查他们的ID是否孩子IDS与否的数组中为止。如果他们没有,那么他们是顶级导航页和我要输出一些code,然后设置另一个foreach循环,这将检查所有子网页是否具有当前页面的父ID等。

我似乎无法找出如何比较$ B与$ childpages数组中的ID不管我试试!这甚至可能如果又如何好吗?

这是一个什么样的IM在present

试图第一节

 < PHP函数buildMenu4($所有页,$ childpages){    的foreach($所有页为$页){    $ a = $页['PARENTID'];
    $ B = $页['身份证'];
    $ C = $ childpages;
    回声< UL> \\ N的;        如果(!in_array($ B,$ C)){        做一点事..........

$ C数组内容:

 阵列

[0] =>阵列([ID] = 1→6)
[1] =>阵列([ID] =→15)
[2] =>阵列([ID] => 100)
[3] =>阵列([ID] => 101)
[4] =>阵列([ID] => 103)
[5] =>阵列([ID] => 104)
[6] =>阵列([ID] => 105)

编辑---------------------------------

我已经重新设计我的code和我回,我是前两天的变化!反正下面的作品code如预期,直到我尝试循环,然后它只是呼应了第一个foreach循环例如结果的foreach($所有页为$页){.....但没有做任何其他事情。

我试图创建一个名为loopMenu函数,然后运行这个递归,直到没有要显示更多的网页。我试图写的函数,如在下面的code座的pusedo code,但我不能得到它的工作。我可能已经糊涂了一些参数或参数,或者我刚刚作出了一个很大的错误的地方,但我不能看到它 - 任何帮助将是巨大的AP preciated和急需

 < PHP函数buildMenu6($所有页,$ childpageids,$ childpages,$ subchildpages){
        的foreach($ childpageids为$ childID的){
            $ C [] = $ childID的['身份证'];            };
            回声< UL> \\ N的;        的foreach($所有页为$页){
        $ a = $页['PARENTID'];
        $ B = $页['身份证'];
            如果(!in_array($ B,$ C)){                回声<立GT;< A HREF =。 $页[URL。 >中。 $页['LinkLabel的']。 &所述; / A>中;
                    在我想要的功能E.G. START功能loopMenu($ childpages,$ subchildpages){...在code后面....
                    回声< UL> \\ N的;                    的foreach($ childpages为$ childparent){                            $ D = $ childparent ['PARENTID'];
                            $ E = $ childparent ['身份证'];                            如果(($ d个== $ b)或($ d个== $ G)){                                回声<立GT;< A HREF =。 $ childparent [URL。 >中。 $ childparent ['LinkLabel的']。 &所述; / A>中;
                                    回声< UL> \\ N的;                                    的foreach($ subchildpages为$ subchild){
                                        $ G = $ subchild ['身份证'];
                                        $ F = $ subchild ['PARENTID'];
                                        如果($ F == $ E){                                            回声<立GT;< A HREF =。 $ subchild [URL。 >中。 $ subchild ['LinkLabel的']。 &所述; / A>中;
                        在这里我尝试重新运行使用loopMenu($ childparent,$ subchild)功能;
                                            回声<李/>中;
                                            };
                                        };                                    回声< / UL> \\ N的;
                                回声< /李>中;                        };
                    };
                    回声< / UL> \\ N的;
我想要我的功能到例如END };
回声< /李>中;                };
    };
        回声< / UL> \\ N的;
        }; ?>

然后我打电话的主要功能buildMenu6像这样:

 < PHP buildMenu6($ pageids,$ childPageIds,$ childPageArray,$ childPageArray); ?>


解决方案

您需要嵌套的foreach (我已经改变了你的变种名称或使用原有的以提高可读性):

 的foreach($所有页为$页){
    的foreach($ childpages为$子){
        如果($页['身份证'] == $孩子['身份证']){
            //做一点事
            打破;
        }
    }
}

或PHP> = 5.5.0使用 array_column

  $ childids = array_column($ childpages,ID);的foreach($所有页为$页){
    如果(in_array($页['身份证'],$ childids)){
        //做一点事
    }
}

作为一种混合型的:

 的foreach($ childpages为$子){
    $ childids [] = $孩子['身份证'];
}
的foreach($所有页为$页){
    如果(in_array($页['身份证'],$ childids)){
        //做一点事
    }
}

Ok I'm seriously stuck, I've been working on a nav menu that I just cannot get to function how I want it to so i've changed tack but am now stuck again so any help would be much appreciated and desperately needed!

I have the code below and am trying to do the following - I have an array that holds info for all the pages of a site and then another array that holds the ids of the pages that are child pages. What I want to do is use a foreach loop to loop through all the pages of the first array and check whether their ids are in the array of child ids or not. If they are not then they are top level nav pages and I want to output some code and then set up another foreach loop which will check whether any subpages have a parent id of the current page and so on.

I can't seem to work out how to compare $b with the ids in the $childpages array no matter what I try! Is this even possible and if so how please?

This is the first section of what im trying at present

<?php function buildMenu4 ($allpages, $childpages) {

    foreach ($allpages as $pages){

    $a = $pages['parentid'];
    $b = $pages['id'];
    $c = $childpages;


    echo "<ul>\n";



        if (!in_array($b, $c)) {

        DO SOMETHING..........

Array contents of $c:

Array 
( 
[0] => Array ( [id] => 6 ) 
[1] => Array ( [id] => 15 ) 
[2] => Array ( [id] => 100 ) 
[3] => Array ( [id] => 101 ) 
[4] => Array ( [id] => 103 ) 
[5] => Array ( [id] => 104 ) 
[6] => Array ( [id] => 105 ) 
)

edit ---------------------------------

I have reworked my code and am back to a variation of where I was a couple of days ago!! Anyway the code below works as intended until I try to loop it and then it just echoes the results of the first foreach loop e.g. foreach ($allpages as $pages){..... but fails to do anything else.

I am trying to make a function called loopMenu and then run this recursively until there are no more pages to be displayed. I have tried to write the function as shown with the pusedo code in the code block below but I just can't get it to work. I may have muddled up some of the arguments or parameters or perhaps I have just made a big mistake somewhere but I can't see it - any help would be hugely appreciated and desperately needed!

 <?php function buildMenu6 ($allpages, $childpageids, $childpages, $subchildpages) {


        foreach ($childpageids as $childid){
            $c[] = $childid['id'];

            };


            echo "<ul>\n";  

        foreach ($allpages as $pages){


        $a = $pages['parentid'];
        $b = $pages['id'];


            if (!in_array($b, $c)){

                echo "<li><a href=" . $pages['url'] . ">" . $pages['linklabel'] . "</a>";


                    WHERE I WANT THE FUNCTION TO START E.G. function loopMenu($childpages, $subchildpages){...the code that follows....


                    echo"<ul>\n";

                    foreach ($childpages as $childparent) {

                            $d = $childparent['parentid'];
                            $e = $childparent['id'];



                            if (($d == $b) or ($d == $g)) {

                                echo "<li><a href=" . $childparent['url'] . ">" . $childparent['linklabel'] . "</a>";
                                    echo "<ul>\n";

                                    foreach ($subchildpages as $subchild){
                                        $g = $subchild['id'];
                                        $f = $subchild['parentid'];


                                        if ($f == $e){

                                            echo "<li><a href=" . $subchild['url'] . ">" . $subchild['linklabel'] . "</a>";
                        WHERE I TRY TO RERUN THE FUNCTION USING loopMenu($childparent, $subchild);                  
                                            echo "<li/>";
                                            };
                                        };

                                    echo"</ul>\n";
                                echo "</li>";

                        };
                    };
                    echo "</ul>\n";


WHERE I WANT MY FUNCTION TO END E.G. };


echo "</li>";

                };


    };
        echo "</ul>\n";
        }; ?>

Then I call the main buildMenu6 function like so:

<?php buildMenu6($pageids, $childPageIds, $childPageArray, $childPageArray); ?>

解决方案

You need a nested foreach (I've changed your var names or used the original ones for readability):

foreach($allpages as $page) {
    foreach($childpages as $child) {
        if($page['id'] == $child['id']) {
            //do something
            break;
        }
    }
}

Or PHP >= 5.5.0 use array_column:

$childids = array_column($childpages, 'id');

foreach($allpages as $page) {
    if(in_array($page['id'], $childids)) {
        //do something
    }
}

As a kind of hybrid:

foreach($childpages as $child) {
    $childids[] = $child['id'];
}
foreach($allpages as $page) {
    if(in_array($page['id'], $childids)) {
        //do something
    }
}

这篇关于检查从foreach循环值是一个数组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆