How to get specific data from array - php

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;

Related

Getting Value from Nested Array -- PHP Wordpress --

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

Loop through JSON arrays using PHP [duplicate]

This question already has an answer here:
How to extract and access data from JSON with PHP?
(1 answer)
Closed 4 years ago.
I'm trying to loop through an array of moves for a Pokemon website. I'm using the API PokeApi (https://pokeapi.co/).
My question is how can I access the moves in these arrays using plain PHP.
I've tried using this just to call 1 move. But I don't know how to access data that's in arrays. Like "move->version-group-details".
$base = "https://pokeapi.co/api/v2/pokemon/";
$id = 1;
$data = file_get_contents($base.$id."/");
$pokemon = json_decode($data);
echo $pokemon->moves[0];
Thanks in advance :)
So you have two methods here you can do so when I run
$base = "https://pokeapi.co/api/v2/pokemon/";
$id = 1;
$data = file_get_contents($base.$id."/");
$pokemon = json_decode($data);
print_r($pokemon->moves[0]);
I get the result:
stdClass Object
(
[move] => stdClass Object
(
[name] => razor-wind
[url] => https://pokeapi.co/api/v2/move/13/
)
[version_group_details] => Array
(
[0] => stdClass Object
(
[level_learned_at] => 0
[move_learn_method] => stdClass Object
(
[name] => egg
[url] => https://pokeapi.co/api/v2/move-learn-method/2/
)
[version_group] => stdClass Object
(
[name] => crystal
[url] => https://pokeapi.co/api/v2/version-group/4/
)
)
[1] => stdClass Object
(
[level_learned_at] => 0
[move_learn_method] => stdClass Object
(
[name] => egg
[url] => https://pokeapi.co/api/v2/move-learn-method/2/
)
[version_group] => stdClass Object
(
[name] => gold-silver
[url] => https://pokeapi.co/api/v2/version-group/3/
)
)
)
)
If you want to access a moves name you will have to run $pokemon->moves[0]->move->name since we are getting an object returned. If you want to get the name inside the move_learn_method of version_group_details you will have to run
$pokemon->moves[0]->version_group_details[0]-> move_learn_method->name
Alternatively, if you want to return all arrays instead of objects just run this
$base = "https://pokeapi.co/api/v2/pokemon/";
$id = 1;
$data = file_get_contents($base.$id."/");
$pokemon = json_decode($data, true);
print_r($pokemon['moves'][0]);
This will now return
Array
(
[move] => Array
(
[name] => razor-wind
[url] => https://pokeapi.co/api/v2/move/13/
)
[version_group_details] => Array
(
[0] => Array
(
[level_learned_at] => 0
[move_learn_method] => Array
(
[name] => egg
[url] => https://pokeapi.co/api/v2/move-learn-method/2/
)
[version_group] => Array
(
[name] => crystal
[url] => https://pokeapi.co/api/v2/version-group/4/
)
)
[1] => Array
(
[level_learned_at] => 0
[move_learn_method] => Array
(
[name] => egg
[url] => https://pokeapi.co/api/v2/move-learn-method/2/
)
[version_group] => Array
(
[name] => gold-silver
[url] => https://pokeapi.co/api/v2/version-group/3/
)
)
)
)
So instead of having to use the object accessor -> you can access the data by using array notation so instead of
$pokemon->moves[0]->version_group_details[0]-> move_learn_method->name
you can now use:
$pokemon['moves']['version_group_details'][0]['move_learn_method']['name']
Hope that helped.

Php unserialized a serialized json string

After a http post in c# to php am getting an output of the form
in the code i have
public function actionSubmitInspection(){
$data = $_POST;
return (array)$data["check_comments"];
}
now am getting a result of the form
[
"[{\"id\":26,\"comment\":\"89oiu\"},{\"id\":27,\"comment\":\"comment 2\"}]"
]
as from my try typecasting array doesnt creating the array, How can i convert the serialized string to an array or an object.
use json_decode function.
public function actionSubmitInspection(){
$data = $_POST;
// replace it
//return (array)$data["check_comments"];
return json_decode($data["check_comments"]);
}
Out put will be array of objects.
Array
(
[0] => stdClass Object
(
[id] => 26
[comment] => 89oiu
)
[1] => stdClass Object
(
[id] => 27
[comment] => comment 2
)
)
as from my try typecasting array doesnt creating the array
Yes, it creates an array but the array it creates contains the JSON text.
You need to parse the JSON in order to restore the data structures it encodes. PHP provides the function json_decode() for this purpose. I recommend you pass TRUE as the second argument to json_decode() to get back arrays (otherwise it creates stdClass objects that are just arrays with a fancy syntax and limited options for processing).
// Assuming the value of $data['check_comments'] is:
// "[{\"id\":26,\"comment\":\"89oiu\"},{\"id\":27,\"comment\":\"comment 2\"}]"
$output = json_decode($data['check_comments']);
print_r($output);
The output:
Array
(
[0] => Array
(
[id] => 26
[comment] => 89oiu
)
[1] => Array
(
[id] => 27
[comment] => comment 2
)
)
You should use json_decode($data["check_comments"]) the output will be an array of stdClass objects:
Array
(
[0] => stdClass Object
(
[id] => 26
[comment] => 89oiu
)
[1] => stdClass Object
(
[id] => 27
[comment] => comment 2
)
)
or passing true on second param, json_decode($data["check_comments"], true) and the output will be an array of arrays:
Array
(
[0] => Array
(
[id] => 26
[comment] => 89oiu
)
[1] => Array
(
[id] => 27
[comment] => comment 2
)
)

php json encode not encoding array

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 :)

How can I extract a PHP string from this sub-array?

The array I am looking at is this:
Array
(
[0] => Array
(
[0] => Bigcommerce\Api\Resources\ProductCustomField Object
(
[ignoreOnCreate:protected] => Array
(
[0] => id
[1] => product_id
)
[ignoreOnUpdate:protected] => Array
(
[0] => id
[1] => product_id
)
[fields:protected] => stdClass Object
(
[id] => 17
[product_id] => 3232
[name] => Artist
[text] => Test
)
[id:protected] => 17
[ignoreIfZero:protected] => Array
(
)
)
)
)
I want to check to see if 'Artist' exists in a php conditional statement. But I don't know how to turn 'Artist' into a string.
UPDATED:
I did not find understand how to extract that value into a string, but I got what I was looking for using the method related to the bigcommerce api:
$customs = Bigcommerce::getProductCustomFields($product->id);
foreach($customs as $custom) {
if($custom->name == 'Artist'): // do something
endif;
}
Ok, looking at the source, it seems you should be able to use the magic __get method. Try
$array[0][0]->name == 'Artist'
The value of the custom field would be stored in the "text" resource for that custom field.
See the following link where you can see the 4 properties of a custom field.
https://developer.bigcommerce.com/api/stores/v2/products/custom_fields

Categories