How to echo a value from Facebook Graph API - php

I'm attempting to use the Facebook Graph API to import details from employees Facebook profiles into our personal website.
Fetching the JSON object from the following URI:
https://graph.facebook.com/RDTATTOOANDPIERCING
I thought that I would be able to simply convert to an array and echo the desired value. However I'm getting errors.
Here's the code:
<?php
$facebook = json_decode(file_get_contents("https://graph.facebook.com/RDTATTOOANDPIERCING"));
echo $facebook["about"];
?>
Here's the URL if you want to see the source being rendered:
http://www.rdtattoopiercing.com/
Thanks ahead of time!

json_decode without the second parameter will return an object, if you want an array to be returned add a second parameter TRUE
try this:
$facebook = json_decode(file_get_contents("https://graph.facebook.com/RDTATTOOANDPIERCING"), TRUE);
echo $facebook["about"];
or use object instead if you don't want to add TRUE to the second parameter
$facebook->about;

The problem is that it returned a stdClass not an array, You could tell that by using var_dump($facebook), To convert an stdClass to an array use get_object_vars().
Here is your solution:
<?php
$facebook = get_object_vars(json_decode(file_get_contents("http://graph.facebook.com/RDTATTOOANDPIERCING")));
echo $facebook['about'];
?>
Here is the var_dump before the conversion to an array:
object(stdClass)#1 (20) { ["about"]=> string(60) "Red Dragon Tattoo & Piercing is devoted to inking Cincinnati" ["checkins"]=> int(144) ["description"]=> string(509) "Red Dragon has been open since Feb. 2009, After Gabriel started constructing J began to Fill the chairs with his large following, after only a few months we brought on Chuck and Jocelyn to help with the overwhelming amount of people ready to get tattooed in the Colerain area and we've been inking up Cincinnati since then. We appreciate artfull expressions, and can help you make your own statement to this world through your body. If it's a body piercing, or a tattoo, what better way to express yourself." ["general_info"]=> string(59) "Artists: J, Chuck Hagedorn, Jocelyn Taylor, Gabriel Lowe " ["hours"]=> object(stdClass)#2 (12) { ["mon_1_open"]=> string(5) "13:00" ["mon_1_close"]=> string(5) "20:00" ["tue_1_open"]=> string(5) "13:00" ["tue_1_close"]=> string(5) "20:00" ["wed_1_open"]=> string(5) "13:00" ["wed_1_close"]=> string(5) "20:00" ["thu_1_open"]=> string(5) "13:00" ["thu_1_close"]=> string(5) "20:00" ["fri_1_open"]=> string(5) "13:00" ["fri_1_close"]=> string(5) "20:00" ["sat_1_open"]=> string(5) "13:00" ["sat_1_close"]=> string(5) "20:00" } ["is_published"]=> bool(true) ["location"]=> object(stdClass)#3 (7) { ["street"]=> string(17) "9242 Colerain Ave" ["city"]=> string(10) "Cincinnati" ["state"]=> string(2) "OH" ["country"]=> string(13) "United States" ["zip"]=> string(10) "45251-2406" ["latitude"]=> float(39.239511218398) ["longitude"]=> float(-84.592958873707) } ["parking"]=> object(stdClass)#4 (3) { ["street"]=> int(0) ["lot"]=> int(1) ["valet"]=> int(0) } ["phone"]=> string(17) "+1 (513) 385-6500" ["price_range"]=> string(10) "$$$$ (50+)" ["talking_about_count"]=> int(97) ["username"]=> string(19) "RDTATTOOANDPIERCING" ["website"]=> string(20) "rdtattoopiercing.com" ["were_here_count"]=> int(535) ["category"]=> string(14) "Local business" ["id"]=> string(15) "151284611579488" ["name"]=> string(28) "Red Dragon Tattoo & Piercing" ["link"]=> string(43) "http://www.facebook.com/RDTATTOOANDPIERCING" ["likes"]=> int(835) ["cover"]=> object(stdClass)#5 (3) { ["cover_id"]=> float(4.636681470078E+14) ["source"]=> string(94) "http://sphotos-c.ak.fbcdn.net/hphotos-ak-ash3/s720x720/547023_463668147007798_1259271309_n.jpg" ["offset_y"]=> int(0) } }
As you can see it begins with object(stdClass) instead it should be an array, so adding the code get_object_vars() will return an array format not an stdClass, as you can see from this var_dump:
array(20) { ["about"]=> string(60) "Red Dragon Tattoo & Piercing is devoted to inking Cincinnati" ["checkins"]=> int(144) ["description"]=> string(509) "Red Dragon has been open since Feb. 2009, After Gabriel started constructing J began to Fill the chairs with his large following, after only a few months we brought on Chuck and Jocelyn to help with the overwhelming amount of people ready to get tattooed in the Colerain area and we've been inking up Cincinnati since then. We appreciate artfull expressions, and can help you make your own statement to this world through your body. If it's a body piercing, or a tattoo, what better way to express yourself." ["general_info"]=> string(59) "Artists: J, Chuck Hagedorn, Jocelyn Taylor, Gabriel Lowe " ["hours"]=> object(stdClass)#2 (12) { ["mon_1_open"]=> string(5) "13:00" ["mon_1_close"]=> string(5) "20:00" ["tue_1_open"]=> string(5) "13:00" ["tue_1_close"]=> string(5) "20:00" ["wed_1_open"]=> string(5) "13:00" ["wed_1_close"]=> string(5) "20:00" ["thu_1_open"]=> string(5) "13:00" ["thu_1_close"]=> string(5) "20:00" ["fri_1_open"]=> string(5) "13:00" ["fri_1_close"]=> string(5) "20:00" ["sat_1_open"]=> string(5) "13:00" ["sat_1_close"]=> string(5) "20:00" } ["is_published"]=> bool(true) ["location"]=> object(stdClass)#3 (7) { ["street"]=> string(17) "9242 Colerain Ave" ["city"]=> string(10) "Cincinnati" ["state"]=> string(2) "OH" ["country"]=> string(13) "United States" ["zip"]=> string(10) "45251-2406" ["latitude"]=> float(39.239511218398) ["longitude"]=> float(-84.592958873707) } ["parking"]=> object(stdClass)#4 (3) { ["street"]=> int(0) ["lot"]=> int(1) ["valet"]=> int(0) } ["phone"]=> string(17) "+1 (513) 385-6500" ["price_range"]=> string(10) "$$$$ (50+)" ["talking_about_count"]=> int(97) ["username"]=> string(19) "RDTATTOOANDPIERCING" ["website"]=> string(20) "rdtattoopiercing.com" ["were_here_count"]=> int(535) ["category"]=> string(14) "Local business" ["id"]=> string(15) "151284611579488" ["name"]=> string(28) "Red Dragon Tattoo & Piercing" ["link"]=> string(43) "http://www.facebook.com/RDTATTOOANDPIERCING" ["likes"]=> int(835) ["cover"]=> object(stdClass)#5 (3) { ["cover_id"]=> float(4.636681470078E+14) ["source"]=> string(94) "http://sphotos-c.ak.fbcdn.net/hphotos-ak-ash3/s720x720/547023_463668147007798_1259271309_n.jpg" ["offset_y"]=> int(0) } }

Related

Echo array with json_decode gives too many echoes

I do get the strings I intend to. But I also get the first letter (or number) of the other data in the json file.
E.g. I would like to have or should get the result:
/Barrafina
Exceptional cooking here. Such simple but amazing dishes. Highly recommend the crab croquettes, lambs kidneys, skewers and the brain. Sherries are also superb here.
Matt Ta-Min/
But instead I get
/Barrafina
ee
Exceptional cooking here. Such simple but amazing dishes. Highly recommend the crab croquettes, lambs kidneys, skewers and the brain. Sherries are also superb here.
5 5 E Eu uh h
Matt Ta-Min
/
What did I do wrong? here is the code:
(If you have time and energy to look the JSON you will see from where eg. 5 5 E Eu uh h comes.)
I appreciate any help.
<?php
$client_id = 'CLIENT_ID';
$client_secret = 'SECRET';
$request_date = date('Ymd');
$base_url = 'https://api.foursquare.com/v2/';
$endpoint = 'venues/explore?';
$params = 'near=London&limit=3&section=food&radius=1000';
$auth = "&client_id=$client_id&client_secret=$client_secret&v=$request_date";
$url = $base_url.$endpoint.$params.$auth;
$results = file_get_contents($url);
$json_results = json_decode($results,true);
//var_dump($json_results);
$items0 = $json_results['response']['groups']['0']['items']['0'];
$items1 = $json_results['response']['groups']['0']['items']['0']['tips'];
$items2 = $json_results['response']['groups']['0']['items']['0']['tips']['0'];
//<div style="align:center;">
foreach ( $items0+$items1 as $item){
echo '<li>';
echo $item['name'];
echo $item['text'];
}
foreach ( $items2 as $item){
print $item['firstName']; print '&nbsp'; print $item['lastName'];
}
HERE is the json decoded:
array(2) { ["meta"]=> array(2) { ["code"]=> int(200) ["requestId"]=> string(24) "55fb1f91498e70ad2e246eed" } ["response"]=> array(10) { ["suggestedFilters"]=> array(2) { ["header"]=> string(12) "Tap to show:" ["filters"]=> array(2) { [0]=> array(2) { ["name"]=> string(13) "With specials" ["key"]=> string(8) "specials" } [1]=> array(2) { ["name"]=> string(8) "Open now" ["key"]=> string(7) "openNow" } } } ["geocode"]=> array(8) { ["what"]=> string(0) "" ["where"]=> string(6) "london" ["center"]=> array(2) { ["lat"]=> float(51.50853) ["lng"]=> float(-0.12574) } ["displayString"]=> string(38) "London, Greater London, United Kingdom" ["cc"]=> string(2) "GB" ["geometry"]=> array(1) { ["bounds"]=> array(2) { ["ne"]=> array(2) { ["lat"]=> float(51.691643999656) ["lng"]=> float(0.33418999705203) } ["sw"]=> array(2) { ["lat"]=> float(51.284674044171) ["lng"]=> float(-0.50855792793694) } } } ["slug"]=> string(6) "london" ["longId"]=> string(17) "72057594040571679" } ["warning"]=> array(1) { ["text"]=> string(114) "There aren't a lot of results near you. Try something more general, reset your filters, or expand the search area." } ["headerLocation"]=> string(6) "London" ["headerFullLocation"]=> string(6) "London" ["headerLocationGranularity"]=> string(4) "city" ["query"]=> string(4) "food" ["totalResults"]=> int(246) ["suggestedBounds"]=> array(2) { ["ne"]=> array(2) { ["lat"]=> float(51.510800358799) ["lng"]=> float(-0.12174369837641) } ["sw"]=> array(2) { ["lat"]=> float(51.508100699014) ["lng"]=> float(-0.13015278468604) } } ["groups"]=> array(1) { [0]=> array(3) { ["type"]=> string(18) "Recommended Places" ["name"]=> string(11) "recommended" ["items"]=> array(1) { [0]=> array(4) { ["reasons"]=> array(2) { ["count"]=> int(0) ["items"]=> array(1) { [0]=> array(3) { ["summary"]=> string(20) "This spot is popular" ["type"]=> string(7) "general" ["reasonName"]=> string(23) "globalInteractionReason" } } } ["venue"]=> array(17) { ["id"]=> string(24) "53bab96d498e7e355fb53d6c" ["name"]=> string(9) "Barrafina" ["contact"]=> array(6) { ["phone"]=> string(13) "+442074401456" ["formattedPhone"]=> string(16) "+44 20 7440 1456" ["twitter"]=> string(13) "barrafinaadst" ["facebook"]=> string(15) "705351912878392" ["facebookUsername"]=> string(23) "BarrafinaAdelaideStreet" ["facebookName"]=> string(25) "Barrafina Adelaide Street" } ["location"]=> array(10) { ["address"]=> string(14) "10 Adelaide St" ["crossStreet"]=> string(13) "William IV St" ["lat"]=> float(51.509450528906) ["lng"]=> float(-0.12594824153122) ["postalCode"]=> string(8) "WC2N 4HZ" ["cc"]=> string(2) "GB" ["city"]=> string(6) "London" ["state"]=> string(14) "Greater London" ["country"]=> string(14) "United Kingdom" ["formattedAddress"]=> array(5) { [0]=> string(30) "10 Adelaide St (William IV St)" [1]=> string(6) "London" [2]=> string(14) "Greater London" [3]=> string(8) "WC2N 4HZ" [4]=> string(14) "United Kingdom" } } ["categories"]=> array(1) { [0]=> array(6) { ["id"]=> string(24) "4bf58dd8d48988d150941735" ["name"]=> string(18) "Spanish Restaurant" ["pluralName"]=> string(19) "Spanish Restaurants" ["shortName"]=> string(7) "Spanish" ["icon"]=> array(2) { ["prefix"]=> string(52) "https://ss3.4sqi.net/img/categories_v2/food/spanish_" ["suffix"]=> string(4) ".png" } ["primary"]=> bool(true) } } ["verified"]=> bool(false) ["stats"]=> array(3) { ["checkinsCount"]=> int(357) ["usersCount"]=> int(278) ["tipCount"]=> int(26) } ["url"]=> string(22) "http://barrafina.co.uk" ["price"]=> array(3) { ["tier"]=> int(2) ["message"]=> string(8) "Moderate" ["currency"]=> string(2) "£" } ["rating"]=> float(9) ["ratingColor"]=> string(6) "00B551" ["ratingSignals"]=> int(80) ["allowMenuUrlEdit"]=> bool(true) ["hours"]=> array(2) { ["status"]=> string(19) "Open until 11:00 PM" ["isOpen"]=> bool(true) } ["specials"]=> array(2) { ["count"]=> int(0) ["items"]=> array(0) { } } ["photos"]=> array(2) { ["count"]=> int(31) ["groups"]=> array(0) { } } ["hereNow"]=> array(3) { ["count"]=> int(0) ["summary"]=> string(11) "Nobody here" ["groups"]=> array(0) { } } } ["tips"]=> array(1) { [0]=> array(9) { ["id"]=> string(24) "55db9142498ede18f5b31b81" ["createdAt"]=> int(1440452930) ["text"]=> string(164) "Exceptional cooking here. Such simple but amazing dishes. Highly recommend the crab croquettes, lambs kidneys, skewers and the brain. Sherries are also superb here." ["type"]=> string(4) "user" ["canonicalUrl"]=> string(52) "https://foursquare.com/item/55db9142498ede18f5b31b81" ["likes"]=> array(3) { ["count"]=> int(2) ["groups"]=> array(0) { } ["summary"]=> string(7) "2 likes" } ["logView"]=> bool(true) ["todo"]=> array(1) { ["count"]=> int(0) } ["user"]=> array(5) { ["id"]=> string(8) "98683884" ["firstName"]=> string(4) "Matt" ["lastName"]=> string(6) "Ta-Min" ["gender"]=> string(4) "male" ["photo"]=> array(2) { ["prefix"]=> string(31) "https://irs1.4sqi.net/img/user/" ["suffix"]=> string(30) "/98683884-1MJ0OTISTPZWSBRJ.jpg" } } } } ["referralId"]=> string(30) "e-3-53bab96d498e7e355fb53d6c-0" } } } } } }
Here is JSON
{"meta":{"code":200,"requestId":"55fbde58498e7fdbd12f8fb2"},"response":{"suggestedFilters":{"header":"Tap to show:","filters":[{"name":"With specials","key":"specials"},{"name":"Open now","key":"openNow"}]},"geocode":{"what":"","where":"london","center":{"lat":51.50853,"lng":-0.12574},"displayString":"London, Greater London, United Kingdom","cc":"GB","geometry":{"bounds":{"ne":{"lat":51.691643999655895,"lng":0.33418999705203406},"sw":{"lat":51.28467404417054,"lng":-0.5085579279369435}}},"slug":"london","longId":"72057594040571679"},"warning":{"text":"There aren't a lot of results near you. Try something more general, reset your filters, or expand the search area."},"headerLocation":"London","headerFullLocation":"London","headerLocationGranularity":"city","query":"food","totalResults":246,"suggestedBounds":{"ne":{"lat":51.5108003587992,"lng":-0.12174369837640672},"sw":{"lat":51.50810069901375,"lng":-0.1301527846860385}},"groups":[{"type":"Recommended Places","name":"recommended","items":[{"reasons":{"count":0,"items":[{"summary":"This spot is popular","type":"general","reasonName":"globalInteractionReason"}]},"venue":{"id":"53bab96d498e7e355fb53d6c","name":"Barrafina","contact":{"phone":"+442074401456","formattedPhone":"+44 20 7440 1456","twitter":"barrafinaadst","facebook":"705351912878392","facebookUsername":"BarrafinaAdelaideStreet","facebookName":"Barrafina Adelaide Street"},"location":{"address":"10 Adelaide St","crossStreet":"William IV St","lat":51.50945052890648,"lng":-0.1259482415312226,"postalCode":"WC2N 4HZ","cc":"GB","city":"London","state":"Greater London","country":"United Kingdom","formattedAddress":["10 Adelaide St (William IV St)","London","Greater London","WC2N 4HZ","United Kingdom"]},"categories":[{"id":"4bf58dd8d48988d150941735","name":"Spanish Restaurant","pluralName":"Spanish Restaurants","shortName":"Spanish","icon":{"prefix":"https:\/\/ss3.4sqi.net\/img\/categories_v2\/food\/spanish_","suffix":".png"},"primary":true}],"verified":false,"stats":{"checkinsCount":357,"usersCount":278,"tipCount":26},"url":"http:\/\/barrafina.co.uk","rating":9.0,"ratingColor":"00B551","ratingSignals":80,"allowMenuUrlEdit":true,"hours":{"status":"Closed until Noon","isOpen":false},"specials":{"count":0,"items":[]},"photos":{"count":31,"groups":[]},"hereNow":{"count":0,"summary":"Nobody here","groups":[]}},"tips":[{"id":"55db9142498ede18f5b31b81","createdAt":1440452930,"text":"Exceptional cooking here. Such simple but amazing dishes. Highly recommend the crab croquettes, lambs kidneys, skewers and the brain. Sherries are also superb here.","type":"user","canonicalUrl":"https:\/\/foursquare.com\/item\/55db9142498ede18f5b31b81","likes":{"count":2,"groups":[],"summary":"2 likes"},"logView":true,"todo":{"count":0},"user":{"id":"98683884","firstName":"Matt","lastName":"Ta-Min","gender":"male","photo":{"prefix":"https:\/\/irs1.4sqi.net\/img\/user\/","suffix":"\/98683884-1MJ0OTISTPZWSBRJ.jpg"}}}],"referralId":"e-3-53bab96d498e7e355fb53d6c-0"}]}]}}

Return array data as object

Im having a problem getting my desired output out of a function. I see that its returning the proper data when i do a 'var dump'. However, it either does not show anything or i'm getting invalid argument errors / 'Object of class stdClass could not be converted to string'.
Here is the function
public function searchUser($name, $limit = 0) {
return $this->_makeCall('users/search', false, array('q' => $name, 'count' => $limit));
}
and my code is calling it like this:
$usearch=$_POST['usersearch'];
$result = $instagram->searchUser($usearch);
....
<div id="searchresults">
<h4> Search result for <?php
echo $usearch;
?>
</h4>
<section id="list">
<?php
foreach ($usearch as $object) {
print $object->username;
}
?>
</div>
</div>
lastly, here is the var dump when i just call it that way:
object(stdClass)#2 (2) { ["meta"]=> object(stdClass)#3 (1) { ["code"]=> int(200) } ["data"]=> array(50) { [0]=> object(stdClass)#4 (6) { ["username"]=> string(6) "george" ["bio"]=> string(0) "" ["website"]=> string(0) "" ["profile_picture"]=> string(57) "http://images.ak.instagram.com/profiles/anonymousUser.jpg" ["full_name"]=> string(0) "" ["id"]=> string(7) "7693231" } [1]=> object(stdClass)#5 (6) { ["username"]=> string(9) "instagod7" ["bio"]=> string(28) "Graphic designer 🫠O.D.U." ["website"]=> string(0) "" ["profile_picture"]=> string(76) "http://images.ak.instagram.com/profiles/profile_26017769_75sq_1358524943.jpg" ["full_name"]=> string(6) "George" ["id"]=> string(8) "26017769" } [2]=> object(stdClass)#6 (6) { ["username"]=> string(17) "georgetowncupcake" ["bio"]=> string(81) "Official Instagram account of Georgetown Cupcake | Home of TLC's DC CUPCAKES!" ["website"]=> string(32) "http://www.georgetowncupcake.com" ["profile_picture"]=> string(77) "http://images.ak.instagram.com/profiles/profile_265095138_75sq_1391136095.jpg" ["full_name"]=> string(18) "Georgetown Cupcake" ["id"]=> string(9) "265095138" } [3]=> object(stdClass)#7 (6) { ["username"]=> string(11) "georgewbush" ["bio"]=> string(79) "43rd President of the United States and Founder of the George W. Bush Institute" ["website"]=> string(21) "http://bushcenter.org" ["profile_picture"]=> string(77) "http://images.ak.instagram.com/profiles/profile_531790154_75sq_1377896593.jpg" ["full_name"]=> string(14) "George W. Bush" ["id"]=> string(9) "531790154" } [4]=> object(stdClass)#8 (6) { ["username"]=> string(11) "georgelopez" ["bio"]=> string(50) "New Saint George Episode April 10th on #FXNetworks" ["website"]=> string(26) "http://www.georgelopez.com" ["profile_picture"]=> string(76) "http://images.ak.instagram.com/profiles/profile_38811207_75sq_1390655218.jpg" ["full_name"]=> string(12) "George Lopez" ["id"]=> string(8) "38811207" } [5]=> object(stdClass)#9 (6) { ["username"]=> string(14) "georgerauscher" ["bio"]=> string(33) "photography | munich - Impressum:" ["website"]=> string(31) "http://www.george.li/impressum/" ["profile_picture"]=> string(77) "http://images.ak.instagram.com/profiles/profile_237349510_75sq_1391160241.jpg" ["full_name"]=> string(18) "George A. Rauscher" ["id"]=> string(9) "237349510" }
Presuming the vardump if of the $result variable, the object array you want to iterate is contained within the data property:
foreach ($result->data as $object) {
print $object->username;
}

Search Array and find Key Value

I have a array
array(153) { [0]=> string(47) "colg - colgate palmolive (pakistan) ltd. " [1]=> string(47) "gatm - gul ahmed textile mills ltd. " [2]=> string(47) "atlh - atlas honda limited " [3]=> string(33) "ffbl - fauji fertilizer bin qasim" [4]=> string(16) " - general news" [5]=> string(49) "searl - the searl company ltd. " [6]=> string(47) "jdws - j. d. w. sugar mills ltd. " [7]=> string(47) "aabs - al- abbas sugar mills limited. " [8]=> string(37) "hmb - habib metropolitan bank limited" [9]=> string(37) "hmb - habib metropolitan bank limited" [10]=> string(23) "ogdc - oil & gas dev.co" [11]=> string(39) "ffc - fauji fertilizer company limited." [12]=> string(46) "bop - bank of punjab limited. " [13]=> string(46) "bop - bank of punjab limited. " [14]=> string(30) "luck - lucky cement limited " [15]=> string(47) "natf - national foods limited. " [16]=> string(46) "pol - pakistan oilfields ltd. " [17]=> string(47) "psmc - pak suzuki motor co. ltd. " [18]=> string(35) "nbp - national bank of pakistan ltd" [19]=> string(27) "ncpl - nishat chunian power" [20]=> string(25) "tgl - tariq glass limited" [21]=> string(47) "bahl - bank al-habib limited " [22]=> string(33) "ffbl - fauji fertilizer bin qasim" [23]=> string(136) " kse 100 kse 30 kmi 30 " [24]=> string(9) "26,730.82" [25]=> string(5) "60.22" [26]=> string(18) "volume: 96,370,040" [27]=> string(9) "19,289.00" [28]=> string(6) "-18.68" [29]=> string(18) "volume: 43,582,400" [30]=> string(9) "43,968.61" [31]=> string(6) "-61.00" [32]=> string(18) "volume: 27,252,400" [33]=> string(3) "anl" [34]=> string(4) "9.73" [35]=> string(4) "0.58" [36]=> string(10) "23,192,000" [37]=> string(4) "jscl" [38]=> string(5) "12.53" [39]=> string(4) "1.00" [40]=> string(10) "20,936,000" [41]=> string(4) "jsil" [42]=> string(5) "13.75" [43]=> string(4) "0.35" [44]=> string(10) "10,966,500" [45]=> string(5) "power" [46]=> string(4) "8.00" [47]=> string(4) "0.71" [48]=> string(9) "7,161,000" [49]=> string(4) "piaa" [50]=> string(4) "9.80" [51]=> string(4) "0.16" [52]=> string(9) "6,978,500" [53]=> string(4) "lpcl" [54]=> string(5) "10.40" [55]=> string(5) "-0.04" [56]=> string(9) "5,988,500" [57]=> string(3) "bop" [58]=> string(5) "11.34" [59]=> string(4) "0.11" [60]=> string(9) "5,785,000" [61]=> string(4) "byco" [62]=> string(4) "9.39" [63]=> string(4) "0.47" [64]=> string(9) "4,972,000" [65]=> string(5) "kasbb" [66]=> string(4) "2.39" [67]=> string(4) "0.34" [68]=> string(9) "4,910,500" [69]=> string(3) "spl" [70]=> string(5) "19.78" [71]=> string(4) "0.65" [72]=> string(9) "4,600,000" [73]=> string(6) "nestle" [74]=> string(9) "11,989.16" [75]=> string(6) "570.91" [76]=> string(5) "2,100" [77]=> string(4) "iltm" [78]=> string(8) "1,564.92" [79]=> string(5) "74.52" [80]=> string(3) "500" [81]=> string(5) "mureb" [82]=> string(6) "913.18" [83]=> string(5) "43.48" [84]=> string(6) "57,500" [85]=> string(5) "wyeth" [86]=> string(8) "4,600.00" [87]=> string(5) "33.50" [88]=> string(3) "600" [89]=> string(4) "colg" [90]=> string(8) "1,700.00" [91]=> string(5) "15.50" [92]=> string(3) "140" [93]=> string(4) "hino" [94]=> string(6) "292.87" [95]=> string(5) "13.94" [96]=> string(3) "400" [97]=> string(3) "ici" [98]=> string(6) "323.00" [99]=> string(5) "11.55" [100]=> string(7) "103,500" [101]=> string(4) "pict" [102]=> string(6) "270.98" [103]=> string(4) "9.88" [104]=> string(6) "29,200" [105]=> string(4) "pkgs" [106]=> string(6) "309.00" [107]=> string(4) "9.04" [108]=> string(6) "37,800" [109]=> string(4) "jdmt" [110]=> string(6) "168.49" [111]=> string(4) "8.02" [112]=> string(5) "3,500" [113]=> string(4) "upfl" [114]=> string(8) "9,100.00" [115]=> string(6) "-90.00" [116]=> string(3) "120" [117]=> string(4) "mffl" [118]=> string(6) "855.00" [119]=> string(6) "-43.33" [120]=> string(3) "300" [121]=> string(4) "siem" [122]=> string(8) "1,331.00" [123]=> string(6) "-38.00" [124]=> string(3) "600" [125]=> string(4) "pakt" [126]=> string(6) "620.00" [127]=> string(6) "-27.90" [128]=> string(5) "1,000" [129]=> string(3) "sfl" [130]=> string(6) "409.00" [131]=> string(6) "-15.90" [132]=> string(5) "9,400" [133]=> string(4) "gadt" [134]=> string(6) "286.00" [135]=> string(6) "-13.37" [136]=> string(6) "48,800" [137]=> string(4) "atba" [138]=> string(6) "394.79" [139]=> string(6) "-12.96" [140]=> string(3) "900" [141]=> string(3) "btl" [142]=> string(6) "265.51" [143]=> string(5) "-8.49" [144]=> string(5) "1,200" [145]=> string(4) "natf" [146]=> string(6) "616.00" [147]=> string(5) "-8.26" [148]=> string(5) "1,200" [149]=> string(4) "admm" [150]=> string(6) "121.51" [151]=> string(5) "-6.39" [152]=> string(6) "98,500" }
And i want to find "kse 100 kse 30 kmi 30" which is located at key number 23
I have tried in_array and other php array functions including preg_match but it dont find string in array. Is there a way to find string in my array. If yes then tell me
Well your "kse 100 kse 30 kmi 30" has a space " " before and after the string in your array.
string(136) " kse 100 kse 30 kmi 30 "
Is this the reason why you cannot find it? Because array_serach, in_array and other functions are looking for exact values.
Otherwise use strstr to compare if your string is inside your haystack but be careful. If you have similar content like "Hello" and "Hello World" in your array you might find the wrong entry to this is not really a good unique solution.
Anyway I am leaving a quick example:
$found = false;
foreach($myArray as $key => $value) {
if(strstr($value, "kse 100 kse 30 kmi 30") == true) {
$found = true;
break;
}
}
if($found == true) {
//Do something awesome!
} else {
//Do something not so awesome but still cool?
}
You should make use of array_search
Searches the array for a given value and returns the corresponding key if successful
So it must be like ..
echo $key = array_search('kse 100 kse 30 kmi 30', $yourarray); //"prints" 23
EDIT:
<?php
$array = [
0 => "colg - colgate palmolive (pakistan) ltd. ",
1 => "gatm - gul ahmed textile mills ltd. ",
2 => "atlh - atlas honda limited ",
3 => "ffbl - fauji fertilizer bin qasim",
4 => " - general news",
5 => "searl - the searl company ltd. ",
23 => " kse 100 kse 30 kmi 30 ",
24 => "26,730.82",
];
$array = array_map('trim',$array);
$searchText = 'kse 100 kse 30 kmi 30';
$searchText = trim($searchText);
echo $key = array_search($searchText, $array); //"prints" 23
Demo
Thanks For you help but i sort it out myself
for ($index = 10; $index < 35; $index++) {
if (substr_count(array[$index], 'kse 100') > 0) {
$IndexKey = $index;
}
}
Got The value of key where that variable is.
For the first one that matches:
$key = key(preg_grep('/kse 100/', $array));

parsing the Paypal REST API response

I have json_decoded my paypal rest api response and gotten this:
["body"]=> object(stdClass)#4 (8) {
["id"]=> string(28) "PAY-66D616332R6551639KJLSMVQ"
["create_time"]=> string(20) "2013-10-10T22:12:38Z"
["update_time"]=> string(20) "2013-10-10T22:12:39Z"
["state"]=> string(7) "created"
["intent"]=> string(4) "sale"
["payer"]=> object(stdClass)#5 (2) {
["payment_method"]=> string(6) "paypal"
["payer_info"]=> object(stdClass)#6 (0) {}
}
["transactions"]=> array(1) {
[0]=> object(stdClass)#7 (3) {
["amount"]=> object(stdClass)#8 (3) {
["total"]=> string(6) "500.85"
["currency"]=> string(3) "USD"
["details"]=> object(stdClass)#9 (2) {
["subtotal"]=> string(6) "460.90"
["shipping"]=> string(5) "39.95"
}
}
["description"]=> string(43) "Mike and Maureen Photography - Order ID #10"
["item_list"]=> object(stdClass)#10 (1) {
["items"]=> array(2) {
[0]=> object(stdClass)#11 (5) {
["name"]=> string(48) "The Bean-8" x 10" - floating frame - black frame"
["sku"]=> string(7) "20 - 13"
["price"]=> string(6) "160.95"
["currency"]=> string(3) "USD"
["quantity"]=> string(1) "1"
}
[1]=> object(stdClass)#12 (5) {
["name"]=> string(62) "40 Steps and a View-36" x 48" - 0.75" thin gallery wrap canvas"
["sku"]=> string(5) "7 - 6"
["price"]=> string(6) "299.95"
["currency"]=> string(3) "USD"
["quantity"]=> string(1) "1"
}
}
}
}
}
["links"]=> array(3) {
[0]=> object(stdClass)#13 (3) {
["href"]=> string(79) "https://api.sandbox.paypal.com/v1/payments/payment/PAY-66D616332R6551639KJLSMVQ"
["rel"]=> string(4) "self"
["method"]=> string(3) "GET"
}
[1]=> object(stdClass)#14 (3) {
["href"]=> string(94) "https://www.sandbox.paypal.com/cgi-bin/webscr?cmd=_express-checkout&token=EC-1XB37931V5368954G"
["rel"]=> string(12) "approval_url"
["method"]=> string(8) "REDIRECT"
}
[2]=> object(stdClass)#15 (3) {
["href"]=> string(87) "https://api.sandbox.paypal.com/v1/payments/payment/PAY-66D616332R6551639KJLSMVQ/execute"
["rel"]=> string(7) "execute"
["method"]=> string(4) "POST"
}
}
}
I am trying to check the value of 'state' but I cant seem to figure out how to reference that value. I have tried: ($result is the variable where the array is located)
$result['body']['state']
$result['state']
$body['state']
None of those work, so can anyone tell me how to reference the key 'state' in that mess up there? I am usually pretty good with PHP but for some reason I can not figure this one out.
Thanks for your help.
Edit
I have formatted the response so its easier to ready but I am stuck on selecting the [1] href value. If I base it on the previous example I would use
$result['body']->link but how do I get past that to the specific href in [1]?
The formatting doesn't help, but it looks like $result['body']->state will get you what you're looking for.
You were closest with your first attempt, $result['body']['state'], however $result['body'] is an object, and so you need to use -> to access its properties.

