2017-10-17 2 views
1

Ich versuche, Daten aus dieser JSON-Datei zu bekommen, aber ich brauche die Daten, die das erweiterte benutzerdefinierte Feld ich eingerichtet.WooCommerce: Get JSON Daten in mehrdimensionalen Array übereinstimmenden ACF-Feld

$str = file_get_contents('http://gold.explorethatstore.com/wp-content/themes/divi-ETS-child-theme/run_results_bgasc.json'); 

// decode JSON 
$json = json_decode($str, true); 

// default value 
$coinPrice = "Not Available"; 
$vendorName = get_field('bgasc_vendor_name'); 
// loop the json array 
foreach($json['coin'] as $value){ 
     // check the condition 
     if($value['coin_name'] == $vendorName){ 
       $coinPrice = $value['url']; // get the price 
       break; // exit the loop 
     } 
} 

echo $coinPrice; 
+0

Es ist ein Problem, da manchmal hat zum Beispiel der Kategoriename "Gold American Eagles" ein "Gewicht" -Array, aber "Gold American Buffalos" hat kein Gewicht-Array (1 Multi-Level-Array weniger) ... Das ist also ein Problem. Alle Kategorien Namen sollten die gleiche Struktur haben ... – LoicTheAztec

+0

Hmm gut, das ist die Art, wie es auf der Website gecrawlt wird, werden einige der Ergebnisse zurückkommen, und einige Kategorien werden keine haben. das PHP wird das Array nicht nach Namen identifizieren? – AaronS

+0

meine Entschuldigung! hier ist es: http://gold.explorethatstore.com/wp-content/themes/divi-ETs-child-theme/run_results_bgasc_gold.json – AaronS

Antwort

2

So, hier ist der richtige Code, der beide Array Fälle behandelt (mit oder ohne "Gewicht" Array):

$str = file_get_contents('http://gold.explorethatstore.com/wp-content/themes/divi-ETS-child-theme/run_results_bgasc_gold.json'); 

// Set te Data in a multi-dimensional array 
$json = json_decode($str, true); 

// Default variable values 
$coin_price = "Not Available"; 
$url = ''; 
$break = false; 

// Get the vendor name (like the "coin_name" value) 
$vendorName = get_field('bgasc_vendor_name'); 


// Go through multi-dimensional array with multiple loops 
foreach ($json['categories'] as $category){ 
    // Case with "weight" additional array 
    if(array_key_exists ('weight' , $category)){ 
     foreach ($category['weight'] as $weight){ 
      foreach ($weight['coin'] as $coin){ 
       // check the condition 
       if($coin['coin_name'] == $vendor_name){ 
        $coin_price = $coin['price']; // get the price 
        $url = $coin['url']; // get the url 
        $break = true; // (exit other loops) 
        break; // exit the loop 
       } 
      } 
      if($break) break; // exit the loop 
     } 
     if($break) break; // exit the loop 
    } else { // Case without "weight" additional array 
     foreach ($category['coin'] as $coin){ 
      // check the condition 
      if($coin['coin_name'] == $vendor_name){ 
       $coin_price = $coin['price']; // get the price 
       $url = $coin['url']; // Get the url 
       $break = true; // (exit other loops) 
       break; // exit the loop 
      } 
     } 
     if($break) break; // exit the loop 
    } 
} 

// output price 
echo $coin_price; 

// output URL 
echo $url; 

Dieser Code wird getestet und funktioniert

+1

Vielen Dank, hat perfekt funktioniert. Ich sehe, was Sie getan haben, wenn die "array_key_exists" Das macht Sinn. Vielen Dank für Ihre Hilfe. Du hast heute viele Probleme für mich gelöst. – AaronS