PHP 如何从主数组中拆分或获取子数组的值
How to split or fetch value of sub-array from main array in PHP
我想从主数组中获取子数组的值。以下是我的数组。
[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
需要将$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
我想从主数组中获取子数组的值。以下是我的数组。
[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
需要将$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