I receive the following URL
/articles?difficulty=1,2
Inside the framework I want to receive an array from that - [1,2]. Is there any method in the framework to convert params like that into an array automatically? Can the framework do that? I can do like that explode(',', $params['difficulty']) - but I'm wondering whether this can be handled by the framework.
I don't want to pass params like that:
/articles?difficulty[]=1&difficulty[]=2
There is no helper in framework Request component for converting such values, it can be easily achieved with native PHP explode function. Use:
$array = explode(',', $string);
as you suggested.
But the wrapper of explode exists - \yii\helpers\StringHelper::explode(), it has additional options for trimming and skipping empty elements, you can use it too. But most of the times using regular explode should be enough.
try this
Use the Request class.
http://www.yiiframework.com/doc-2.0/yii-web-request.html
print_r(Yii::$app->request->get()); returns all get variables in an array. It's like doing print_r($_GET); in straight php.
If you want a specific $_GET variable you access it as follows:
Yii::$app->request->get('difficulty');
In your case it would be:
$success = Yii::$app->request->get('success');
$token = Yii::$app->request->get('token');
Then after that explode it with comma, so it will get converted into array.
$array = explode(',', $success );
Related
I fetch a result in Fat Free Framework with this:
$product = new DB\SQL\Mapper($db,'products');
$product->load(array('productId=:ID',':ID'=>$productId));
Then I walk through $product using the dry() method and do some calculations to some of it's fields. I want to make the re-calculated content of the $product available in my template, so I do:
$f3->set('product_contents', $product);
Now, when in my template I do:
<repeat group ="{{#product_contents}}" value="{{#item}}">
<p>{{#item.productName}}</p>
</repeat>
I get this error:
Internal Server Error
Illegal string offset 'productName'
I discovered that my {{#product_contents}} is a mapper object and not an array, hence the error.
The question is:
how can I still use the contents of $product in my template in repeat groups?
The cast() method is there to cast a mapper object to an array:
$f3->set('product_contents', $product->cast());
Just load the result into a variable
$output = $product->load(array('productId=:ID',':ID'=>$productId));
$f3->set('product_contents', $output);
I have this code
$graph_url = "https://graph.facebook.com/100000070848126/statuses?access_token=".$params['access_token'];
$status = json_decode(file_get_contents($graph_url),true);
echo $status->data->message;
and I'm having a problem on how to output data in the array $status. I just don't know how to call the items for this feed
The second parameter of json_decode() is whether to create an associative array or not.
json_decode ( string $json [, bool $assoc = false])
You specified that you do want an array, so the way you would access the values would be like this -
echo $status['data']['message'];
If you leave out the true parameter of the json_decode() function, you'll be able to access the values in a more object oriented way like the syntax in your question.
I am creating a store in CakePHP and have added a text field which stores a json array of all the categories the store falls into.
How do I do a cake find on all stores that fall into the "gardening" category?
json encoded field:
["gardening","books-and-toys"]
I am thinking some sort of in_array() find but not quite sure.
Many Thanks
You can use the PHP method json_decode and return the JSON object as an associative array by setting the second option of that function to true. You can then call array_search for example to return the corresponding key.
<?php
$categories = json_decode($json, true);
$categoryKey = array_search('gardening', $categories);
$categoryName = $categories[$categoryKey];
?>
You can then use $categoryName in a CakePHP find() call.
I'm working on a perl script that parses reddit's JSON using the JSON module.
However I do have the problem of being very new to both perl and json.
I managed to parse the front page and subreddits successfully, but the comments have a different structure and I can't figure out how to access the data I need.
Here's the code that successfully finds the "data" hash for the front page and subreddits:
foreach my $children(#{$json_text->{"data"}->{"children"}}) #For values of children.
{
my $data = $children->{"data"}; #accessing each data hash.
my %phsh = (); #my hash to collect and print.
$phsh{author} = $data->{"author"};#Here I get the "author" value from "data"
*Etc....
This successfully gets what I need from http://www.reddit.com/.json
But when I go to the json of a comment, this one for example, it has a different format and I can't figure out how to parse it. If I try the same thing as before my parser crashes, saying it is not a HASH reference.
So my question is: How do access the "children" in the second JSON? I need to get both the data for the Post and the data for the comments. Can anybody help?
Thanks in advance!
(I know it may be obvious, but I'm running on very little sleep XD)
You need to either look at the JSON data or dump the decoded data to see what form it takes. The comment data, for example is an array at the top level.
Here is some code that prints the body field of all top-level comments. Note that a comment may have an array of replies in its replies field, and each reply may also have replies in turn.
Depending on what you want to do you may need to check whether a reference is to an array or a hash by checking the value returned by the ref operator.
use strict;
use warnings;
binmode STDOUT, ':utf8';
use JSON;
use LWP;
use Data::Dump;
my $ua = LWP::UserAgent->new;
my $resp = $ua->get('http://www.reddit.com/r/funny/comments/wx3n5/caption_win.json');
die $resp->status_line unless $resp->is_success;
my $json = $resp->decoded_content;
my $data = decode_json($json);
die "Error: $data->{error}" if ref $data eq 'HASH' and exists $data->{error};
dd $data->[1]{data}{children}[0];
print "\n\n";
my $children = $data->[1]{data}{children};
print scalar #$children, " comments:\n\n";
for my $child (#$children) {
print $child->{data}{body}, "\n";
}
I have a stored procedure that I call upon using 'core_read' and query method. The results are then gathered using fetchAll(PDO::FETCH_ASSOC).
The data comes out perfectly. I can do a foreach on the array, and access data by array keys ($row['name']).
I would like to convert the associative array into a Varien_Object, so I could access the data using $row->getName() notation... Keeping it in Magento style... How would I perform such a conversion, if possible?
Pass your array to the constructor of Varien_Object
$object = new Varien_Object($array);
See the code for the constructor in lib/varien/object
I think you just can use:
foreach($rows as $row) {
$object = new Varien_Object();
$object->setData($row);
}
Thank you for your suggestions, and I think it would have worked if I had one line coming back from the stored procedure. Here's what I ended up doing:
foreach($rows as $row) {
$orders[] = new Varien_Object($row);
}