I try to update nested parameter in document MongoDb:
$this->collection->update(
["prototype_id" => $id],
["$set" => ["parameters" => $newdata]],
["upsert" => true, "multiple" => true]);
In result I get error:
zero-length keys are not allowed, did you use $ with double quotes?
Where $newdata is:
array:1 [
"5920d086470e6cb30e3c986c" => array:1 [
"Acceleration" => "2"
]
]
Use dot notation to add/overwrite field in embedded document.
Something like
$parameter_id = "5920d086470e6cb30e3c986c";
['$set' => ["parameters.".$parameter_id => ["Acceleration" => "2"]]];
Alternatively, define parameters as embedded array and you can $push to insert whole embedded document.
Related
I faced some problem when I get second array value and pass it to a variable.
array:0 [
"student_id" => 1212
"class" => array:1 [
"id" => 550
]
]
Normally $student= array_get($data, 'student_id');
able to get value.
Now I'm trying to get class id something like $class= array_get($data, 'class', 'id');
but it pop-up this error;
To get values from nested array use the dot notation.
$class = array_get($data, 'class.id');
How to put value on first element in laravel collection ? Something like that $collection->put('foo', 1) but adding value to the first element.
Collection {#376
#items: array:1 [
0 => array:9 [
"id" => 83
"status" => "offline"
"created_date" => "Oct 31, 2018"
// add foo => 1 here
]
]
}
I suspect there's a cleaner way to do this, but this is the best I could come up with at the moment. You could also use map or transform with a comparison run on the key value that gets sent to their closures, but that would end up cycling through all elements of the array despite you knowing the specific one you want to target.
$collection = collect([
[
'id' => 83,
'status' => 'offline',
'created_date' => 'Oct 31, 2018'
]
]);
$firstKey = $collection->keys()->first(); //This avoids the unreliable assumption that your index is necessarily numeric.
$firstElement = $collection->first();
$modifiedElement = array_merge($firstElement, ['foo1' => 1]);
$collection->put($firstKey, $modifiedElement);
use this
$data = Model::all();
$data[0]->foo = 'your data here';
I am currently trying to learn Laravel and PHP in general.
I need to be able to import an Excel file, then get the data from that file. Currently, the import part works and I can see the data from the file. However, I am having trouble accessing the data.
I've read about the toArray() function in Laravel, and is using that as below:
$data = Excel::load($path, function($reader) {})->skipColumns(2)->get();
$data = $data->toArray();
foreach ($data as $key => $value) {
//We only need some of the available data.
echo $value->next_milestone_desc_cur._comp._incl_rltd;
echo $value->shipment_id;
}
Above code gives me below error:
Trying to get property 'next_milestone_desc_cur' of non-object
Below is an output from the array, which I have generated using dd($value):
array:543 [▼
0 => array:20 [▼
"next_milestone_desc_cur._comp._incl_rltd" => "005. DK - Add DropMode/Local Trp Org in Pickup Tab"
"milestone_cur._comp._sequence_no" => "005"
"cur._comp._export_validation" => "NOT OK"
"shipment_id" => "SBRY0162091"
"consol_id" => "CDK327188" ]
1 => array:20 [▼
"next_milestone_desc_cur._comp._incl_rltd" => "005. DK - Add DropMode/Local Trp Org in Pickup Tab"
"milestone_cur._comp._sequence_no" => "005"
"cur._comp._export_validation" => "NOT OK"
"shipment_id" => "SBRY0162124"
"consol_id" => "CDK327221"
]
What am I doing wrong here? I have also tried
echo $value[0]->next_milestone_desc_cur._comp._incl_rltd;, however this doesn't work either.
You are trying to access an array as an object hence your error and to address you second point, you need to use a nested loop to then access the sub-array.
Your array looks something like this:
$array = [
0 => [
0 => [
'test' => 'value'
],
1 => [
'test' => 'something'
]
],
1 => [
0 => [
'test' => 'value'
],
1 => [
'test' => 'something'
]
]
];
You need the following loop:
foreach ($array as $key => $nested) {
foreach ($nested as $nested_value) {
// You have access to your inner arrays.
echo $nested_value['test'] . ' | ';
}
}
Where your nested loops $nested would be $value.
Live Example
Repl
Your output from dd($value) shows that $value is an array. So you cannot use arrow notation that is for objects. Change these lines:
echo $value->next_milestone_desc_cur._comp._incl_rltd;
echo $value->shipment_id;
To:
echo $value[0]["next_milestone_desc_cur._comp._incl_rltd"];
echo $value[0]["shipment_id"];
this might be a bit of a generic title, but I will try to explain my problem the best way I can.
So I've got this type of associative array:
Array
(
[ICO_0] => checked
[Investment_0] => checked
[Investment_1] => checked
[Marketing_1] => checked
)
What I would like to do is to divide it into multiple arrays based on numbers that are attached to the end of the key ( 0, 1 ... ). Basically I'd like to get a new array to look like this:
Array(
Array(
[ICO_0] => checked
[Investment_0] => checked
[Token Sale_0] => checked
),
Array(
[Investment_1] => checked
[Marketing_1] => checked
)
)
I've tried approaching this issue with array_chunk but couldn't get it to work.
I'll need this output since I want to sort those nested arrays based on items that they're holding, starting from the highest number of items.
Thanks!
You can use several different methods to accomplish this. One method is to loop through and explode the key name if you know there will always be an underscore, grab the last number, and use that as your index:
$results = [
"ICO_0" => "checked",
"Investment_0" => "checked",
"Investment_1" => "checked",
"Marketing_1" => "checked",
"Investment_2" => "checked",
"Marketing_2" => "checked"
];
foreach($results as $key => $value){
$ex = explode('_', $key);
$new_result[end($ex)][$key] = $value;
}
Which for me returns the following:
array:3 [▼
0 => array:2 [▼
"ICO_0" => "checked"
"Investment_0" => "checked"
]
1 => array:2 [▼
"Investment_1" => "checked"
"Marketing_1" => "checked"
]
2 => array:2 [▼
"Investment_2" => "checked"
"Marketing_2" => "checked"
]
]
I have an array like this :
[▼
0 => array:47 [▼
"ProductID" => "37883"
"ProductCode" => "G-49211"
"ProductName" => "Preludes"
"StockStatus" => "2"
"LastModified" => "2014-02-27T09:50:00-08:00"
"LastModBy" => "1"
"ProductPopularity" => "110"
"AutoDropShip" => "N"
1 => [
"ProductID" => "37884"
"ProductCode" => "G-49212"
"ProductName" => "Preludes "
"StockStatus" => "2"
"LastModified" => "2014-02-27T09:50:00-08:00"
"LastModBy" => "1"
"ProductPopularity" => "110"
"AutoDropShip" => "N"
]
]
but all values of this array are strings. I want to iterate over this array and cast its values to their original types. if ProductID is integer I want to convert it to integer. Convert dates to real date blabla.
Can this be done ?
There isnt any real function in php that can parse away array elements based on their data types, but still you can do it using preg_match pattern matching techniques, by recognizing the characters in each element and then type converting them
idea :- use a foreach loop and take each element and apply preg_match to check what kind of data that is and then set a data type for it :)