retrieving value from array - php

print_r($array) results
I retrieved some values from database and got the following result while using print_r:
Array
(
[122] => Array
(
[p_name] => Tony Hutson
[p_image] => profile_image/profile_61323166474.jpg
[pid] => 42
[sid] => 122
[stitle] => sfcxdggf
[audio] => audio_file/audio_61324302202.mp3
[s_description] => ?mnlmkl bvnbmnmmn, bnbmn
[s_image] => sermon_image/image_41324302202.jpg
)
[count] => Array
(
[count] => 2
)
)
How can I retrieve the value of count?

echo $array['count']['count'];

Like this,
echo $array['count']['count'];

Related

PHP Get values from nested array

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

PHP: Displaying data from Bigcommerce API

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;

Get result from multidimensional array without duplicate results in php

I have an array, and would like to get all the values like this [*][place],
without duplicate results.
The output should be looking like this :
Sønderjylland
Nordjylland
Sjælland
Array
(
[0] => Array
(
[place] => Sønderjylland
[active] => Lagerarbejde
[num] => 123
)
[1] => Array
(
[place] => Nordjylland
[active] => Tømrer
[num] => 124
)
[2] => Array
(
[place] => Sønderjylland
[active] => Klejnsmed
[num] => 125
)
[3] => Array
(
[place] => Sjælland
[active] => Elektriker
[num] => 126
)
)
You can use array_column, array_unique, array_filter, implode together.
echo implode(' ', array_filter(array_unique(array_column($yourArray, 'place'))));
array_column is supported for (PHP 5 >= 5.5.0, PHP 7)
If you are using older versions then a loop would help.

How would I get the value inside the array?

I would like to get the value of [source] inside the cover array. What should I do? Thanks.
Array
(
[cover] => Array
(
[id] => 763132827070934
[offset_y] => 29
[source] => https://scontent-a.xx.fbcdn.net/hphotos-xap1/t31.0-8/q71/s720x720/10507065_763132827070934_644083226197727503_o.jpg
)
[id] => 123456789000
)
Like this:
$array['cover']['source'];
<?php
$data = Array
(
[cover] => Array
(
[id] => 763132827070934
[offset_y] => 29
[source] => https://scontent-a.xx.fbcdn.net/hphotos-xap1/t31.0-8/q71/s720x720/10507065_763132827070934_644083226197727503_o.jpg
)
[id] => 123456789000
)
echo $data['cover']['source'];
?>

PHP output an Array

i`ve an array with more arrays inside.
But how can i get a specific value from it ?
For example how can i output the first name "John" ?
I tried this but it doesn't work.
foreach($arr as $result) {
echo $result['CLIENTS']['FIRSTNAME'];
}
This is the < pre> output of the array
Array
(
[WHMCSAPI] => Array
(
[ACTION] => getclients
[RESULT] => success
[TOTALRESULTS] => 12
[STARTNUMBER] => 0
[NUMRETURNED] => 12
[CLIENTS] => Array
(
[CLIENT] => Array
(
[ID] => 14
[FIRSTNAME] => John
[LASTNAME] => Doe
[COMPANYNAME] => Muster Company
[EMAIL] => info#mustermann.de
[DATECREATED] => 2014-04-13
[GROUPID] => 0
[STATUS] => Active
)
)
)
)
The other answers & comments are almost right, but they didn't notice that you're iterating over the array.
Here's your code:
foreach($arr as $result) {
echo $result['CLIENTS']['FIRSTNAME'];
}
Inside the loop (this is important), $result is equal to:
Array
(
[ACTION] => getclients
[RESULT] => success
[TOTALRESULTS] => 12
[STARTNUMBER] => 0
[NUMRETURNED] => 12
[CLIENTS] => Array
(
[CLIENT] => Array
(
...
So to access the client name inside the loop, use this:
$firstname = $result["CLIENTS"]["CLIENT"]["FIRSTNAME"];
You're missing a couple levels of the array. Try $result['WHMCSAPI']['CLIENTS']['CLIENT']['FIRSTNAME'].

Categories