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
Related
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
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
how i can get WP_Widget_Archives from array,
This is my array:
$control = Array
(
[name] => Archives
[id] => archives-6
[callback] => Array
(
[0] => WP_Widget_Archives Object
(
[id_base] => archives
[name] => Archives
[widget_options] => Array
(
[classname] => widget_archive
[description] => A monthly archive of your site’s Posts.
)
[control_options] => Array
(
[id_base] => archives
)
[number] => 8
[id] => archives-8
[updated] =>
[option_name] => widget_archives
)
[1] => form_callback
)
[params] => Array
(
[0] => Array
(
[number] => 6
)
)
[width] => 250
[height] => 200
[id_base] => archives
)
i have try with this code
`echo '<pre>'; print_r(array_keys($control['callback'])); echo '</pre>';`
but I get result like this
Array
(
[0] => 0
[1] => 1
)
where I think the result will be like this
$result = Array
(
[0] => WP_Widget_Archives Object
[1] => form_callback
)
so i can write $result[0] for get WP_Widget_Archives, please help me and thank you for your help :)
Probably you misunderstood array_key function. It will give you keys of the array not value. In your case you require value which is an object 'WP_Widget_Archives', so you can directly use $control['callback'][0].
I have the following code:
<?php
//The company_array:
$company_array = array(
"AAA" => "AAA",
"BBB" => "BBB",
"CCC" => "CCC",
"DDD" => "DDD"
);
$platform_data = 'PC'; //Just to keep it short :)
foreach ($company_array as $company) {
if ($stmt = $mysqli->prepare("SELECT price, time FROM $company WHERE platform = ? ORDER BY time ASC")) {
$stmt->bind_param("s", $platform_data);
$stmt->execute();
$stmt->bind_result($price[$company], $time[$company]);
$i=0;
while ($stmt->fetch()) {
$company_info[$company][$i] = array('Price' => $price[$company], 'Time' => $time[$company]);
$i++;
}
$stmt->close();
}
?>
Now I had some issues with this, the last iteration of the loop seems to break, if I print $company_info all but the last company displays fine, the last one seem to repeat the last value for all the rows:
Array
(
[AAA] => Array
(
[0] => Array
(
[Price] => 626.8600
[Time] => 2013-09-27 14:30:06
)
[1] => Array
(
[Price] => 615.5900
[Time] => 2013-09-27 15:45:05
)
[2] => Array
(
[Price] => 604.7400
[Time] => 2013-09-27 17:45:05
)
)
[BBB] => Array
(
[0] => Array
(
[Price] => 246.7200
[Time] => 2013-09-27 14:30:06
)
[1] => Array
(
[Price] => 245.4700
[Time] => 2013-09-27 15:45:05
)
[2] => Array
(
[Price] => 244.8300
[Time] => 2013-09-27 17:45:05
)
)
[CCC] => Array
(
[0] => Array
(
[Price] => 189.0900
[Time] => 2013-09-27 14:30:06
)
[1] => Array
(
[Price] => 188.9800
[Time] => 2013-09-27 15:45:05
)
[2] => Array
(
[Price] => 188.8900
[Time] => 2013-09-27 17:45:05
)
)
[DDD] => Array
(
[0] => Array
(
[Price] => 134.3100
[Time] => 2013-10-06 13:30:06
)
[1] => Array
(
[Price] => 134.3100
[Time] => 2013-10-06 13:30:06
)
[2] => Array
(
[Price] => 134.3100
[Time] => 2013-10-06 13:30:06
)
)
)
As you can see company AAA, BBB and CCC all have different Prices and Times in each of their arrays, but company DDD has the same value repeated 3 times (the last value in the database) when the values should be different in the same way as the other companies.
Now from what I have read I am doing this wrong, instead of using foreach I should use implode() on the array and use that, but its confusing me since I can not find a good example where it is used on the FROM field (seems to only be used on WHERE)
My question would be, how can I get away from using a foreach loop since it seems to be causing issues, and use the proper implode() method on the FROM field?
I am guessing I need to modify my while loop also so that the output remains in the same format.
I wonder if it's possible to add a key and value to an array, based on certain conditions.
This piece of script makes an api-call to retrieve sportresults from multiple teams based on a teamID number.
$length = $numberofTeams
for ($i = 0; $i < $length; $i++) {
$teamID = $objTeamID[$i]['Teamid'];
$teamResults = 'http://api.com/teamresults/' . $Teamid;
$dataResults = file_get_contents($teamResults);
$objResults[] = json_decode($dataResults, true);
}
The result is an array with this structure:
Array (
[0] => Array (
[errorcode] => 9995
[message] => No results
)
[1] => Array (
[errorcode] => 1000
[message] => Ok, Schedule follows
[List] => Array (
[0] => Array (
[MatchID] => 7683403
[Number] => 630
[Result] => 2 - 1
[Datum] => 2013-08-27
[Tijd] => 2000
[CompType] => B )
[1] => Array (
[MatchID] => 7683403
[Number] => 630
[Result] => 4 - 0 [Datum] => 2013-08-27
[Tijd] => 2000
[CompType] => B )
)
)
[2] => Array (
[errorcode] => 9995
[message] => No results )
)
Before saving it in an MySql database, for later use I need to add the teamID-variable to every result so it would become:
Array (
[0] => Array (
[errorcode] => 9995
[message] => No results
)
[1] => Array (
[errorcode] => 1000
[message] => Ok, Schedule follows
[List] => Array (
[0] => Array (
[teamID] => 'value from $teamID'
[MatchID] => 7683403
[Number] => 630
[Result] => 2 - 1
[Datum] => 2013-08-27
[Tijd] => 2000
[CompType] => B )
[1] => Array (
[teamID] => 'value from $teamID' [MatchID] => 7683403
[Number] => 630
[Result] => 4 - 0
[Datum] => 2013-08-27
[Tijd] => 2000
[CompType] => B )
)
)
[2] => Array (
[errorcode] => 9995
[message] => No results )
)
The length of the array varies and also the number of results vary. I have no influence on the result of the api-call itself, because it's been set up by big sport association.
I'm absolutely no programmer, so I'm out of my depth her, but this is a voluntary job for an amateur sportsclub so hiring a programmer is no option.
Rgds, Bonzyx
if(isset($objResults[1]['List'])){
foreach($objResults[1]['List'] as &$listItem){
$listItem['teamID'] = $teamID;
}
unset($listItem); //good practice to unset this reference to last array element
}
You could do the same with php's array_walk() function, but since you said you're not a programmer I think the foreach method is more clear to you.