Inside each array there is an object, in general I am working with Php & Larvel, and I take care of putting the results of the 2nd array ( Users to Questions ) in the first one, at the end I put the way I expect to have the data;
I tried this way, to enter the key, value to the questions array , but I got an error:
*Cannot use object of type stdClass as array*
$questions[0]["firstname"] = $user[1]->firstname
Then I tried removing the index at the top of
$questions
and it worked but it pushed it as a new index, not into the existing object:$questions["firstname"] = $user[1]->firstname
I also used
*array_push()*
&*array_merge*
but no data was displayed in the console.
1 Array questions (Here I want to add the data of the 2nd Array)
#items: array:2 [
0 => {#2012
+"question": "Como describirías un riesgo en la planta"
}
1 => {#2010
+"question": "Si hubiera un incendio como reaccionarias?"
}
]
2 array users
0 => {#2008
+"id": 7
+"firstname": "Norma"
+"lastname": "Molina"
}
1 => {#2006
+"id": 6
+"firstname": "Rodrigo"
+"lastname": "Sanchez"
}
What I try to do / expectation:
0 => {
+"question": "Si hubiera un incendio como reaccionarias?"
0 => {#2012
+"id": 7
+"firstname": "Norma"
+"lastname": "Molina"
}
1 => {#2010
+"id": 6
+"firstname": "Rodrigo"
+"lastname": "Sanchez"
}
}
1 => {
+"question": "Como describirías un riesgo en la planta"
0 => {#2012
+"id": 7
+"firstname": "Norma"
+"lastname": "Molina"
}
1 => {#2010
+"id": 6
+"firstname": "Rodrigo"
+"lastname": "Sanchez"
}
}
The purpose is that the 2 arrays are in a single arrangement, the users will always be 2, so it does not matter if they are repeated, in fact I occupy it like this and as for the questions, they can be from 1 to many
Update Add the index ' users ' or whatever index 2 would do to the questions array; this way is functional
0 => {#2012
+"question": "Como describirias un riesgo en la planta"
}
1 => {#2010
+"question": "Si hubiera un incendio como reaccionarias?"
}
"users" => {#2013
#items: array:2 [
0 => {#2008
+"id": 7
+"firstname": "Norma"
+"lastname": "Molina"
}
1 => {#2006
+"id": 6
+"firstname": "Rodrigo"
+"lastname": "Sanchez"
}
]
}
Well I was able to solve the problem, I used a Laravel helper similar to array_push , only in the helper you define the key value that you want to insert in the array, in my case my array had two indexes $questions[0] & $questions 1 , I decided to independently add one more index where I would enter the users added index:
$questions['users']
, inside this add the 2 indexes of my users$user[0]
&$user[1]
;The way I add the $user object to the $question object:
Use the Laravel helper,
array_add
the way it works is similar to array_push, only in it you can define the key, value to insert, either position that exists or a new one;Syntax example:
Result:
Applied to my problem:
Obtained result:
UPDATE To be able to push a new item to an object in a simpler way is as follows;
First we know that Laravel returns an array [] and within that array we have an object: [ { } ] and from that object we have properties or keys with their values:
Well if we wanted to add within the users array that has the object with Name, Surname properties, we would like to add question, how would it be?
It can be this way:
Then it would appear as a result like this;
More information about the helpers: