我使用的是一个返回数组的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;
}
}
但它不会返回结果。
以为例
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
^
}
修复所有循环的问题,您的就很好了
我在这段代码中第一次看到的两个问题:
- 如果这是你的整个文件,你不会以任何方式输出值,以便它们显示在浏览器上
- 您每次都要覆盖变量,而不是附加到变量上(使用
.=
) -
你用错了
key
=>value
对,即要取食谱的名字,你应该做类似的事情:$effects = ""; foreach($i['effects'] as $a=>$value){ $effects .= '<li>'. $value['name'] .'</li>'.PHP_EOL; } echo $effects;
尽管这是未经测试的代码,但它应该可以工作,因为循环中的$value
仍然是一个数组,您需要添加name
或其他适当的索引来获得所需的值。
定义主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;
}