How to read var_dump in order to know how to load its variable contents in to an array with a foreach?

Here is a sample var_dump. Now, how do I know how to construct a foreach from it to load any particular word or fragment in to an array?
object(stdClass)#1 (2)
{
["noun"]=>
object(stdClass)#2 (1)
{
["syn"]=> array(24)
{
[0]=> string(12) "domestic dog"
[1]=> string(16) "Canis familiaris"
[2]=> string(5) "frump"
[3]=> string(3) "cad"
[4]=> string(7) "bounder"
[5]=> string(10) "blackguard"
[6]=> string(5) "hound"
[7]=> string(4) "heel"
[8]=> string(5) "frank"
[9]=> string(11) "frankfurter"
[10]=> string(6) "hotdog"
[11]=> string(7) "hot dog"
[12]=> string(6) "wiener"
[13]=> string(11) "wienerwurst"
[14]=> string(6) "weenie"
[15]=> string(4) "pawl"
[16]=> string(6) "detent"
[17]=> string(5) "click"
[18]=> string(7) "andiron"
[19]=> string(7) "firedog"
[20]=> string(8) "dog-iron"
[21]=> string(8) "blighter"
[22]=> string(5) "canid"
[23]=> string(6) "canine"
[24]=> string(5) "catch"
}
}
}
Before we can decipher it, we have to format it.
Tobias Kun's answer shows a very good way to format the var_dump output so you can read it.
object(stdClass)#1 (2)
{
["noun"]=>
object(stdClass)#2 (1)
{
["syn"]=> array(24)
{
[0]=> string(12) "domestic dog"
[1]=> string(16) "Canis familiaris"
[2]=> string(5) "frump"
[3]=> string(3) "cad"
[4]=> string(7) "bounder"
[5]=> string(10) "blackguard"
[6]=> string(5) "hound"
[7]=> string(4) "heel"
[8]=> string(5) "frank"
[9]=> string(11) "frankfurter"
[10]=> string(6) "hotdog"
[11]=> string(7) "hot dog"
[12]=> string(6) "wiener"
[13]=> string(11) "wienerwurst"
[14]=> string(6) "weenie"
[15]=> string(4) "pawl"
[16]=> string(6) "detent"
[17]=> string(5) "click"
[18]=> string(7) "andiron"
[19]=> string(7) "firedog"
[20]=> string(8) "dog-iron"
[21]=> string(8) "blighter"
[22]=> string(5) "canid"
[23]=> string(6) "canine"
[24]=> string(5) "catch"
}
}
}
You have a stdClass object with a property called ,"noun". noun is an abject with a property called "syn", which is an array of strings.
Suppose we call the object $object. Then we can access the array like:
echo $object->noun->syn[23];
which gives us canine. So a loop might look like:
foreach($data->noun->syn as $value) {
echo $value . "<br>\n";
}
First of all you should really increase the quality of your questions. The code is not formatted at all.
If you use echo "<pre>" . print_r($your_data_object_or_array,1) . "</pre>" your data will be formatted fine.
If i understand you right this should help you:
foreach($data['noun']['syn'] as $value) {
//with this you loop through all your words in "syn" e.g. domestic, "Canis familiaris etc."
echo $value . "<br>";
}
//Ouput:
domestic
Canis familiaris
frump
cad
etc ...

Categories