I would need to insert an associative array into my existing code, to make an HTTP request.
My code at the moment:
$payload =[
'method_id' => 2,
'api_key' => 5,
];
$res = $client->post('some.website', [,
'form_params' => [
foreach($this->payload as $key => $s_key) {
$key => $s_key;
}
],
]);
How to make now sure, each element of the $payload array is inserted into the form_params array?
I tried using:
foreach ($this->payload as $s_key => $key) {
//?!
}
But I don't know how to proceed inside the form_params element?
Using the payload array directly inside form elements is resulting into this:
"form_params" => [
0 => array:2 [
"method_id" => 2
"api_key" => 5
]
]
What I would need is something like this:
"form_params" => [
"method_id" => 2
"api_key" => 5
]
You should just be able to just use the $payload variable directly, like so:
$res = $client->post('some.website', [
'form_params' => $payload,
]);
Related
I have stored a document set in MongoDB like below where ID is generated automatically through PHP
{
"_id": {
"$oid": "5ff745237d1b0000860007a6"
},
"user": "5ff741fb7d1b0000860007a4",
"name": "Test",
"slug": "fed73b70d080584c21e0a4353825ef91",
"category": "5fef4a467d1b000086000745",
"images": [{
"100x128": "test/year/month/image_da4181762396a31ff44f5ddc08ce6186.jpeg",
"200x256": "test/year/month/image_da4181762396a31ff44f5ddc08ce6186.jpeg",
"300x385": "test/year/month/image_da4181762396a31ff44f5ddc08ce6186.jpeg",
"500x642": "test/year/month/image_da4181762396a31ff44f5ddc08ce6186.jpeg"
},
{
"100x128": "test/year/month/image_da4181762396a31ff44f5ddc08ce6186.jpeg",
"200x256": "test/year/month/image_da4181762396a31ff44f5ddc08ce6186.jpeg",
"300x385": "test/year/month/image_da4181762396a31ff44f5ddc08ce6186.jpeg",
"500x642": "test/year/month/image_da4181762396a31ff44f5ddc08ce6186.jpeg"
}],
}
Now the problem is I want to get value from images object from key 100x128 only from the first array.
I am using the below code to achieve
$productsArray = $collection->aggregate(
[
[
'$match' => [
'user_id' => $user_id
]
],
[ '$unwind' => '$images' ],
[ '$unwind' => '$images.100x128' ],
[
'$project' => [
'name' => 1,
'image' => '$images'
]
]
]
);
and I always get an output as below
MongoDB\Model\BSONDocument Object
(
[storage:ArrayObject:private] => Array
(
[_id] => MongoDB\BSON\ObjectId Object
(
[oid] => 5ff745357d1b0000860007a7
)
[name] => Test 1
[image] => MongoDB\Model\BSONDocument Object
(
[storage:ArrayObject:private] => Array
(
[100x128] => test/year/month/image_da4181762396a31ff44f5ddc08ce6186.jpeg
[200x256] => test/year/month/image_da4181762396a31ff44f5ddc08ce6186.jpeg
[300x385] => test/year/month/image_da4181762396a31ff44f5ddc08ce6186.jpeg
[500x642] => test/year/month/image_da4181762396a31ff44f5ddc08ce6186.jpeg
)
)
)
)
I am not sure how to get just that specific value. Where am i going wrong here?
As far as i am getting your question it looks like you would need to understand the mongo concept. So you might get an array representation in UI or in PHP but at the end of day it is object.
Look closely the format that you have posted there is no array but multiple {} in that object just like JS so forget about array concept here.
If my understanding is correct this is what you are looking for
$productsArray = $collection->aggregate(
[
[
'$match' => [
'user_id' => $user_id
]
],
[ '$unwind' => '$images' ],
[ '$unwind' => '$images.100x128' ],
[
'$project' => [
'name' => 1,
'image' => '$images.100x128'
]
]
]
);
Whatever you have googled or used it is correct just you need to add the second unwind value i.e $images.100x128 instead of $images in the $project variable.
You can understand this way that $unwind variable uses recursive logic to get output as you want. Like let's say you have an array inside an array first it will get values from internal array then it will come to parent array and then so on. So the last value in $unwind variable should be used.
I am not sure if it will work on multiple objects but as you said you just need the first one. This should do the charm.
This is an array of PHP database output:
[
[hi]=>array(
[text] => 'ok'
)
[work]=>array(
[text] => 'usa'
)
[city]=>array(
[text] => 'newyork'
)
]
How do I convert it to look like this:
[
[hi]=> 'ok'
[work]=> 'usa'
[city]=> 'newyork'
]
You just want to loop over the array and build your newly formatted array.
<?php
$arr = [
'please' => [
'text' => 'it'
],
'use' => [
'text' => 'will'
],
'google' => [
'text' => 'help'
]
];
$formattedArr = [];
foreach ($arr as $k => $v) {
$formattedArr[$k] = $v['text'];
}
Another way using some array sorting methods to get us a one liner:
$formattedArr = array_combine( array_keys($arr), array_column($arr, 'text'));
array_combine is a php function that creates a new array using an array for keys and an array for values.
Simply enough, array_keys gets - you guessed it - the keys of the array.
array_column creates an array of a selected property from an inner array, so in this case, it goes plucks the 'text' values from the inner arrays.
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"];
I'm trying to convert an array in string in PHP so that I can store it into mySQL fields and reversely I can fetch:
Following is the data format:
array:2 [
0 => array:2 [
"year" => "234"
"amount" => "2387"
]
1 => array:2 [
"year" => "23"
"amount" => "324"
]
]
I'm trying something like this:
$var = implode(',', $data['revenues']);
I tried using implode but it is not working. Please help me out.
Edit
I tried this before:
public function store(Request $request)
{
$this->validate($request, Research::$create_validation_rules);
$data = $request->only(['coverage_date', 'company_id', 'contact_id', 'type', 'reco', 'target', 'projections', 'revenues', 'ebitas', 'profits']);
$data['coverage_date'] = Carbon::parse($request->coverage_date)->toDateTimeString();
$data['revenues'] = json_encode($data['revenues']);
$data['projections'] = json_encode($data['projections']);
$data['profits'] = json_encode($data['profits']);
$research = Research::create($data);
if($research)
{
return response()->json(['msg' => 'Successfully Created'], 200);
}
else
{
return response()->json(['msg' => 'Something went wrong'], 450);
}
}
Error which I'm getting is:
Use json_encode
example
$array = [["year" => "234","amount" => "2387"],["year" => "23","amount" => "324"]];
/*
Depending on PHP Version you can pass additional parameters
in json_encode for example json_encode($array,JSON_PRETTY_PRINT)
*/
$string = json_encode($array);
echo $string;
check this on PHP sandbox
I have a PHP variable I need to convert to JSON string.
I have following PHP code:
$username="admin";
$password="p4ssword";
$name="Administrator";
$email="myname#smsfree4all.com"
$params=compact('username', 'password','name','email', 'groups');
json_encode($params);
This works fine. But what I am not sure about is how do I encode the properties in PHP with nested key value pairs shown below:
{
"username": "admin",
"password": "p4ssword",
"name": "Administrator",
"email": "admin#example.com",
"properties": {
"property": [
{
"#key": "console.rows_per_page",
"#value": "user-summary=8"
},
{
"#key": "console.order",
"#value": "session-summary=1"
}
]
}
}
What is this with # before key value?
Something like this should do it
$username="admin"; //more variables
$params=compact('username' /* more variables to be compacted here*/);
$params["properties"] = [
"property" => [
[
"#key" => "console.rows_per_page",
"#value"=> "user-summary=8"
],
[
"#key"=> "console.order",
"#value"=> "session-summary=1"
]
]
];
echo json_encode($params);
The manual has more examples you can use
Notice that:
A key~value array is encoded into an object
A regular array (array of arrays here) is encoded into an array
Those are all the rules you need to consider to encode any arbitrary object
Something like this perhaps?
$properties = [
'property' => [
['#key' => 'console.rows_per_page', '#value' => 'user-summary=8'],
['#key' => 'console.order', '#value' => 'session-summary=1']
]
];
It's difficult to tell what you're asking.
You can nest in PHP using simple arrays, very similar to JavaScript objects:
$grandparent = array(
"person1" => array(
"name" => "Jeff",
"children" => array(
array("name" => "Matt"),
array("name" => "Bob")
)
),
"person2" => array(
"name" => "Dillan",
"children" => array()
)
);