douyou7072 2018-12-15 11:21
浏览 121
已采纳

laravel请求索引不能列出数组分类

Note: my question is not duplicate

I have form in out of site and use web api laravel 5.7, now is send these parameters as post:

estate: "545345"
id_number: "43534545"
serial_number: "435435345"
type[code_id]: "45345345435"
type[company]: ""
type[country]: ""
type[maintenance_period]: ""
type[model]: "5435345435"
type[name]: "5345345345"
waranty[company_id]: "0"
waranty[duration]: ""
waranty[end]: ""
waranty[start]: ""

part of form:

<input type="text" name="estate" required="">
<input type="text" name="id_number" required="">
<input type="text" name="type[code_id]">
<input type="text" name="type[company]">

Now I want to get type and waranty array: same as for example:

$type = ['name' => 'test','company' => '...', 'model' => '...' ]

I try these codes:

$type = $request->input('type.*'); // NULL
and
$type = $request->input('type'); // NULL
and 
$type = Input::get('type'); // NULL
and
$in = $request->all();
$type = $in['type']; // NULL
AND
$request->get('type'); // NULL
AND
$request->post('type') // NULL

Doesn't work :( , only I can get single variable, by this method: (but I need array)

$in = $request->all();
$in['type[code_id]'] ;

And I test type[][code_id],type[][name],... in form but not work :(

My axios method:

serial = function (id) {
    var srl = $(id).serializeArray();
    var post = {};
    for (const k in srl) {
        let item = srl[k];
        post[item.name] = item.value;
    }
    return post;
}

axios.post(url, serial("#frm")).then(function (res) {// succs axios ;

     console.log(res);
}).catch(err => {// error axios ;
    console.log(err);
}); // end axios ;

What is the Laravel native solution ? for this problem ?

note: I can create an function for solve this problem but I need laravel native solution.

  • 写回答

1条回答 默认 最新

  • dongxiangqian1855 2019-07-10 06:38
    关注

    You can handles arrays with this function:

    function categorizedArray($array){
        $ret = [] ;
        $re = '/(.*)\[(.*)\]/U';
        foreach ($array as $i => $value) {
            preg_match_all($re, $i, $matches, PREG_SET_ORDER, 0);
            if ($matches == []){
                $ret[$i] = $value ;
            }else{
                $ret[$matches[0][1]][$matches[0][2]] = $value ;
            }
        }
        return $ret;
    }
    

    for exmp: ( We assume the function define as helper )

    $all = categorizedArray($request->all());
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 做个有关计算的小程序
  • ¥15 MPI读取tif文件无法正常给各进程分配路径
  • ¥15 如何用MATLAB实现以下三个公式(有相互嵌套)
  • ¥30 关于#算法#的问题:运用EViews第九版本进行一系列计量经济学的时间数列数据回归分析预测问题 求各位帮我解答一下
  • ¥15 setInterval 页面闪烁,怎么解决
  • ¥15 如何让企业微信机器人实现消息汇总整合
  • ¥50 关于#ui#的问题:做yolov8的ui界面出现的问题
  • ¥15 如何用Python爬取各高校教师公开的教育和工作经历
  • ¥15 TLE9879QXA40 电机驱动
  • ¥20 对于工程问题的非线性数学模型进行线性化