简体   繁体   中英

update value of key in json php/laravel

So I have this 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":""}]

and I want to update answer value for "id" Horario with the value I'm getting according to a radiobutton is selected.

I want something like this but I can't figure why is passing null to the specific "id" Horario,

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

--- all my code

$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);

So my var_dump returns result for $option = 2 but in my answer is 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)

You can make a collection and query your desired item, pop it from the stack, update it and push it back rather than iterating over all items with a foreach
Something like this should work:

$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;

But if you want to keep using loops, then you'd have to move your conditional to the foreach loop and skip Horario and treat it exceptionally like so (changed code to work on standalone php script)

$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);

The output

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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