未初始化的字符串偏移错误处理
I have a variable name $accountNumber
which holds a 9 digit number. When a user types it in and when it is displayed, it is cut up in 9 separate box's.
Like so:
When getting the account number I do something like this:
//$data['acountNumber'] = 453554334;
$accountNumber = (string)$data['accountNumber'];
$accountNumber_0 = $accountNumber[0];
$accountNumber_1 = $accountNumber[1];
$accountNumber_2 = $accountNumber[2];
$accountNumber_3 = $accountNumber[3];
$accountNumber_4 = $accountNumber[4];
$accountNumber_5 = $accountNumber[5];
$accountNumber_6 = $accountNumber[6];
$accountNumber_7 = $accountNumber[7];
$accountNumber_8 = $accountNumber[8];
However, if I don't have an account number listed, I get an Uninitialized string offset error. I can make this "go away" if I add an @
in font of each $accountNumber[#]
but I thought I would ask you guys if there is a better way at handling this.
What do you think?
EDIT - After accepted answer
The below answers work great. But since I needed to have my array be size 9 no matter what, I found that this works best.
$accountNumber = array_pad(str_split($data['accountNumber']), 9, '');
我有一个变量名 如下所示:
获取帐号时我会这样做: p>
但是,如果我没有列出帐号,我会收到 未初始化的字符串偏移 em> strong>错误。 如果我在每个 您的看法是什么? p>
编辑 - 接受回答后 strong> p>
\ n 以下答案很有效。 但是因为我需要让我的数组大小为9,所以我发现这个效果最好。 p>
$ accountNumber code>,它包含一个9位数字。 当用户输入并显示时,它会被分成9个单独的框。 p>
p>
// $ data ['acountNumber'] = 453554334;
$ accountNumber =(string)$ data ['accountNumber'];
$ accountNumber_0 = $ accountNumber [0];
$ accountNumber_1 = $ accountNumber [1];
$ accountNumber_2 = $ accountNumber [2];
$ accountNumber_3 = $ accountNumber [3];
$ accountNumber_4 = $ accountNumber [4];
$ accountNumber_5 = $ accountNumber [5] ;
$ accountNumber_6 = $ accountNumber [6];
$ accountNumber_7 = $ accountNumber [7];
$ accountNumber_8 = $ accountNumber [8];
code> pre>
$ accountNumber [#] code>的字体中添加
@ code>,我可以让它“消失”,但我想我会问你们是否有更好的方法 处理此问题。 p>
$ accountNumber = array_pad(str_split($ data ['accountNumber'] ),9,''); code> p>
div>
You have not converted the string to array. str_split
would do that for you.
This should work for you:
<?php
$data['acountNumber'] = 453554334;
$data['acountNumber'] = strval($data['acountNumber']); // converts numbers to string
$accountNumber = str_split($data['acountNumber']); // split the string to array
$accountNumber_0 = $accountNumber[0];
$accountNumber_1 = $accountNumber[1];
$accountNumber_2 = $accountNumber[2];
$accountNumber_3 = $accountNumber[3];
$accountNumber_4 = $accountNumber[4];
$accountNumber_5 = $accountNumber[5];
$accountNumber_6 = $accountNumber[6];
$accountNumber_7 = $accountNumber[7];
$accountNumber_8 = $accountNumber[8];
Here is a PHP Fiddle that shows it in action.
//Cast your string
$accountNumber = (string)$data['accountNumber'];
//Get the length of the string
$length = strlen ($accountNumber);
//Create an empty array containing the account numbers
$accountNumberArr = array();
//Start the loop
for($i = 0; $i < $length; $i++){
//Add the account numbers to the account number array
$accountNumberArr['accountNumber_'.$i] = $accountNumber[$i];
}
//Now you can access your account number as follows
// $accountNumberArr['accountNumber_0'], $accountNumberArr['accountNumber_1'];