i have php array like (( print_r($fdata); ))
Array
(
[status] => YES
[data] => Array
(
[0] => Array
(
[0] => Array
(
[0] => Need
[1] => Am need
)
)
[1] => Array
(
[0] => Array
(
[0] => 0
[1] => No deductibles and no copayments.
)
[1] => Array
(
[0] => 1
[1] => No lifetime limit—policy won't terminate based on number or dollar amount of claims paid.
)
)
[2] => Array
(
[0] => Array
(
[0] => Volvo
[1] => 22
[2] => 18
)
[1] => Array
(
[0] => Volvo
[1] => 22
[2] => 18
)
)
[3] => Array
(
[0] => Array
(
[0] => Volvo
[1] => 22
[2] => 18
)
[1] => Array
(
[0] => Volvo
[1] => 22
[2] => 18
)
)
)
)
i want to encode it to json , but when i pass it to "json_encode" function it print blank result !
about my php code
i have declared few php array in which i load data from database
$fdata = array(); // final data to process
$product = array(); // product info
$adv_arr = array(); // advantages
$benefits_arr = array(); // benefits
$limits_arr = array(); // limits
$terms_arr = array(); // terms
after loading to arrays , i push them to one another array like
$ffdata = array();
array_push($ffdata , $product ,$adv_arr,$benefits_arr,$terms_arr);
and then it end i try to encode but no result
$fdata['status'] = "YES";
$fdata['data'] = $ffdata;
echo json_encode($fdata);
am trying to produce json data result like :: http://s1.postimg.org/efvvx74xr/C29_CA6_EA8_CAA946_E44076_CA72_B98502932_BA2_A6_DE4517_FB.jpg
sample data :: http://www.datafilehost.com/d/58c5d154
If json_encode() encounters an error when encoding a data set, it returns false, which will not echo.
In order to determine if the operation was erroneous, you can use json_last_error() and json_last_error_msg() to determine what went wrong.
For example:
$json = json_encode($data);
if (json_last_error() !== JSON_ERROR_NONE) {
throw new RuntimeException(json_last_error_msg());
}
This should provide some more meaningful information if something goes wrong.
At this point, if you are getting a WSOD instead of any output then you might have a different problem; e.g: a fatal or parse error. In this case you should ensure that error reporting is enabled while developing (and, crucially, ensure it's disabled in your production environment!).
Generally, the easiest way to do this is to add the following to the top of your script:
<?php
error_reporting(E_ALL|E_STRICT);
ini_set('display_errors', true);
Hope this helps :)
Related
I am using get_post_meta like below:
$job_owner = get_post_meta($post->ID, 'assignedUsers', true);
That returns the following:
(
[total] => 1
[data] => Array
(
[0] => stdClass Object
(
[id] => 440968
[firstName] => John
[lastName] => Doe
[email] => john#website.com
)
)
)
I am trying to grab the values from the object but catch an error each time I simply try and use echo $job_owner. Error is - Object of class stdClass could not be converted to string
I have tried to use:
$array = json_decode(json_encode($job_owner), true);
Which returns the arrays:
Array
(
[total] => 1
[data] => Array
(
[0] => Array
(
[id] => 440968
[firstName] => Megan
[lastName] => Collins
[email] => megan#bridgeviewit.com
)
)
)
But I cannot seem to get anything to return using echo $array[0]->id etc...
My ideal scenario is to use the array values as variables to use throughout the theme.
So with echo $array[0]->id are trying to get the 0th element of that array which is total. So echo $array[0]->id should fail but echo $array[0] should return 1. If you change your request to $array[data][0]->id or $array[1][0]->id that should get you the value you are looking for - the id of the first element in the data bit.
$array[data][0]->id
I have simple API call for themoviedb.org and I get results by external id (imdb_id), but I can't parse and get correct value.
I just want get value of ID --> [id] => 18773
So how I can get stored only themoviedb ID from API?
My code:
$url = "https://api.themoviedb.org/3/find/".$imdbId."?api_key=$apikey&language=en-US&external_source=imdb_id";
$content = file_get_contents($url, false);
// Tried
$json = json_decode($content, true);
echo $json['id']; // return nothing?
// Testing (check below array return)
$result = array_values(json_decode($content, true));
print("<pre>".print_r($result,true)."</pre>");
Array from $result
Array
(
[0] => Array
(
[0] => Array
(
[adult] =>
[backdrop_path] => /iSaKc4Nu7hTQDAvJigUVmNwTkj6.jpg
[genre_ids] => Array
(
[0] => 27
[1] => 9648
[2] => 53
)
[id] => 18773
[original_language] => en
[original_title] => Doppelganger
[overview] => A woman moves from NYC to LA after a murder, in which she is implicated. She is followed by what is apparently her evil alter- ego. She moves into a room for rent by a writer, and he begins having an affair with her, but after some strange things happen, he's not so sure if the affair is with her or her doppelganger.
[poster_path] => /kdMJhjqAlwJekpp8jGu6Lk3Tfy6.jpg
[release_date] => 1993-03-01
[title] => Doppelganger
[video] =>
[vote_average] => 5
[vote_count] => 56
[popularity] => 5.931
)
)
[1] => Array
(
)
[2] => Array
(
)
[3] => Array
(
)
[4] => Array
(
)
)
According to the documentation: https://developers.themoviedb.org/3/find/find-by-id you can get the id value by something like this $json['movie_results'][0]['id'].
But as you can see, the object movie_results is an array so you may have there more than one result or even no results.
So here are some tips what you should consider in your code:
What to do if there are more than one results?
What to do if there are no result at all?
If there is only one result you can safely get the ID.
Working on a personal project that will pull results from an API with full details of each pokemon.
So far I got the contents of the URL and returned the results into a JSON array format.
At the moment I am stuck on trying to retrieve results for[stats] inside from the array in an efficient manner.
private function getGenOnePokemon()
{
// the url of the api
$url = $this->baseUrl;
//get the contents of $url var and decode it into a json array
$json = file_get_contents($url , true);
$pokemon = json_decode($json, true, JSON_UNESCAPED_UNICODE);
// array output of pokemon
echo '<pre> ';
print_r($pokemon);
echo'</pre>';
//echo out value as speed
foreach($pokemon['results'][0] as $happy)
{
echo $happy['name'] . '<br />';
}
// echo base_stat value for speed with value of 90
echo $pokemon['stats'][0]['base_stat'];
}
However I do not seem to get anywhere much printing values/keys as I need to add something else to have full access to the values?
Would prefer not to directly access results, like I am doing with base_stat as plan on using this logic to pass into HTML View layer later.
Example of print_r dump (not full dump as really long) Full example: https://pokeapi.co/api/v2/pokemon/pikachu
Array
(
[forms] => Array
(
[0] => Array
(
[url] => https://pokeapi.co/api/v2/pokemon-form/25/
[name] => pikachu
)
)
[abilities] => Array
(
[0] => Array
(
[slot] => 3
[is_hidden] => 1
[ability] => Array
(
[url] => https://pokeapi.co/api/v2/ability/31/
[name] => lightning-rod
)
)
[1] => Array
(
[slot] => 1
[is_hidden] =>
[ability] => Array
(
[url] => https://pokeapi.co/api/v2/ability/9/
[name] => static
)
)
)
[stats] => Array
(
[0] => Array
(
[stat] => Array
(
[url] => https://pokeapi.co/api/v2/stat/6/
[name] => speed
)
[effort] => 2
[base_stat] => 90
)
[1] => Array
(
[stat] => Array
(
[url] => https://pokeapi.co/api/v2/stat/5/
[name] => special-defense
)
[effort] => 0
[base_stat] => 50
)
[2] => Array
(
[stat] => Array
(
[url] => https://pokeapi.co/api/v2/stat/4/
[name] => special-attack
)
[effort] => 0
[base_stat] => 50
)
[3] => Array
(
[stat] => Array
(
[url] => https://pokeapi.co/api/v2/stat/3/
[name] => defense
)
[effort] => 0
[base_stat] => 40
)
[4] => Array
(
[stat] => Array
(
[url] => https://pokeapi.co/api/v2/stat/2/
[name] => attack
)
[effort] => 0
[base_stat] => 55
)
[5] => Array
(
[stat] => Array
(
[url] => https://pokeapi.co/api/v2/stat/1/
[name] => hp
)
[effort] => 0
[base_stat] => 35
)
)
Any advice on how to access the data using foreach or other tips greatly appreciated. Thank you!
PHP has a specific function designed to target columnar data from arrays. It is called array_column()
If you want to isolate all of the name elements inside the forms subarray, use this:
$names=array_column($pokemon['forms'],'name');
If you want to isolate all of the base_stat elements inside of the stats subarray, use this:
$base_stats=array_column($pokemon['stats'],'base_stat');
Now you will have $names and $base_stats which are single-dimensional arrays by which you can perform additional processes or return from the function. Clean, intuitive, and simple.
Your $pokemon array doesn't contain a results field. There's only an forms field. So you should iterate over forms to print the names of the forms.
foreach($pokemon['forms'] as $happy) {
echo $happy['name'] . '<br />';
}
You could do the same thing with the stats
foreach($pokemon['stats'] as $stat) {
$base_stat = $stat['base_stat'];
// ...
}
When I have an array like this :
Array (
[0] => Array ( [0] => 1 [1] => 12 [2] => Essener [3] => 1 )
[1] => Array ( [0] => 2 [1] => 12 [2] => Dinkel Spezial [3] => 0.2 )
[2] => Array ( [0] => 1 [1] => 1 [2] => Essener [3] => 1 )
)
and I use json_encode and echo it, I get this:
[["1","12","Essener","1"],["2","12","Dinkel Spezial","0.2"],["1","1","Essener","1"]]
which is good for me.
Now I have an array with stdClass Objects, which I wasn't able to transform into JSON with json_encode. When I echo it, it just doesn't show anything.
Then I transformed this array with objects to an array like this (with get_object_vars()):
Array (
[0] => Array (
[item_id] => 1
[item_name] => Essener
)
[1] => Array (
[item_id] => 2
[item_name] => Dinkel Spezial
)
[2] => Array (
[item_id] => 3
[item_name] => Saatenbrot
)
)
and when I use json_encode and echo it still doesn't show anything. Can anyone tell me what I am doing wrong or what I need to do to get a JSON array?
I need this json array to send data to an IOS App.
From the documentation:
Note:
In the event of a failure to encode, json_last_error() can be used to determine the exact nature of the error.
So you can try to detect the exact error by yourself. From your information I can't see any error.
Furthermore I don't think, it doesn't return anything. Try to var_dump() the result of json_encode(). I assume it returns false, which means an error occured.
So if anybody wondered what was wrong,
the problem was that i had "ä,ü,ö,ß" in my array and i needed to convert these to unicode and then everything worked just fine.
I have data like this:
Array
(
[data] => Array
(
[0] => Array
(
[name] => xxxxxxxxxxxx
[category] => yyyyyyyy
[id] => 12345666666
[data] => ABCDE
)
[1] => Array
(
[name] => ZZZZZZZZZZZ
[category] => JJJJJJ
[id] => 88888888888888
[data] => ABCDEHIJK
)
)
)
I need to get the data from the first array only. I don't want to use a loop. I have tried this:
$name = $data['data'][0]['name'];
but that doesn't get the name. Does anyone know how I can get the specific name only from the first array?
EDIT** my code:
$movie_details_tmdb = $tmdb->searchMovie($search);
$movie_details_tmdb_results = json_decode(json_encode($movie_details_tmdb), true);
$id = $movie_details_tmdb_results['results'][0]['id'];
it's in the same format as the code above.
maybe you have problems with json?
(PHP 5 >= 5.2.0, PECL json >= 1.2.0)
The better way to convert object to array:
$arr = (array) $obj;