为了账号安全,请及时绑定邮箱和手机立即绑定

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

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

PHP
繁星点点滴滴 2022-09-17 15:25:53
我想从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<?phpforeach ($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/2020Location_seven - Location_nine  02/21/2020 - 02/26/2020
查看完整描述

1 回答

?
慕码人8056858

TA贡献1803条经验 获得超6个赞

您需要在 中分离出每个数组的键和值;然后你可以迭代它们。请注意,此代码假定日期数与位置相同:$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++) {

    echo $start_locations[$i] . ' - ' . $end_locations[$i] . "\n";

    echo $start_dates[$i] . ' - ' . $end_dates[$i] . "\n";

}

输出(对于示例数据):


Location_one - Location_two

02/11/2020 - 02/17/2020

Location_seven - Location_nine

02/21/2020 - 02/26/2020

3v4l.org 演示


查看完整回答
反对 回复 2022-09-17
  • 1 回答
  • 0 关注
  • 108 浏览

添加回答

举报

0/150
提交
取消
意见反馈 帮助中心 APP下载
官方微信