I have this array that i used json_decode to create. This is my code:
$obj = json_decode($json);
$results = $obj->{"results"}; // This selects the results section
echo $results[artistName];
$results is the array
The array looks like this:
array(1) {
[0]=> object(stdClass)#5 (8) {
["wrapperType"]=> string(6) "artist"
["artistType"]=> string(6) "Artist"
["artistName"]=> string(5) "Human"
["artistLinkUrl"]=> string(56) "https://itunes.apple.com/us/artist/human/id35761368?uo=4"
["artistId"]=> int(35761368)
["amgArtistId"]=> int(1173165)
["primaryGenreName"]=> string(3) "Pop"
["primaryGenreId"]=> int(14)
}
}
But it does not echo anything. Please help.
It's still an object, so you need to access the key like this.
echo $results[0]->artistName;
echo $results[0]->artistName
Notice, u have array of stdClasses
Related
I'm new to arrays. I'd like to know if you can add data to the array by name.
This would be an example of what I want to achieve
example:
$guitars = ['names',['Warvick', 'Gibson', 'Fender']];
$guitars[names][] = "Ibanez";
echo "<pre>";
var_dump($guitars);
echo "</pre>";
result
WARNING Use of undefined constant names - assumed 'names' (this will throw an Error in a future version of PHP) on line number 3
array(3) {
[0]=>
string(5) "names"
[1]=>
array(3) {
[0]=>
string(7) "Warvick"
[1]=>
string(6) "Gibson"
[2]=>
string(6) "Fender"
}
["names"]=>
array(1) {
[0]=>
string(6) "Ibanez"
}
what I want is to add the data "ibanez" to the array "names" but nevertheless create a new one.
I need it to stay this way. Is there any way to access the data directly by the name "names"?
array(2) {
[0]=>
string(5) "names"
[1]=>
array(4) {
[0]=>
string(7) "Warvick"
[1]=>
string(6) "Gibson"
[2]=>
string(6) "Fender"
[3]=>
string(6) "Ibanez"
}
}
You want to use associative arrays. In this case, for example, use the arrow token to associate the guitar names with the "names" key.
$guitars = ['names' => ['Warvick', 'Gibson', 'Fender']];
$guitars['names'][] = "Ibanez";
echo "<pre>";
var_dump($guitars);
echo "</pre>";
I'm currently getting a JSON response from a company's API and converting it into a PHP array like this:
$api_url = file_get_contents('http://example.com');
$api_details = json_decode($api_url, true);
When I run var_dump on $api_details, I am getting this:
array(2) {
["metadata"]=>
array(5) {
["iserror"]=>
string(5) "false"
["responsetime"]=>
string(5) "0.00s"
["start"]=>
int(1)
["count"]=>
int(99999)
}
["results"]=>
array(3) {
["first"]=>
int(1)
["result"]=>
array(2) {
[0]=>
array(4) {
["total_visitors"]=>
string(4) "3346"
["visitors"]=>
string(4) "3249"
["rpm"]=>
string(4) "0.07"
["revenue"]=>
string(6) "0.2381"
}
[1]=>
array(4) {
["total_visitors"]=>
string(6) "861809"
["visitors"]=>
string(6) "470581"
["rpm"]=>
string(4) "0.02"
["revenue"]=>
string(7) "13.8072"
}
}
}
}
I'm trying to do 2 things and can't figure out how to do either with a multidimensional array.
I need to check to see if metadata > iserror is false. If it is not false, I want to show an error message and not continue with the script.
If it is false, then I wants to loop through the results of results > result and echo the total_visitors, visitors, etc for each of them.
I know how to echo data from array, I guess I'm just getting confused when there's multiple levels to the array.
Anyone that can point me in the right direction would be much appreciated :)
You can iterate over arrays using foreach. You can read up on it here: http://php.net/manual/en/control-structures.foreach.php
Since you're using associative arrays, your code will look something like this:
if ($arr['metadata']['iserror']) {
// Display error here
} else {
foreach($arr['results']['result'] as $result) {
echo $result['total_visitors'];
echo $result['visitors'];
}
}
You'll have to tweak the code to fit exactly what you're doing, but this should get you over the line.
Hope that helps!
<?php
$array = json_decode('{"image":"9794948495.jpg","image":"9703471814.jpg","image":"9711995133.jpg"}');
echo json_encode($array,JSON_FORCE_OBJECT);
?>
My code is like that.
And it result just like :
{"image":"9711995133.jpg"}
Could you guys help me to have :
{"image":"9794948495.jpg","image":"9703471814.jpg","image":"9711995133.jpg"}
Thank you very much.
You must declare image as an array:
{"image":["9794948495.jpg","9703471814.jpg","9711995133.jpg"]}
Your format is overwriting each time the image key and you end up with the last parsed value :
{"image":"9794948495.jpg","image":"9703471814.jpg","image":"9711995133.jpg"}
when you will var_dump the array you will have:
array(1) { ["image"]=> array(3) { [0]=> string(14) "9794948495.jpg" [1]=> string(14) "9703471814.jpg" [2]=> string(14) "9711995133.jpg" } }
thus the original: $images = array('image'=>array("9794948495.jpg","9703471814.jpg","9711995133.jpg"));
I'm new to php, and I'm wording how to return an associative array of JSON data type? So here's my php code:
$JSON = file_get_contents($url);
// echo the JSON (you can echo this to JavaScript to use it there)
//echo $JSON;
// You can decode it to process it in PHP
$data = json_decode($JSON,true);
var_dump($data);
Here's a snippet of what the data looks like when I dump it to the screen:
array(2) {
["product"] => array(1) {
[0]=> array(7) {
["styles"]=> array(13) {
[0]=> array(7) {
["price"]=> string(6) "$64.95"
["productUrl"]=> string(46) "http://www.zappos.com/product/7515478/color/25"
["percentOff"]=> string(2) "0%"
["styleId"]=> string(7) "1788226"
["imageUrl"]=> string(65) "http://www.zappos.com/images/z/1/7/8/8/2/2/1788226-p-DETAILED.jpg"
["color"]=> string(6) "Almond"
["originalPrice"]=> string(6) "$64.95"
}
Now how would I go about accessing the first array element?
I've tried:
echo $data[0][0]['orignalPrice'];
and it does not work. Could someone please help? Also is there a decent debugger for php on a mac? Appreciate any help.
Try that $data['product'][0]['styles'][0]['originalPrice']
Would also recommend you to have a look on a documentation, to learn more about arrays.
In PHP when I do
var_dump($galleryCategoriesThumb);
Output is
array(1) {
[0]=>
array(1) {
["Gallery"]=>
array(3) {
["id"]=>
string(3) "190"
["photofile"]=>
string(6) "50.jpg"
["gallery_category_id"]=>
string(2) "58"
}
}
}
When I do
var_dump($galleryCategoriesThumb["photofile"]);
I get NULL output. I tried various other options also.
All I want to do is echo ["photofile"]. Please help.
Try $galleryCategoriesThumb[0]['Gallery']["photofile"]
Try var_dump($galleryCategoriesThumb[0]["Gallery"]["photofile"]);.
It's a 3 dimensions array.
It is expected that var_dump($galleryCategoriesThumb["photofile"]) returns NULL because "photofile" key does not exist in $galleryCategoriesThumb.
To access "photofile", you need a 'full path' in the array as posted by others
var_dump($galleryCategoriesThumb[0]["Gallery"]["photofile"]);.