我想單獨把一個數字放在數組中把數字分別放在數組中
ex。
$num = 345;
應該是在一個陣列,這樣我可以調用數字作爲
$num[1] (which should return 4)
我試圖str_split($num,1)
但沒有更迭。
感謝
編輯-------
一些調查研究str_split($num,1)
後實際做的伎倆。 (謝謝,Crayon暴力)
我想單獨把一個數字放在數組中把數字分別放在數組中
ex。
$num = 345;
應該是在一個陣列,這樣我可以調用數字作爲
$num[1] (which should return 4)
我試圖str_split($num,1)
但沒有更迭。
感謝
編輯-------
一些調查研究str_split($num,1)
後實際做的伎倆。 (謝謝,Crayon暴力)
如果您只是試圖從字符串中獲取單個字符,請使用substr
。
$second_digit = substr($num, 1, 1);
while ($num >0) {
$arr[i] = $num %10;
$num = $num/10;
i++
}
//this leaves the array in reverse order i.e. 543
//to flip
array_reverse($arr);
我想知道什麼是「沒有成功」是指因爲面值,你的代碼應該工作正常。你確實把它分配給了某些東西,對吧?例如:'$ num = 345; $ num = str_split($ num,1);' –
我想推入數組中的每個數字。所以它會像$ num = array(「3」,「4」,「5」); – user1292738
+如果我使用str_split($ num,1)我得到錯誤提示:試圖獲取非對象 – user1292738