我正在使用一个返回数组的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;
}
}
但它没有返回结果。
答案 0 :(得分:1)
我可以在这段代码中看到两个问题:
.=
)您正在使用key
=&gt; value
对错误,即取出您应该执行类似操作的食谱名称:
$effects = "";
foreach($i['effects'] as $a=>$value){
$effects .= '<li>'. $value['name'] .'</li>'.PHP_EOL;
}
echo $effects;
虽然这是未经测试的代码,但它应该在循环内的$value
仍然是一个数组,你需要添加name
或其他适当的索引来获得所需的值。
答案 1 :(得分:1)
举一个例子
foreach($i['effects'] as $a=>$value){
$effects = '<li>'. $a[$value] .'</li>'.PHP_EOL;
}
这是一个提取效果的错误循环。 $a
是每个效果的关键,$value
是包含名称和分数的数组。所以你不能说$a[$value];
。它应该是
foreach($i['effects'] as $a=>$value){
$effects. = '<li>'. $value['score'] .'</li>'.PHP_EOL; // or name
^
}
修复所有循环,你很好
答案 2 :(得分:0)
在主foreach循环旁边定义变量
请注意[popularCities]
不是多维数组
$effects = $symptoms = $negatives = $popularCities = "";
//foreach($cleaned_response as $i)
foreach($i['effects'] as $a=>$value){
$effects .= '<li>'. $value['name'] .'</li>';
$effects .= '<li>'. $value['score'] .'</li>'.PHP_EOL;
}
foreach($i['symptoms'] as $a=>$value){
$symptoms .= '<li>'. $value['name'] .'</li>';
$symptoms .= '<li>'. $value['score'] .'</li>'.PHP_EOL;
}
foreach($i['negatives'] as $a=>$value){
$negatives .= '<li>'. $value['name'] .'</li>';
$negatives .= '<li>'. $value['score'] .'</li>'.PHP_EOL;
}
foreach($i['popularCities'] as $a=>$value){
$popularCities .= '<li>'. $value .'</li>'.PHP_EOL;
}