У меня длинный массив, из которого я хочу получить все значения и сохранить его в отдельной переменной и сохранить каждое значение в базе данных.
Массив, который у меня есть:
Array ( [success] => 1 [categories] => Array ( [0] => Array ( [category_id] => 39 [name] => BAGS [categories] => Array ( [0] => Array ( [category_id] => 59 [name] => Handcrafted Purses [categories] => [status] => 1 ) [1] => Array ( [category_id] => 45 [parent_id] => 39 [name] => Laptop Bag [categories] => [status] => 1 ) ) [status] => 1 ) [1] => Array ( [category_id] => 40 [name] => BOXERS [categories] => Array ( [0] => Array ( [category_id] => 56 [parent_id] => 40 [name] => Women Boxers [status] => 1 ) ) [status] => 1 ) [2] => Array ( [category_id] => 91 [parent_id] => 0 [name] => Business Corporate [image] => [categories] => Array ( [0] => Array ( [category_id] => 92 [parent_id] => 91 [name] => Bags [image] => [categories] => Array ( [0] => Array ( [category_id] => 93 [parent_id] => 92 [name] => Potli Bags [image] => [categories] => [status] => 1 ) ) [status] => 1 ) ) [status] => 1 ) [3] => Array ( [category_id] => 60 [parent_id] => 0 [name] => Business Corporates [image] => [categories] => Array ( [0] => Array ( [category_id] => 90 [parent_id] => 60 [name] => Art Cushions [image] => [categories] => [status] => 1 ) [1] => Array ( [category_id] => 67 [parent_id] => 60 [name] => Bags [image] => [categories] => Array ( [0] => Array ( [category_id] => 77 [parent_id] => 67 [name] => Potli Bags [image] => [categories] => [status] => 1 ) [1] => Array ( [category_id] => 76 [parent_id] => 67 [name] => Smart Bags [image] => [categories] => [status] => 1 ) ) [status] => 1 ) [2] => Array ( [category_id] => 86 [parent_id] => 60 [name] => Fashion Jewellery [image] => [categories] => Array ( [0] => Array ( [category_id] => 88 [parent_id] => 86 [name] => Coming Soon - Products Uploading [image] => [categories] => [status] => 1 ) ) [status] => 1 ) [3] => Array ( [category_id] => 61 [parent_id] => 60 [name] => Men Footwear [image] => [categories] => Array ( [0] => Array ( [category_id] => 65 [parent_id] => 61 [name] => Canvas Loafers [image] => [categories] => [status] => 1 ) ) [status] => 1 ) [4] => Array ( [category_id] => 87 [parent_id] => 60 [name] => Shawls And Stoles [image] => [categories] => Array ( [0] => Array ( [category_id] => 89 [parent_id] => 87 [name] => Coming Soon - Products Uploading [image] => [categories] => [status] => 1 ) ) [status] => 1 ) ) [status] => 1 ) [4] => Array ( [category_id] => 15 [parent_id] => 0 [name] => ETHNIC WEAR [image] => [categories] => Array ( [0] => Array ( [category_id] => 28 [parent_id] => 15 [name] => Designer Lehngas [image] => [categories] => [status] => 1 ) [1] => Array ( [category_id] => 2 [parent_id] => 15 [name] => Suits [image] => [categories] => [status] => 1 ) ) [status] => 1 ) ) )
Я могу получить внешние значения этого массива с помощью этого кода:
if (!empty($array)) { foreach ($array['categories'] as $category) { echo $category['category_id']; echo "<br>"; } }
Я получил значения как:
39 40 91 60 15 16 38 57
Но я также хочу получить доступ к внутренним большинству значений массива. Может ли кто-нибудь сказать, как я могу создать вложенный цикл?
Если вы сейчас, насколько глубоко ваш массив, вы можете просто добавить другие петли foreach в ваш основной цикл.
if (!empty($array)) { foreach ($array['categories'] as $category) { echo $category['category_id']; echo "<br>"; if(isset($category['categories'])){ foreach($category['categories'] as $category2) { echo $category2['category_id']; echo "<br>"; if (isset($category2['categories'])){ foreach($category2['categories'] as $category3) { echo $category3['category_id']; echo "<br>"; ... } } }} } }