Php “拉雷维尔”;未定义索引……”;Foreach循环中的错误 拉威尔5.7
我有一个函数,可以根据名称更改十进制数的长度。我需要使用该功能9次。这就是为什么我试图调用它,但我得到了“未定义索引停止”错误 如果我对第一个函数进行以下更改,它会工作,但这不是我想要做的Php “拉雷维尔”;未定义索引……”;Foreach循环中的错误 拉威尔5.7,php,laravel,Php,Laravel,我有一个函数,可以根据名称更改十进制数的长度。我需要使用该功能9次。这就是为什么我试图调用它,但我得到了“未定义索引停止”错误 如果我对第一个函数进行以下更改,它会工作,但这不是我想要做的 移除foreach循环 不要使用$input[$data],而是分别使用$input['entry']、$input['stop']和$input['target'] 不要多次调用函数,而是使用不同的$input['…']粘贴代码块 顺便说一句,我尝试在makeDecimals函数中使用$input['e
- 移除foreach循环
- 不要使用$input[$data],而是分别使用$input['entry']、$input['stop']和$input['target']
- 不要多次调用函数,而是使用不同的$input['…']粘贴代码块
public function update(Request $request, $id)
{
// The codes down below are the same as my original update function
$transaction = Trade::findOrFail($id);
$input = $request->all();
$assetName = $input['asset'];
if (strpos($assetName, 'XAU') !== false || strpos($assetName, 'OIL') !== false) {
$decimal = 2;
} elseif (strpos($assetName, 'JPY') !== false || strpos($assetName, 'XAG') !== false) {
$decimal = 3;
} else {
$decimal = 5;
}
// The end of the same codes.
// I removed $this->makeDecimals() and paste the codes down below.
// The differences are:
// • There is no foreach loop.
// • I used $input['entry'] instead of $input[$data]
// When I duplicate those codes and use 'stop' instead of 'entry', it also works.
if ($input['entry']) {
$input['entry'] = str_replace(',', '.', $input['entry']);
$entryArray = explode(".", $input['entry']);
if (count($entryArray) == 1) {
$decimalPart = null;
} else {
$decimalPart = $entryArray[1];
}
if ($decimalPart != null) {
$decimalLength = strlen($decimalPart);
if ($decimalLength < $decimal) {
for ($i = $decimalLength; $i < $decimal; $i++) {
$input['entry'] = $input['entry'] . 0;
}
}
if ($decimalLength > $decimal) {
for ($i = $decimalLength; $i > $decimal; $i--) {
$input['entry'] = substr($input['entry'], 0, -1);
}
}
} else {
$input['entry'] = $input['entry'] . '.';
for ($i = 0; $i < $decimal; $i++) {
$input['entry'] = $input['entry'] . 0;
}
}
$transaction->update($input);
}
公共功能更新(请求$Request,$id)
{
//下面的代码与我原来的更新功能相同
$transaction=Trade::findOrFail($id);
$input=$request->all();
$assetName=$input['asset'];
if(strpos($assetName,'XAU')!==false | | strpos($assetName,'OIL')!==false){
$decimal=2;
}elseif(strpos($assetName,'JPY')!==false | | strpos($assetName,'XAG')!==false){
$decimal=3;
}否则{
$decimal=5;
}
//相同代码的结尾。
//我删除了$this->makeDecimals()并将代码粘贴到下面。
//区别在于:
//•没有foreach循环。
//•我使用$input['entry']而不是$input[$data]
//当我复制这些代码并使用“停止”而不是“输入”时,它也会起作用。
如果($input['entry'])){
$input['entry']=str_replace(“,”,“,”,$input['entry']);
$entryArray=explode(“.”,$input['entry']);
if(计数($entryArray)==1){
$decimalPart=null;
}否则{
$decimalPart=$entryArray[1];
}
如果($decimalPart!=null){
$decimalLength=strlen($decimalPart);
如果($decimalLength<$decimal){
对于($i=$decimalLength;$i<$decimal;$i++){
$input['entry']=$input['entry'].0;
}
}
如果($decimalLength>$decimal){
对于($i=$decimalLength;$i>$decimal;$i--){
$input['entry']=substr($input['entry'],0,-1);
}
}
}否则{
$input['entry']=$input['entry'];
对于($i=0;$i<$decimal;$i++){
$input['entry']=$input['entry'].0;
}
}
$transaction->update($input);
}
错误很明显,“停止”在您的输入中不存在。快速检查可修复此问题
if (strlen($input[$data]) > 0) { //This is the line that throw the error.
致:
据我所知,您的函数
函数makeDecimals()
只是php中的数字格式
加上str\u替换
你可以这样使用它
$number=$request->input('entry');
$FormattedNumber=number_格式(str_替换(“,”,“,”,$number),$decimal,”);
将函数strlen($input[$data])替换为!empty($input[$data]),然后尝试使用tanks,但我知道stop是存在的,因为当我使用$input['stop']它工作得很好。谢谢,但我的问题不是设置数字的格式。我调用它时无法使我的函数工作。但是当我使用常量而不是$data变量时,代码块工作。@BülentAkgül the make atry{…}catch()
环绕foreach()
如果发生异常,只需转储$datas
即可检查dd($datas,$data)
public function update(Request $request, $id)
{
// The codes down below are the same as my original update function
$transaction = Trade::findOrFail($id);
$input = $request->all();
$assetName = $input['asset'];
if (strpos($assetName, 'XAU') !== false || strpos($assetName, 'OIL') !== false) {
$decimal = 2;
} elseif (strpos($assetName, 'JPY') !== false || strpos($assetName, 'XAG') !== false) {
$decimal = 3;
} else {
$decimal = 5;
}
// The end of the same codes.
// I removed $this->makeDecimals() and paste the codes down below.
// The differences are:
// • There is no foreach loop.
// • I used $input['entry'] instead of $input[$data]
// When I duplicate those codes and use 'stop' instead of 'entry', it also works.
if ($input['entry']) {
$input['entry'] = str_replace(',', '.', $input['entry']);
$entryArray = explode(".", $input['entry']);
if (count($entryArray) == 1) {
$decimalPart = null;
} else {
$decimalPart = $entryArray[1];
}
if ($decimalPart != null) {
$decimalLength = strlen($decimalPart);
if ($decimalLength < $decimal) {
for ($i = $decimalLength; $i < $decimal; $i++) {
$input['entry'] = $input['entry'] . 0;
}
}
if ($decimalLength > $decimal) {
for ($i = $decimalLength; $i > $decimal; $i--) {
$input['entry'] = substr($input['entry'], 0, -1);
}
}
} else {
$input['entry'] = $input['entry'] . '.';
for ($i = 0; $i < $decimal; $i++) {
$input['entry'] = $input['entry'] . 0;
}
}
$transaction->update($input);
}
if (strlen($input[$data]) > 0) { //This is the line that throw the error.
if (isset($input[$data]) && strlen($input[$data]) > 0) {