doukunsan5553 2016-10-30 17:00
浏览 11

爆炸时打印阵列

In the below code i am getting the output

Array ( [0] => 1 [1] => 2 )

but the expected output is

Array ( [0] => 1 [1] => 2 [2] => 3)  Array ( [0] => 1 [1] => 2 )   

why is it always executing the second if condition? although the first condition is also true. this is the code I have tried

    <?php 
$test_arr=array();
$temp_option_arr=array();
$option_arr=array();

$options_array_val = Array ( 0 => "animals:1", 1 => "animals:2", 2 => "animals:3", 3 => "birds:1", 4 => "birds:2" );

    foreach($options_array_val as $options_val)
    {
        $search_filter = explode(":", $options_val);
        print_r($search_filter);
        if(!in_array($search_filter[0],$option_arr))
        {
            array_push($temp_option_arr,$search_filter[1]);
            array_push($option_arr,$search_filter[0]);
            $temp_option_arr=array();

        }
        array_push($temp_option_arr,$search_filter[1]);
    }
    $test_arr[$search_filter[0]]=$temp_option_arr;
    $find_species = array();
    if(!empty($test_arr['animals']))
    {

        $find_species = $test_arr['animals'];
        print_r($find_species);

    }

    if(!empty($test_arr['birds']))
    {

        $find_species = $test_arr['birds'];
        print_r($find_species);
    }

?>     
  • 写回答

2条回答 默认 最新

  • douwen1313 2016-10-30 17:31
    关注

    The line

    $test_arr[$search_filter[0]]=$temp_option_arr;
    

    is out of the foreach scope, therefore it only sets the array for the birds and not for the animals, so you need to move it one line upper.

    Also, you assign temp_option_arr to a value

    array_push($temp_option_arr,$search_filter[1]);
    

    then set it back to empty array without using it

    $temp_option_arr=array();
    

    You can remove the first one I guess

    评论

报告相同问题?

悬赏问题

  • ¥15 基于卷积神经网络的声纹识别
  • ¥15 Python中的request,如何使用ssr节点,通过代理requests网页。本人在泰国,需要用大陆ip才能玩网页游戏,合法合规。
  • ¥100 为什么这个恒流源电路不能恒流?
  • ¥15 有偿求跨组件数据流路径图
  • ¥15 写一个方法checkPerson,入参实体类Person,出参布尔值
  • ¥15 我想咨询一下路面纹理三维点云数据处理的一些问题,上传的坐标文件里是怎么对无序点进行编号的,以及xy坐标在处理的时候是进行整体模型分片处理的吗
  • ¥15 CSAPPattacklab
  • ¥15 一直显示正在等待HID—ISP
  • ¥15 Python turtle 画图
  • ¥15 stm32开发clion时遇到的编译问题