如果我的數組在內部編譯後爲空,我想顯示破折號( - )。以下是我的嘗試。如果沒有值,則使用默認值Implode
結果與數組數據 - >https://repl.it/HIUy/0
<?php
$array = array(1,2);
$result = array();
foreach ($array as $curr_arr) {
$result[] = $curr_arr;
}
echo 'Array With Data - ' . implode(',', $result);
//Result : Array With Data : 1,2
?>
的結果,而不在陣列數據 - >https://repl.it/HIVE/0
<?php
$array = array();
$result = array();
foreach ($array as $curr_arr) {
$result[] = $curr_arr;
}
echo 'Array Without Data - ' . implode(',', $result);
//Result : Array With Data - :
?>
正如你在第二個結果看,我無法打印任何東西,如我的陣列是空白的,因此我無法打印任何東西。
不過,我想打印破折號( - )僅通過使用類似array_filter
這我已經嘗試過使用implode
,但我不能這樣做。在這裏,我已經試過https://repl.it/HIVP/0
<?php
$array = array();
$result = array();
foreach ($array as $curr_arr) {
$result[] = $curr_arr;
}
echo 'Array With Data : ' . implode(',', array_filter($result));
//Result : Array With Data :
?>
有人可以指導我如何實現這一目標嗎?
感謝
張貼代碼爲你的問題 –
'如果(空(YOUR_ARRAY))'' –
回聲的一部分 '數組沒有數據 - '。 (implode(',',$ result)?:' - ');' –