C
C
chelkaz2017-02-17 04:24:34
Laravel
chelkaz, 2017-02-17 04:24:34

Why doesn't the function return?

If in the get_full_city function I output print_r to the return place, then everything correctly outputs as intended. But I don’t need to output, to collect ready-made arrays into one and work with it after foreach. But if I put return, then it gives me null. At the moment else, I have an array collected and I need to somehow get all these collected ready-made arrays after foreach, but I can’t understand how return in else gives null.

class SearchController extends Controller
{
    //
    public function input(Request $request)
    {
// В таблице все города и тут мы ищем до последнего родителя
        $city = $request->input('city'); 
        $arr = LocName::where('NAME', 'like', '%'.$city.'%')->take(10)->get(); // Выбераем все совпадения
        $full_arr = array();

        function get_full_city($one, $full_arr)
        {
            $loc_id = $one->LOCATION_ID; // Получили ИД города
            $location = DB::table('location')->where('ID', $loc_id)->first(); // Список родителя и прочего
            $name_arr = DB::table('loc_name')->where('LANGUAGE_ID', 'ru')->where('LOCATION_ID', $location->ID)->first();
            $name = $name_arr->NAME;

            $parent = $location->PARENT_ID;
            $type_id = $location->TYPE_ID;
            $type = DB::table('loc_type_name')->where('TYPE_ID', $type_id)->where('LANGUAGE_ID', 'ru')->first();
            $full_arr[] = array(
                'name' => $name,
                'type' => $type->NAME,
            );
            echo ">>>".$location->DEPTH_LEVEL.PHP_EOL;
            if($location->DEPTH_LEVEL > 1) // Если не главный родитель то продолжаем
            {
                $par_ar = DB::table('loc_name')->where('LANGUAGE_ID', 'ru')->where('LOCATION_ID', $parent)->first();
                get_full_city($par_ar, $full_arr); // Отправляем на следующий круг
            }
            else {
               // И тут если постепить print_r($full_arr) То все верно выводится по списку, то-есть когда достигаем главного родителя, то выводим, но мне нужно все собрать в массив и я пробую вернуть ретурном 
                return $full_arr;
            }
        }
        foreach ($arr as $one)
        {
            $data[] = get_full_city($one, $full_arr);
        }
        И в итоге значения массива null, хотя если в функции вместо ретурна ставить вывод, то он правильно выводит.
        dd($data);

    }
}

Answer the question

In order to leave comments, you need to log in

2 answer(s)
C
chelkaz, 2017-02-17
@chelkaz

The error was in the first condition, since it was necessary to just start the function:

if($location->DEPTH_LEVEL > 1) // Если не главный родитель то продолжаем
            {
                $par_ar = DB::table('loc_name')->where('LANGUAGE_ID', 'ru')->where('LOCATION_ID', $parent)->first();
                get_full_city($par_ar, $full_arr); // Отправляем на следующий круг
            }

And return via return!
if($location->DEPTH_LEVEL > 1) // Если не главный родитель то продолжаем
            {
                $par_ar = DB::table('loc_name')->where('LANGUAGE_ID', 'ru')->where('LOCATION_ID', $parent)->first();
                return get_full_city($par_ar, $full_arr); // Отправляем на следующий круг
            }

I
Ilya Karavaev, 2017-02-17
@Quieteroks

Never thought that the condition is true, not false, where by the way you do not return a value from the recursion. Therefore, when printing, everything is clear, on the second circle it displays the values ​​\u200b\u200bthat you need. At the same time, everything is sad on the first round, because it went into recursion and did not return anything.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question