如何在PHP中从主数组中拆分或获取子数组的值

我想从main获取子数组的值。以下是我的数组。


 [tour_avaibility] => Array

        (

            [tour_avaibility_location] => Array

                (

                    [Location_one] => Location_two

                    [Location_seven] => Location_nine

                )


            [tour_avaibility_date] => Array

                (

                    [02/11/2020] => 02/17/2020

                    [02/21/2020] => 02/26/2020

                )


        )

我想要通过一个循环来获取价值。我使用了以下代码,但它重复值。'tour_avaibility_location'tour_avaibility_date


<?php

foreach ($tourAvaibilityDate['tour_avaibility'] as $key => $value) {?>

    <div class="other-tag justify-content-between">

            <?php

            foreach ( array_unique($tourAvaibilityLocation['tour_avaibility']['tour_avaibility_location'] ) as $k => $v) {?>    

                <div class="tag-name"><?php echo $k.$v; ?></div>

            <?php }?>


            <?php

            foreach ( array_unique($tourAvaibilityDate['tour_avaibility']['tour_avaibility_date'] ) as $k => $v) {?>    

            <div class="tag-price">25 Nov 2019 - 30 Nov 2019</div>

            <?php } ?>

        <div class="tag-remove text-light">

            <i class="fa fa-times"></i>

        </div>

    </div>

<?php } ?>

我想放出去喜欢..


Location_one - Location_two  02/11/2020 - 02/17/2020

Location_seven - Location_nine  02/21/2020 - 02/26/2020


繁星点点滴滴
浏览 109回答 1
1回答

慕码人8056858

您需要在 中分离出每个数组的键和值;然后你可以迭代它们。请注意,此代码假定日期数与位置相同:$tourAvaibilityDate['tour_avaibility']$tours = $tourAvaibilityDate['tour_avaibility'];$start_locations = array_keys($tours['tour_avaibility_location']);$end_locations = array_values($tours['tour_avaibility_location']);$start_dates = array_keys($tours['tour_avaibility_date']);$end_dates = array_values($tours['tour_avaibility_date']);$c = count($start_locations);for ($i = 0; $i < $c; $i++) {&nbsp; &nbsp; echo $start_locations[$i] . ' - ' . $end_locations[$i] . "\n";&nbsp; &nbsp; echo $start_dates[$i] . ' - ' . $end_dates[$i] . "\n";}输出(对于示例数据):Location_one - Location_two02/11/2020 - 02/17/2020Location_seven - Location_nine02/21/2020 - 02/26/20203v4l.org 演示
打开App,查看更多内容
随时随地看视频慕课网APP