php array_keys返回[0,1]但是array [1]抛出未定义的偏移量

时间:2016-11-25 19:12:10

标签: php arrays offset explode

我使用explode函数创建和数组这个数组实际上应该有两个元素爆炸后我用count函数检查它确实包含2个元素,我还打印了一个数组的json,它包含两个元素最后我使用数组键并返回[0,1]但是数组[1]抛出未定义的偏移...请帮助

代码

   $name = $request['name'];
        $exercises = $request['video_exercises'];
        $custom_names = $request['video_custom_names'];
        $custom_names = explode('-', $custom_names);
        $exercises = explode('-', $exercises);
        $set = new Set();
        try {
           $set->name = $name;
           $set->save();    
        } catch (\Illuminate\Database\QueryException $ex) {

            return ['success'=>false,'message'=>'An unexpected error occured, a different may be using the same name you are trying to use'.$ex->getMessage()];
        }
        foreach ($exercises as $exercise) {
            $temp = new Exercise();
            $custom_name = '';
            foreach ($custom_names as $name) {
                if($name!=='')
                if (explode($exercise, $name)!== false) {
                    $temp_name = explode('+', $name);
                    $custom_name = $temp_name[1]; //Returns undefined offset
                } 
            }

0 个答案:

没有答案