php loop获取值,循环遍历一个对象数组并在laravel PHP中获取值(Loop over an array of objects and get values in laravel PHP)...

这篇博客讨论了在Laravel PHP框架中处理来自QA应用的JSON对象,该对象包含问题和答案。博主遇到的问题是在迭代答案数组时出现错误。解决方案是正确引用数组键,并检查'isCorrect'是否存在后再赋值。

循环遍历一个对象数组并在laravel PHP中获取值(Loop over an array of objects and get values in laravel PHP)

我正在开发一个QA应用程序,我需要根据需要动态地为问题添加多个答案。 为此,我发送一个带有问题和答案的对象。

question={

'question_text':'what is your name',

'answers':[

{'answer_text':'some answer','isCorrect':'1'},

{'answer_text':'some answer','isCorrect':'1'},

{'answer_text':'some answer'} // answer may or may not have isCorrect key

]

}

在服务器端,我有两个表或迁移1用于问题,1用于回答。 答案表有三个字段question_id , answer_text' and isCorrect'。 question_id是答案表中问题的外键。

存储我正在做的对象是什么

$question_text=Input::get('question_text');

$answers=Input::get('answers');

$question=new Question;

$question->question_text=$question_text;

$question->save();

foreach($answers as $ans){

$answer=new Answer;

$answer->question_id=$question->id;

$answer->answer_text=$ans->answer_text;

if($answer->isCorrect){

$answer->is_correct=$ans->isCorrect;

}

else{

$answer->is_correct=0;

}

$answer->save();

}

但是迭代时会出现错误

`production.ERROR: exception 'ErrorException' with message 'Trying to get property of non-object'`

我在这做什么错。 我是第一次使用PHP。 我试图像Javascript或Python方式迭代它。 告诉我如何获取答案数组对象的值并存储它们。

I am working on a QA app where i need to add multiple answers to a question as per requirement dynamically. for this i am sending an object which carry question and the answers like.

question={

'question_text':'what is your name',

'answers':[

{'answer_text':'some answer','isCorrect':'1'},

{'answer_text':'some answer','isCorrect':'1'},

{'answer_text':'some answer'} // answer may or may not have isCorrect key

]

}

On server side I have two tables or migrations 1 for Question and 1 for answer. The answer table have three fields question_id, answer_text' andisCorrect'. question_id is the foreign key of question in answer table.

to store the objects what i am doing is

$question_text=Input::get('question_text');

$answers=Input::get('answers');

$question=new Question;

$question->question_text=$question_text;

$question->save();

foreach($answers as $ans){

$answer=new Answer;

$answer->question_id=$question->id;

$answer->answer_text=$ans->answer_text;

if($answer->isCorrect){

$answer->is_correct=$ans->isCorrect;

}

else{

$answer->is_correct=0;

}

$answer->save();

}

But while iterating there is an error

`production.ERROR: exception 'ErrorException' with message 'Trying to get property of non-object'`

what wrong I am doing here. I am first time working on PHP. I am trying to iterate it like Javascript or Python way. Tell me how can i get the values of answers array object and store them.

原文:https://stackoverflow.com/questions/23827825

更新时间:2020-01-18 21:20

最满意答案

您似乎没有正确引用数组变量。 这应该工作:

foreach($answers as $ans){

$answer=new Answer;

$answer->question_id=$question->id;

$answer->answer_text=$ans['answer_text'];

$answer->is_correct = isset($ans['isCorrect']);

$answer->save();

}

ps我不确定forEach - 我很惊讶它有效 - 但你应该重命名为foreach以确认正常标准

You dont seem to be referencing the array variables correctly. This should work:

foreach($answers as $ans){

$answer=new Answer;

$answer->question_id=$question->id;

$answer->answer_text=$ans['answer_text'];

$answer->is_correct = isset($ans['isCorrect']);

$answer->save();

}

p.s. Im not sure about forEach - I'm suprised it works - but you should probably rename it to foreach to confirm to the normal standards

2014-05-23

相关问答

我已经使用了array_walk_recursive ,但是array_walk甚至是一个简单的foreach就足够了。 //Recreate input data from question

$in = [

(object) [

'ContractorID' => '7049',

'ContractorName' => 'Supermarket 1',

'SpendAmount' => '615.36',

...

在你的foreach循环中做$category['name'] inside your foreach loop do $category['name']

这个: array_push($fieldsList,serialize($reportField)我认为如果你正在执行$gpForecast->fieldList=(serialize($fieldsList));在itteration之后没用。这也可能导致你得到空条目 - 这里: foreach($fieldList as $field){ 您正试图获取序列化数据 (在这里序列化它们: array_push($fieldsList,serialize($reportField)); )。 这意

...

在数组上使用foreach循环并使用管道符号分解每个值。 像这样的东西: foreach ($arr as $val)

{

$newData = explode("|", $val);

}

Use foreach loop on the array and explode each value with pipe symbol. Something like this : foreach ($arr as $val)

{

$newData = explode("|", $va

...

您似乎没有正确引用数组变量。 这应该工作: foreach($answers as $ans){

$answer=new Answer;

$answer->question_id=$question->id;

$answer->answer_text=$ans['answer_text'];

$answer->is_correct = isset($ans['isCorrect']);

$answer->save();

}

ps我不确定forEach - 我很惊讶

...

尝试这个: foreach($header->to as $key => $val)

{

$result[] = $val->personal;

}

print_r($result);

Try this: foreach($header->to as $key => $val)

{

$result[] = $val->personal;

}

print_r($result);

$productsTitles = array();

foreach ($result->result->products as $product) {

$productsTitles[] = $product->productTitle;

}

var_dump($productsTitles);

$productsTitles = array();

foreach ($result->result->products as $product) {

$productsTitles

...

问题在于这个电话: .val(jsonLab)

jsonLab是JS中保存的对象数组。 因此,将其设置为jQuery对象的val()将意味着在其上调用toString() 。 结果是[object Object] 。 这是发送到您的PHP逻辑的,因此错误。 要解决此问题,您需要在将JSON设置为文本字段的值时手动对其进行字符串化: $('').val(JSON.stringify(jsonLab)).appendTo('

...

您可以使用“地图”方法 collect($objects)->map(function ($item) {

return [

'id' => $item->id,

'name' => $item->name

];

});

You can use "map" method collect($objects)->map(function ($item) {

return [

'

...

要获得总计数,请尝试 $totalfriends = $friends['summary']['total_count'];

To get total count try $totalfriends = $friends['summary']['total_count'];

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值