数组返回不需要的值 - php

时间:2016-04-10 06:05:29

标签: php arrays

我的数组返回一些我不想要它们的值并且我从未将它们添加到它中,这非常有趣:

    foreach ($this->cart as $c) {


        if ($c['parent_id'] != 0) {
            $parent_id = $c['parent_id'];

            if(in_array($parent_id,$checked_parents))
            {
                echo 'true';
            }else
            {

                echo 'parent=>'.$parent_id;
                echo '<br />';
                $checked_parents[] = $parent_id;
                $childes[] = array();
                foreach($this->cart as $c2)
                {


                    if($c2['parent_id'] == $parent_id)
                    {
                        $childes[$c2['id']] = $c2 ;
                        echo 'added'.$c2['id'];
                        echo '<br />';

                    }

                }


                foreach($childes as $k => $v)
                {

                    echo $k ;
                    echo '<br />';
                    if(!$k || $k == 0)
                        continue;
                  ...
                  //my other codes
                  ...

echo的输出:

parent=>16709
added3
0 // unwanted value ?
3
parent=>16710
added2
added1
0 // unwanted value ?
3 // unwanted value ?
4 // unwanted value ?
2 // it's ok
1 // it's ok
true

不需要的值:

0,3,4 

为什么要添加到我的阵列?

1 个答案:

答案 0 :(得分:0)

我改变了:

$childes[] = array();

到:

unset($childes);