foreach循环为给定数组循环一次

时间:2018-08-14 08:22:24

标签: php json laravel

我的数组

{"_token":"NeTBqkwEBqSD9AiXQ3lrIbGyos0sHKD5FEhJRdsW","questionnaires_id":null,"Int1":"2345","type":"1","asdf":"1","eq":"2018-08-02","radio_btn":"0","checkbox":"1"}

我的代码

$datas = $request->all();

foreach ($datas as $key => $data)
{
    return $data;
}

1 个答案:

答案 0 :(得分:0)

protected void On_Update_Click(object sender, EventArgs e) { //cast the sender back to a control and get the gridviewrow from the namingconainer GridViewRow row = (GridViewRow)(((Control)sender).NamingContainer); //get the gridview itself from the gridviewrow GridView gv = row.Parent.Parent as GridView; //get the correct datakey from the gridview with the dataitemindex int SKU = Convert.ToInt32(gv.DataKeys[row.DataItemIndex].Values[0]); //display result Label1.Text = SKU.ToString(); } 不是数组。
这是JSON

所以:

1)将JSON转换为数组

{"_token":"NeTBqkwEBqSD9AiXQ3lrIbGyos0sHKD5FEhJRdsW","questionnaires_id":null,"Int1":"2345","type":"1","asdf":"1","eq":"2018-08-02","radio_btn":"0","checkbox":"1"}

2)然后循环数组

$json = '{"_token":"NeTBqkwEBqSD9AiXQ3lrIbGyos0sHKD5FEhJRdsW","questionnaires_id":null,"Int1":"2345","type":"1","asdf":"1","eq":"2018-08-02","radio_btn":"0","checkbox":"1"}';

$array = json_decode($json, true); //true for associative array representation

print_r($array);

//result:
Array
(
    [_token] => NeTBqkwEBqSD9AiXQ3lrIbGyos0sHKD5FEhJRdsW
    [questionnaires_id] => 
    [Int1] => 2345
    [type] => 1
    [asdf] => 1
    [eq] => 2018-08-02
    [radio_btn] => 0
    [checkbox] => 1
)
相关问题