首页 > 解决方案 > 更新 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 的结果,但在我的答案中是空的......

:\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)

标签: phplaravel

解决方案


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"
  ]
]

推荐阅读