2014-10-20 33 views
1

我正在使用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&rsquo;s thick blanket of crystal trichomes that contributes to its staggeringly high THC content of up to 26 percent. Crystal Coma&rsquo;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&rsquo;s potency, a medicine that is highly recommended for nighttime use.&nbsp;Crystal Coma took 3rd place in&nbsp;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&rsquo;s thick blanket of crystal trichomes that contributes to its staggeringly high THC content of up to 26 percent. Crystal Coma&rsquo;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&rsquo;s potency, a medicine that is highly recommended for nighttime use.&nbsp;Crystal Coma took 3rd place in&nbsp;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

這是整個文件還是代碼繼續? – Kypros 2014-10-20 18:05:13

+0

'$ a [$ value]'不正確。 $ a在這一點上是** KEY **,而不是數組本身。 – 2014-10-20 18:08:41

+0

Kypros,它是整個代碼。我實際上只是將它們移動到他們需要的位置,然後將變量從'$ a [$ value'];'更改爲'$ value ['name'];'並且它非常完美! – 2014-10-20 22:08:42

回答

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 
           ^
     } 

修復,對於所有的循環,你是好

+0

謝謝,那正是我所做的。感謝您的回覆! – 2014-10-20 22:09:40

1

兩個問題,我可以在這段代碼先看看看:

  1. ,如果這是你的整個文件,你是不是爲了他們,以顯示到瀏覽器,輸出以任何方式值
  2. 每次要覆蓋變量,而不是附加給他們(使用.=
  3. 您正在使用key =>value對錯誤,即把你應該做同樣的事情到配方的名稱:

    $effects = ""; 
    foreach($i['effects'] as $a=>$value){ 
        $effects .= '<li>'. $value['name'] .'</li>'.PHP_EOL;  
    } 
    
    echo $effects; 
    

雖然這是未經測試的代碼,它應該作爲$value在裏面工作的循環仍然是一個數組,你需要添加name或其他適當的索引來得到所需的值。

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; 
     }