Basiclly I have an url that have a parameter.
So, I need to access those parameter using GET. This is the GET that coming:
Array
(
[r] => hanwa/incoming-pipe/assign-incoming
[1] => Array
(
[min_urut] => 1
[max_urut] => 44
)
[_] => 1496645704980
)
In docs, docs,
We can use like this :
echo $request->get('min_urut');
But, I got nothing. Please Advise.
Use Concept of ArrayHelper class :-> http://www.yiiframework.com/doc-2.0/guide-helper-array.html#getting-values
ex:
$data = ArrayHelper::getValue($request->get(), 'Temp.yourvalue.yourindex');
Main use of $request->get() method mainly returns to you a value from $_GET,
so example is
$temp = $request->get('Temp'); // Here $temp variable contains $_GET['Temp']
$data = $temp['yourvaluename'][0];
Judging by the fact that you have an array with the key [r] you have an associative array, method get() you will not get it, you need to do it just like this ..
echo $request->get()[1]['min_urut'];
Related
I'm working on a Laravel project that implements react-jsonschema-form and I need to convert the values saved in the database to an associative array so I can pluck certain values from it. However I am getting strange results when doing so.
Here is the code I use to grab the JSON data from the table and then convert to an array:
$form = Form::where('id', $formId)->get();
$converted = json_decode($form[0]->form_data, true);
$formArray = print_r($converted, 1);
return $formArray;
For testing purposes I am simply rendering the data in the browser.
The result from the above return is:
Array
(
[1] => Array
(
[1.1] => New
[1.2] => Ms
[1.3] => Isobel Fleming
[1.4] => Array
(
[uprn] => 52375918
[address_1] => Fake Street
[address_2] =>
[address_3] =>
[town_city] => BRISTOL
[postcode] => BS1 3KE
)
[1.5] => 0129711011
[1.6] => 0800999111
[1.7] => 0781100022
[1.8] => isobelfleming#jourrapide.com
)
)
which is great. However when I try and access anything from it like:
return $formData[1][1.1]
I get:
String offset cast occurred
If I try using a string:
return $formData[1]['1.1']
I get:
Illegal string offset '1.1'
So I am not sure what to do to access this data. The problem is, although it's not ideal to have the associate keys with decimals in them, this is the way the schema is set up and it's several thousand lines long - this is just a snippet of the form data.
Is there anything that can be done in order to get the data from this array?
I figured it out. It seems like the following line was the problem:
print_r($converted, 1);
It didn't need to be printed as an array, I should have just used the $converted variable to access the data like so:
return $converted[1]['1.1'];
I've got the following:
$srv = DB::table('ads')
->join('ad_service','ad_service.ad_id', '=', 'ads.id')
->select('ads.id')
->whereIn('ads.id',[45789,46531])
->get();
Log::info($srv);
and log info gives me
49 => stdClass::__set_state(array(
'id' => '46531',
)),
50 =>
stdClass::__set_state(array(
'id' => '46531',
)),
51 =>
stdClass::__set_state(array(
'id' => '46531',
)),
What I would like to have:
return the entry ONCE, there are multiple entries with this id in the "ad_service" table , and i would like to retrieve it once.
its a many to many relationship.
also biggest problem, what is this stdclass set state?
I was expecting something like:
array('id' => '46531', ...,)
thanks guys.
EDIT:
Solved it using Eloquent:
$srv = Ad::whereIn('id',[46696,48982,...MORE IDS HERE])->get();
this returns not an object but a collection itself. i do not really know what #ourmandave meant with "you get a collection with std class objects" i mean obviously i could see that but i do not really know why i got it, and why i get it as i wish using eloquent.
Any answer to that would be appreciated:
Why does DB:: return this std class stuff and Eloquent returns the "wanted format"?
If you just want one record you could try:
$srv = DB::table('ads')
->join('ad_service','ad_service.ad_id', '=', 'ads.id')
->select('ads.id')
->whereIn('ads.id',[45789,46531])
->first();
Change the get to a first(). The reason you are getting multiple records is because get() returns a laravel collection of your ads.
You're actually getting back a lararvel Collection.
From the docs (https://laravel.com/docs/5.8/queries#retrieving-results)
The get method returns an Illuminate\Support\Collection containing the
results where each result is an instance of the PHP stdClass object.
You may access each column's value by accessing the column as a
property of the object:
foreach ($users as $user) {
echo $user->name;
}
There's a lot of methods they provide for collections to treat them like arrays.
(https://laravel.com/docs/5.8/collections#available-methods)
You might also try the distinct() method to get back one of multiples.
$srv = DB::table('ads')
->join('ad_service','ad_service.ad_id', '=', 'ads.id')
->select('ads.id')
->whereIn('ads.id',[45789,46531])
->distinct()
->get();
To convert a model and its loaded relationships to an array, you should use the toArray method. This method is recursive, so all attributes and all relations (including the relations of relations) will be converted to arrays
$srv = DB::table('ads')
->join('ad_service','ad_service.ad_id', '=', 'ads.id')
->select('ads.id')
->whereIn('ads.id',[45789,46531])
->get()
->toArray();
Print array:
Log::info($srv);
I am not getting all the key values of and array using foreach, its giving only first key value.
$order_id = DB::table('order')->where('delivery_boy_id', $delivery_boy_id)->where('is_accept', 1)->whereRaw('delivery_completed_at < time_of_assignment')->get();
When i run this code i get two key value
array:2 [0 => {#325
+"id": 37
+"order_id": 8261
+"delivery_boy_id": 8}1 => {#326
+"id": 38
+"order_id": 8261
+"delivery_boy_id": 8]
After using this json response i am getting the response of only one key value
foreach($order_id as $value){ $values = $value) };
$this->response['items'] = $order_id;
return json_encode($this->response);
check this
$order_id = DB::table('order')->where('delivery_boy_id', $delivery_boy_id)->where('is_accept', 1)->whereRaw('delivery_completed_at < time_of_assignment')->get();
// return $order_id;(returns json array)
$data = [];
foreach($order_id as $order)
{
var_dump($order);
//in first loop first array will come
//second loop second array
$data[]=[
'id' => $order['id'],
'..' => '..'
];
}
return $data;
}
Dd means dump and die so it stops execution. If you dd in a for each loop, you will only get the current value in the loop (I.e the first one) because it will halt execution and you will never reach the second loop through. Use var_dump to allow execution to continue and dump variables.
If you simply want to return the Json to the user, you can just return the result of your query as laravel handles serialisation for you. You don't need to loop over the result. See the docs:
https://laravel.com/docs/5.1/eloquent-serialization
You are overwriting $values in your loop. So every time it passes through the for each loop, it overwrites $values with the current $value so it will only ever contain 1 object. You don't need to do the serialisation manually, use the laravel helper methods
Also in your code snippet you are looping over $order, but the result of your query is assigned to $order_id? I'm assuming that's due to abbreviating your code
I'm finding list of Areas for my CakePHP 2.x website and it supports, JSON output as below with find all method:
$this->Area->find('all', array('fields' => array('id', 'name', 'order'), 'conditions' => array('Area.status' => 1)));
Below is my JSON output:
[{"Area":{"id":"2","name":"Area 1","order":"1"}},{"Area":{"id":"3","name":"Area 2","order":"1"}}]
Now Is that possible for me to remove Area tag which is repeating everytime?
Any patch for same? Do let me know if any suggestions / ideas.
on your view for output json write this:
echo json_encode(Set::extract('/Area/.', $area));
For me works fine.
CakePHP Provides some in-built library functions for data extraction from result set and output same as JSON format.
// initialize your function to render false and response type json
$this->autoRender = false; // no view to render
$this->response->type('json');
// Remove Area from array and encode
$area = Set::extract('/Area/.', $area);
$json = json_encode( $area);
$this->response->body($json);
Hope it helps !
This is array in external php file
Array
(
[0] => Array
(
[CurrencyAbbreviation] => AUD
[CurrencyRate] => 0.50600000
[DateOfCurrencyRate] => 2013-07-11
[Units] => 1
)
)
Want to call for example CurrencyRate
Tried code like $('#currency_load').html(data.CurrencyRate); (Chrome F12 get 404 error).
For comparison. If php array is like this
$arr = array ('item1'=>"I love jquery4u",'item2'=>"You love jQuery4u",'item3'=>"We love jQuery4u");
and json $('#currency_load').html(data.item1);
then all works.
Do I need somehow to modify php array?