我需要一些幫助,以便在此思考。我有這個代碼來計算正確的數量forecast_conditions
這是4; (count($condition) -1) <= 4 ? ', ' : ', eller '
。如果計數(數據)低於或等於4,請打印一張或打印兩張 - 不起作用
count($condition)
打印5
所以我必須有負之一。 $condition
是在foreach
,所以我可以循環內容forecast_conditions
; foreach($whome_answer[weather][forecast_conditions] AS $condition) {
。
上面的代碼打印onsdag, torsdag, fredag, lördag,
這是錯誤的。以下是我想要的:onsdag, torsdag, fredag, eller lördag
但我無法在代碼中正確使用!我測試了各種解決方案(== 4,> = 4,!= 4,< 4等等)。
我怎樣才能讓它正常工作?
在此先感謝!
編輯
這裏的循環:
foreach($whome_answer[weather][forecast_conditions] AS $condition) {
# KONTROLL
if(utf8_decode($condition['day_of_week']['@attributes']['data']) == 'mån') {
$day = 'måndag';
# KONTROLL
} elseif(utf8_decode($condition['day_of_week']['@attributes']['data']) == 'tis') {
$day = 'tisdag';
# KONTROLL
} elseif(utf8_decode($condition['day_of_week']['@attributes']['data']) == 'ons') {
$day = 'onsdag';
# KONTROLL
} elseif(utf8_decode($condition['day_of_week']['@attributes']['data']) == 'tors') {
$day = 'torsdag';
# KONTROLL
} elseif(utf8_decode($condition['day_of_week']['@attributes']['data']) == 'fre') {
$day = 'fredag';
# KONTROLL
} elseif(utf8_decode($condition['day_of_week']['@attributes']['data']) == 'lör') {
$day = 'lördag';
# KONTROLL
} elseif(utf8_decode($condition['day_of_week']['@attributes']['data']) == 'sön') {
$day = 'söndag';
}
echo '<a href="javascript:void(0)" class="forecast-link">';
echo $day;
echo '</a>';
echo (count($condition) -1) <= 4 ? ', ' : ', eller ';
}
這裏是我獲取從內容的鏈接:http://www.google.com/ig/api?weather=,,,59378217,13504219&hl=en
EDIT(解決方案,但多了一個問題) 代碼下面是解決方案,但它現在打印onsdag, torsdag, fredag, eller lördag,
而不是我正在尋找。如何刪除「lördag」後的最後一個逗號?如果在「if tag」之後包含$i
:onsdag, 2torsdag, 3fredag, eller 4lördag, 5
,看起來如何。
我該如何解決這個問題?
$i = 1;
foreach($whome_answer[weather][forecast_conditions] AS $condition) {
$i++;
# KONTROLL
if(utf8_decode($condition['day_of_week']['@attributes']['data']) == 'mån') {
$day = 'måndag';
# KONTROLL
} elseif(utf8_decode($condition['day_of_week']['@attributes']['data']) == 'tis') {
$day = 'tisdag';
# KONTROLL
} elseif(utf8_decode($condition['day_of_week']['@attributes']['data']) == 'ons') {
$day = 'onsdag';
# KONTROLL
} elseif(utf8_decode($condition['day_of_week']['@attributes']['data']) == 'tors') {
$day = 'torsdag';
# KONTROLL
} elseif(utf8_decode($condition['day_of_week']['@attributes']['data']) == 'fre') {
$day = 'fredag';
# KONTROLL
} elseif(utf8_decode($condition['day_of_week']['@attributes']['data']) == 'lör') {
$day = 'lördag';
# KONTROLL
} elseif(utf8_decode($condition['day_of_week']['@attributes']['data']) == 'sön') {
$day = 'söndag';
}
echo '<a href="javascript:void(0)" class="forecast-link">';
echo $day;
echo '</a>';
echo $i != 4 ? ', ' : ', eller ';
}
不知道我明白這個問題。聽起來像是對數字的誤解。 「如果計數(數據)低於或等於4」只是計數($數據)<= 4。不知道-1來自哪裏。 – Corbin 2012-04-11 19:35:56
請閱讀整個問題。 – Erik 2012-04-11 19:37:06
<而不是<= – jack 2012-04-11 19:37:36