繁体   English   中英

更新json php / laravel中键的值

[英]update value of key in json php/laravel

所以我有这个json

[{"id":"Horario","text":"Horario","answer":null},{"id":"Name","text":"Nome","answer":"teste"},{"id":"Phone","text":"Telefone","answer":"91"},{"id":"Email","text":"Email","answer":"teste@hotmail.com"},{"id":"Insc1","text":"Insc1","answer":"albano"},{"id":"Insc2","text":"Insc2","answer":"jorge"},{"id":"Insc3","text":"Insc3","answer":""}]

并且我想使用根据单选按钮获取的值来更新“ id” Horario的答案值。

我想要这样的东西,但我不知道为什么将null传递给特定的“ id” Horario,

    foreach($legend as $obj){
         if($obj->id == 'Horario')  
            $obj->answer=$option;
    }   

---我所有的代码

$legend = json_decode($request->input('quiz-legend'));  
$option=Input::get('qOp');

$answers = [];
$answersToTable = [];

foreach ($legend as $q) {
    array_push($answers, array(
        'id' => $q->id,
        'text' => $q->text,
        'answer' => $request->input('q'.$q->id)
    ));
}

foreach($legend as $obj){
    if($obj->id === 'Horario')
        $obj->answer=$option;
}

var_dump($option);
var_dump($answers);

所以我的var_dump返回$ option = 2的结果,但是在我的答案中为null ...

:\xampp\htdocs\LactInfo\app\Http\Controllers\InquiryController.php:178:string '2' (length=1)
D:\xampp\htdocs\LactInfo\app\Http\Controllers\InquiryController.php:179:
array (size=7)
  0 => 
    array (size=3)
      'id' => string 'Horario' (length=7)
      'text' => string 'Horario' (length=7)
      'answer' => null
  1 => 
    array (size=3)
      'id' => string 'Name' (length=4)
      'text' => string 'Nome' (length=4)
      'answer' => string 'teste' (length=22)
  2 => 
    array (size=3)
      'id' => string 'Phone' (length=5)
      'text' => string 'Telefone' (length=8)
      'answer' => string '91' (length=9)
  3 => 
    array (size=3)
      'id' => string 'Email' (length=5)
      'text' => string 'Email' (length=5)
      'answer' => string teste@hotmail.com' (length=22)
  4 => 
    array (size=3)
      'id' => string 'Insc1' (length=5)
      'text' => string 'Insc1' (length=5)
      'answer' => string 'albano' (length=6)
  5 => 
    array (size=3)
      'id' => string 'Insc2' (length=5)
      'text' => string 'Insc2' (length=5)
      'answer' => string 'jorge' (length=5)
  6 => 
    array (size=3)
      'id' => string 'Insc3' (length=5)
      'text' => string 'Insc3' (length=5)
      'answer' => string '' (length=0)

您可以进行收集并查询所需的项目,将其从堆栈中弹出,更新并推回,而不用使用foreach遍历所有项目
这样的事情应该起作用:

$json = request('quiz-legend');
$collection = collect($json);
$collection = $collection->keyBy('id');
$horario = $collection->where('id', 'Horario')->first();
$collection = $collection->forget('Horario');
$horario['answer'] = $option;
$collection = $collection->prepend($horario);
return $collection;

但是,如果您想继续使用循环,则必须将条件移至foreach循环,并跳过Horario并对其进行特殊处理(更改后的代码可在独立的php脚本上运行)

$legend = json_decode('[{"id":"Horario","text":"Horario","answer":null},{"id":"Name","text":"Nome","answer":"ABILIO BRANDAO DE MELO"},{"id":"Phone","text":"Telefone","answer":"917778621"},{"id":"Email","text":"Email","answer":"melo22_fca@hotmail.com"},{"id":"Insc1","text":"Insc1","answer":"albano"},{"id":"Insc2","text":"Insc2","answer":"jorge"},{"id":"Insc3","text":"Insc3","answer":""}]');
    $option = 'Very distinctive answer';

    $answers = [];
    $answersToTable = [];

    foreach ($legend as $q) {
        if ($q->id === 'Horario') {
            array_push($answers, array(
                'id' => $q->id,
                'text' => $q->text,
                'answer' => $option
            ));
            continue; // Skip this object
        }
        array_push($answers, array(
            'id' => $q->id,
            'text' => $q->text,
            'answer' => 'cute' . $q->id
        ));
    }

    var_dump($answers);

输出

array:7 [
  0 => array:3 [
    "id" => "Horario"
    "text" => "Horario"
    "answer" => "Very distinctive answer"
  ]
  1 => array:3 [
    "id" => "Name"
    "text" => "Nome"
    "answer" => "cuteName"
  ]
  2 => array:3 [
    "id" => "Phone"
    "text" => "Telefone"
    "answer" => "cutePhone"
  ]
  3 => array:3 [
    "id" => "Email"
    "text" => "Email"
    "answer" => "cuteEmail"
  ]
  4 => array:3 [
    "id" => "Insc1"
    "text" => "Insc1"
    "answer" => "cuteInsc1"
  ]
  5 => array:3 [
    "id" => "Insc2"
    "text" => "Insc2"
    "answer" => "cuteInsc2"
  ]
  6 => array:3 [
    "id" => "Insc3"
    "text" => "Insc3"
    "answer" => "cuteInsc3"
  ]
]

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM