im trying to filter a JSON array response as i only need a small part of the results.
I need to get the players displayName only.
Here is the repose for the first player, there can be upto 12 player per match.
I need something that can loop through and extract the names..
[displayName] => jonhofun
At present the only way i can get the data i need is by doing
$player1 = $json11['Response']['data']['entries']['0']['player']['destinyUserInfo']['displayName'];
$player2 = $json11['Response']['data']['entries']['1']['player']['destinyUserInfo']['displayName'];
etc... etc...
heres the original response
Array
(
[Response] => Array
(
[data] => Array
(
[period] => 2016-08-20T10:16:46Z
[activityDetails] => Array
(
[referenceId] => 3156370656
[instanceId] => 5370359303
[mode] => 12
[activityTypeHashOverride] => 3614615911
)
[entries] => Array
(
[0] => Array
(
[standing] => 0
[score] => Array
(
[basic] => Array
(
[value] => 2190
[displayValue] => 2,190
)
)
[player] => Array
(
[destinyUserInfo] => Array
(
[iconPath] => /common/destiny_content/icons/d0d3cd4c26aa1a931d46c4bf720856ba.jpg
[membershipType] => 2
[membershipId] => 4611686018454971653
[displayName] => jonhofun
)
[characterClass] => Warlock
[characterLevel] => 40
[lightLevel] => 322
)
)
)
)
)
)
Any help would be appreciated.
You need to loop through the sub array under "entries".
foreach ($json11['Response']['data']['entries'] as $entries) {
$player_names[] = $entries['player']['destinyUserInfo']['displayname'];
}
echo "<pre>";
print_r($player_names); // Check all player names
Related
I am new to PHP and Arrays, I am trying to get the values from an array. But no matter how I'm trying to do it, I can't get the value. What am I doing wrong?
The Array:
Array ( [playerinfo] => Array ( [rank] => Godfather [cash] => € 8,520,530 [weapon] => M-16 (29000) [health] => Array ( [width] => 100 [color] => green ) [wealth] => Too rich to be true [protection] => Bulletproof Humvee [plf] => Huge [plane] => Concorde [crew] => None [pbf] => Large [ship] => None ) [character] => Array ( [crime] => Array ( [0] => 120 [1] => 69 ) [gta] => Array ( [0] => 400 [1] => 70 ) [drugs] => Array ( [0] => 120 [1] => 2528 ) [airport] => Array ( [0] => 2700 [1] => 2529 ) [oc] => Array ( [0] => 86400 [1] => 1442364 ) [tr] => Array ( [0] => 10800 [1] => 1640016011 ) [plf] => Array ( [0] => 7200 [1] => 6712 ) [kill] => Array ( [0] => 3600 [1] => 1640019611 ) ) )
The way I tried to get the info:
$AccData = json_decode($MobinfoString, true);
echo $AccData['playerinfo']['rank'].'<br/>';
echo $AccData['playerinfo']['cash'].'<br/>';
foreach ($AccData as $playerinfo) {
echo $playerinfo['playerinfo']['rank'].'<br/>';
echo $playerinfo['character']['gta'].'<br/>';
}
EDIT:
The json string
{"playerinfo":{"rank":"Boss","cash":"€ 5,923,712","weapon":"M-16 (4500)","health":{"width":"100","color":"green"},"wealth":"Too rich to be true","protection":"Bulletproof Humvee","plf":"Huge","plane":"Concorde","crew":"None","pbf":"Large","ship":"None"},"character":{"crime":[120,122],"gta":[400,369],"drugs":[120,2582],"airport":[2700,2582],"oc":[86400,1640020450],"tr":[10800,1640016850],"plf":[7200,3935],"kill":[3600,1640020450]}}
Anyone knows how to do this ? For example I need the Concorde from plane in a variable and the time values from gta in a variable. And some more from this string.
So your first Part is okay and you rank you can just display like the first part as well
$AccData = json_decode($MobinfoString, true);
echo $AccData['playerinfo']['rank'].'<br/>';
echo $AccData['playerinfo']['cash'].'<br/>';
echo $AccData['playerinfo']['rank'].'<br/>';
echo $AccData['playerinfo']['plane'].'<br/>';
echo $AccData['character']['gta'][0].'<br/>';
but the character is on the same level as playerinfo so you need to access it from AccData. also gta is an array like health, so you have to specify which value you want to show, the first so 0 or second which is 1
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'];
// ...
}
I am trying to display the data from Bigcommerce through php when I run the Bigcommerce::getCategories() i getting a array of data like this:
[0] => Bigcommerce\Api\Resources\Category Object
(
[ignoreOnCreate:protected] => Array
(
[0] => id
[1] => parent_category_list
)
[ignoreOnUpdate:protected] => Array
(
[0] => id
[1] => parent_category_list
)
[fields:protected] => stdClass Object
(
[id] => 88
[parent_id] => 0
[name] => Dell
[description] =>
[sort_order] => 0
[page_title] =>
[meta_keywords] =>
[meta_description] =>
[layout_file] =>
[parent_category_list] => Array
(
[0] => 88
)
[image_file] =>
[is_visible] => 1
[search_keywords] =>
[url] => /dell/
)
[id:protected] => 88
[ignoreIfZero:protected] => Array
(
)
[fieldMap:protected] => Array
(
)
)
but when I try to pass this to JQuery so that I can display it using this statement: <?php echo json_encode($categories); ?> I am getting an array of empty objects, what is the right way to get the array of objects in Bigcommerce API? Thanks.
From the first line of your code:
[0] => Bigcommerce\Api\Resources\Category Object
You have an object, not an array. Try casting it to an array first:
$array = (array) $yourObject;
Can you please explain how to change one array value to second value.
Please find below example.
First array
Array
(
[20239802] => one test
[20239801] => two testttttt
)
Second array
Array (
[content] => Array (
[0] => Array (
[content_pack_id] => 10002
[content_pack_name] => 100 Days Of Love-FLA
[image_path] => Array ( [0] => pack_image_10002_width. )
[content_image_path] => Array ( [imgjpeg80] => http://content.jpg )
[content_id] => 20239802
[track] => Lede Inthati Santhosham
[duration] => 0
)
[1] => Array (
[content_pack_id] => 10003
[content_pack_name] => 1001 fdfdf
[image_path] => Array ( [0] => pack_image_10002_width. )
[content_image_path] => Array ( [imgjpeg80] => http://content.jpg )
[content_id] => 20239801
[track] => Lede Inthati Santhosham
[duration] => 0
)
)
[autoshuffle_pack] => no
)
We need to replace [track] value in second array if match first array [20239802] with second array [content_id]
Need out put:-
Array
(
[content] => Array
(
[0] => Array
(
[content_pack_id] => 10002
[content_pack_name] => 100 Days Of Love-FLA
[image_path] => Array
(
[0] => pack_image_10002_width.
)
[content_image_path] => Array
(
[imgjpeg80] => http://content.jpg
)
[content_id] => 20239802
[track] => one test
[duration] => 0
)
[1] => Array
(
[content_pack_id] => 10003
[content_pack_name] => 1001 fdfdf
[image_path] => Array
(
[0] => pack_image_10002_width.
)
[content_image_path] => Array
(
[imgjpeg80] => http://content.jpg
)
[content_id] => 20239801
[track] => two testttttt
[duration] => 0
)
)
[autoshuffle_pack] => no
)
Check [track] value change in my need out put
as per depend first array.
Array
(
[20239802] => one test
[20239801] => two testttttt
)
with second array
[content_id] => 20239801
[track] => two testttttt
Is this you want to do :
foreach($second_array as $key => $second_row) {
$content_id = $second_row['content_id'];
if(isset($first_array[$content_id]) && $first_array[$content_id] != '') {
$second_array['track'] = $first_array[$content_id];
}
}
You can do you own function.
Foreach records in your first array:
Loop though the second and search for key == content_id
If it matched, set the value of array2[$index][track] to array1[key]
If not just continue
You can also use array_search() function to find the match faster, for I'll let you take a look at the PHP documentation
All, I have got a JSON response from NEO4J:
Array
(
[columns] => Array
(
[0] => n
)
[data] => Array
(
[0] => Array
(
[0] => Array
(
[outgoing_relationships] => http://localhost:7474/db/data/node/1/relationships/out
[labels] => http://localhost:7474/db/data/node/1/labels
[data] => Array
(
[position] => Developer
[awesome] => 1
[name] => Michael
[children] => 3
)
[traverse] => http://localhost:7474/db/data/node/1/traverse/{returnType}
[all_typed_relationships] => http://localhost:7474/db/data/node/1/relationships/all/{-list|&|types}
[property] => http://localhost:7474/db/data/node/1/properties/{key}
[self] => http://localhost:7474/db/data/node/1
[properties] => http://localhost:7474/db/data/node/1/properties
[outgoing_typed_relationships] => http://localhost:7474/db/data/node/1/relationships/out/{-list|&|types}
[incoming_relationships] => http://localhost:7474/db/data/node/1/relationships/in
[extensions] => Array
(
)
[create_relationship] => http://localhost:7474/db/data/node/1/relationships
[paged_traverse] => http://localhost:7474/db/data/node/1/paged/traverse/{returnType}{?pageSize,leaseTime}
[all_relationships] => http://localhost:7474/db/data/node/1/relationships/all
[incoming_typed_relationships] => http://localhost:7474/db/data/node/1/relationships/in/{-list|&|types}
)
)
)
)
I do not know how to retrieve the value "position" under the data array, because the array is under another array.
Can you tell me how to do it with PHP?
Thx
Access them like this.
echo $yourarr['data'][0][0]['data']['position'];
Tips on how to locate :
Just locate where is the position keyword, Now look up to the array, As you can see the parent of position is data, think of how you reach from the start
(data)to the destination(position) (like a maze).
When you have more than 1 record try this
foreach($var['data'] as $inside){ //inner 1st stage
foreach($inside as $index => $main){ //inner 2nd stage
if($index == 'data'){ //check if index is data
echo $main['position']; //output position
}
}
}