从数组php获取价值

时间:2017-11-22 08:48:29

标签: php jquery arrays laravel

我在click函数上有一个jquery,我正在创建一个名为weekdays的新数组,稍后我插入该数组值:

 var weekdays = {};
 weekdays['id'] = [id];
 weekdays['amount_of_points'] = [amount_points];

我做完之后

weekdays = JSON.stringify(weekdays);
parent.find($('.form_points')).append('<input type="hidden" value=' + weekdays + ' name="skills[]">');

所有这些价值观都在Laravel的foreach中形成。

在控制器中,当我呼叫$ request-&gt;技能时,我正在接收

   array:2 [▼
    0 => "{"id":[41],"amount_of_points":[6]}"
    1 => "{"id":[40],"amount_of_points":[4]}"
   ]

如何获取id的值?

我试过了:

    foreach($request->skills as $skill){
        dd($skill['id']); // Illegal string offset id
        dd($skill->id); //Trying to get a property of a non object
     }

2 个答案:

答案 0 :(得分:2)

你需要将$skill数组中的json对象解码为php数组,然后才能像$skill['id']

那样访问它
foreach($request->skills as $skill){
  $newSkills = json_decode($skill)

  $newSkills['id']
}

答案 1 :(得分:0)

var weekdays = {};
var skill = [
    'id' => id,
    'amount_of_points' => amount_points
] 

并且,将每个技能附加到您的工作日阵列

weekdays.push(skill);

在PHP中

foreach($request->skills as $skill){
    $skill = json_decode($skill);
    var_dump($skill['id']);
    var_dump($skill['amount_points']);
}