我有以下数组:
array (
0 =>
array (
'new_service' =>
array (
0 =>
array (
'service_name' => 'wi-fi',
'this_service_is_present' => true,
),
1 =>
array (
'service_name' => 'Washing machine',
'this_service_is_present' => false,
),
),
),
)
我需要显示该服务(如果存在),或者理想地同时显示两者,但要说该服务存在/不存在。
我只是尝试了一下以测试是否能够获取这些值然后进行计算,所以:
$repeater = get_field('servizi_casa');
foreach( $repeater as $row ) {
echo $row[0][0][1];
}
或
$repeater = get_field('servizi_casa');
foreach( $repeater as $row ) {
echo $row['service_name'];
}
和
$repeater = get_field('servizi_casa');
foreach( $repeater as $row ) {
echo $row[0]['service_name'];
}
但是我做错了,因为我没有价值
答案 0 :(得分:3)
如果您的输出为$repeater
的值,则此代码应该起作用:
foreach ($repeater as $row) {
foreach ($row['new_service'] as $service) {
echo "{$service['service_name']} is " . ($service['this_service_is_present'] ? "" : "not ") . "present\n";
}
}
输出:
wi-fi is present
Washing machine is not present
答案 1 :(得分:0)
<?php
$a = array (
0 =>
array (
'new_service' =>
array (
0 =>
array (
'service_name' => 'wi-fi',
'this_service_is_present' => true,
),
1 =>
array (
'service_name' => 'Washing machine',
'this_service_is_present' => false,
),
),
),
);
foreach($a[0]['new_service'] as $k) {
if($k['this_service_is_present'])
echo $k['service_name'] .'is present';
} else {
echo $k['service_name'] .'is Not present';
}
}