Answer the question
In order to leave comments, you need to log in
How to write a foreach loop for a given array?
There is an array that is output from the database
$firsts= array(
1 => array(
['id'] => 1,
['title'] => '1',
['parent'] => 0,
['visible'] => 1,
['childs'] => array(
12 => array(
['id'] => 12,
['title'] => '1.1',
['parent'] => 1,
['visible'] => 1,
['childs'] => array(
18 => array(
['id'] => 18,
['title'] => '1.1.1',
['parent'] => 12,
['visible'] => 1,
['childs'] => array()
)
)),
13 => array(
['id'] => 13,
['title'] => '1.2',
['parent'] => 1,
['visible'] => 1,
['childs'] => array())
)
),
2 => array(
['id'] => 1,
['title'] => '2',
['parent'] => 0,
['visible'] => 1,
['childs'] => array()
)
)
switch($query) {
case 'getFirst': // Запрос на получение 1ой подкатегории
// Сохраним в переменную значение выбранного типа
$first_id = trim($_POST['first_id']); // Очистим его от лишних пробелов
// Формируем массив с ответом
$result = NULL;
$i = 0;
foreach($firsts[$first_id] as $kind_id => $kind)
{
$result[$i]['kind_id'] = $kind_id;
foreach($kind['title'] as $value)
{
$result[$i]['kind'] = $value;
}
$i++;
}
break;
Answer the question
In order to leave comments, you need to log in
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question