我正在使用api返回一個數組,我試圖設置每個條件的變量,我需要但無法弄清楚。我想加載數組中的變量
這裏是數組:
Array
(
[0] => Array
(
[sl_translate] => description,descriptionPlain,category
[id] => 65392
[slug] => crystal-coma
[name] => Crystal Coma
[symbol] => Crc
[category] => Sativa
[description] => <p class="p1">Crystal Coma is a rare sativa-dominant hybrid from California that induces deep, trance-like effects. Its name foreshadows the debilitating relaxation to come, a long-lasting calm that shuts off mental overactivity. Pastel green breaks through this sativa’s thick blanket of crystal trichomes that contributes to its staggeringly high THC content of up to 26 percent. Crystal Coma’s genetics are long lost, but myth has it that the origins lie in <a href="http://www.leafly.com/indica/cheese"><span class="s1">Cheese</span></a> and <a href="http://www.leafly.com/sativa/skunk-1"><span class="s1">Skunk #1</span></a>. Anxiety, PTSD, pain, and sleeplessness are no match for Crystal Coma’s potency, a medicine that is highly recommended for nighttime use. Crystal Coma took 3rd place in the 2014 L.A. Cannabis Cup.</p>
[descriptionPlain] => Crystal Coma is a rare sativa-dominant hybrid from California that induces deep, trance-like effects. Its name foreshadows the debilitating relaxation to come, a long-lasting calm that shuts off mental overactivity. Pastel green breaks through this sativa’s thick blanket of crystal trichomes that contributes to its staggeringly high THC content of up to 26 percent. Crystal Coma’s genetics are long lost, but myth has it that the origins lie in Cheese and Skunk #1. Anxiety, PTSD, pain, and sleeplessness are no match for Crystal Coma’s potency, a medicine that is highly recommended for nighttime use. Crystal Coma took 3rd place in the 2014 L.A. Cannabis Cup.
[aka] =>
[rating] => 5
[reviewCount] => 6
[flavors] => Array
(
[0] => Array
(
[name] => Lemon
[score] => 40
)
[1] => Array
(
[name] => Pungent
[score] => 26.25
)
[2] => Array
(
[name] => Cheese
[score] => 20
)
[3] => Array
(
[name] => Flowery
[score] => 20
)
[4] => Array
(
[name] => Skunk
[score] => 20
)
)
[effects] => Array
(
[0] => Array
(
[name] => Euphoric
[score] => 105
)
[1] => Array
(
[name] => Relaxed
[score] => 92.5
)
[2] => Array
(
[name] => Energetic
[score] => 85
)
[3] => Array
(
[name] => Uplifted
[score] => 78.75
)
[4] => Array
(
[name] => Happy
[score] => 65
)
)
[symptoms] => Array
(
[0] => Array
(
[name] => Depression
[score] => 51.25
)
[1] => Array
(
[name] => Stress
[score] => 51.25
)
[2] => Array
(
[name] => Pain
[score] => 38.75
)
[3] => Array
(
[name] => Fatigue
[score] => 26.25
)
[4] => Array
(
[name] => Lack of Appetite
[score] => 25
)
)
[conditions] => Array
(
[0] => Array
(
[name] => ADD/ADHD
[score] => 32.5
)
[1] => Array
(
[name] => Anxiety
[score] => 32.5
)
[2] => Array
(
[name] => PTSD
[score] => 32.5
)
[3] => Array
(
[name] => Asthma
[score] => 20
)
[4] => Array
(
[name] => Arthritis
[score] => 6.25
)
)
[negatives] => Array
(
[0] => Array
(
[name] => Dizzy
[score] => 6.25
)
[1] => Array
(
[name] => Dry Eyes
[score] => 6.25
)
[2] => Array
(
[name] => Dry Mouth
[score] => 6.25
)
)
[articlesAvailable] => 1
[photos] => Array
(
[0] => Array
(
[uploaded] => /Date(1398397718127)/
[thumb] => http://leafly.blob.core.windows.net/reviews/crystal-coma_100x100_255e.jpg
[fullsize] => http://d3odcnigi1nnzz.cloudfront.net/cdn/strain-photo/132654/b/crystal-coma_825x550_4e1f.jpg
)
)
[popularCities] => Array
(
[0] => Colorado Springs,CO
[1] => Vancouver,BC
[2] => North Palm Springs,CA
[3] => Temecula,CA
[4] => Torrance,CA
)
[permalink] => http://www.leafly.com/sativa/crystal-coma
[starImage] => //d3odcnigi1nnzz.cloudfront.net/stars/5/240
[testGraph] =>
[weakDescription] =>
[parents] => Array
(
)
[growInfo] => Array
(
[difficulty] =>
[preferredMedium] =>
[floweringDays] => 0
[outdoorFinish] =>
[height] =>
[averageYield] =>
[environment] =>
[growNotes] =>
)
)
)
我試圖讓中提取出數組的效果,症狀,底片和流行的城市。
這裏是我到目前爲止已經試過:
$strain = $_REQUEST['strain'];
//init curl
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, "http://data.leafly.com/strains/".$strain); //change the strain dynamically of course for your app
curl_setopt($ch,CURLOPT_HTTPHEADER,array('APP_ID:'.$appID,'APP_KEY:'.$appKEY));
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
$output = curl_exec($ch);
//Clean it into an php array object and set var
$cleaned_response = array(json_decode($output,true));
//Set Variables
foreach($cleaned_response as $i)
{
$titles=$i['name'];
$category = $i['category'];
$description = $i['description'];
$rating = $i['rating'];
//Convert this array to variables
foreach($i['effects'] as $a=>$value){
$effects = '<li>'. $a[$value] .'</li>'.PHP_EOL;
}
foreach($i['symptoms'] as $a=>$value){
$symptoms = '<li>'. $a[$value] .'</li>'.PHP_EOL;
}
foreach($i['negatives'] as $a=>$value){
$negatives = '<li>'. $a[$value] .'</li>'.PHP_EOL;
}
foreach($i['popularCities'] as $a=>$value){
$popularCities = '<li>'. $a[$value] .'</li>'.PHP_EOL;
}
}
,但它沒有返回結果。
這是整個文件還是代碼繼續? – Kypros 2014-10-20 18:05:13
'$ a [$ value]'不正確。 $ a在這一點上是** KEY **,而不是數組本身。 – 2014-10-20 18:08:41
Kypros,它是整個代碼。我實際上只是將它們移動到他們需要的位置,然後將變量從'$ a [$ value'];'更改爲'$ value ['name'];'並且它非常完美! – 2014-10-20 22:08:42