2016-05-11 10 views
1

Ich versuche, einen Wert basierend auf einem Attribut aus einem Array zurückzuziehen, und es scheint gerade genug, aber ich kann nicht scheinen, den richtigen Weg zu nageln Dies.PHP-Array Überprüfen Sie das Attribut, Rückgabewert basierend auf Attribut

Hier das Array ich von ziehen versuchen:

[1] => InfoOptions Object 
      (
      [description] => INFO 
      [optSequence] => 2 
      [eqpObject] => CUSTOMER NTWK ENG 
      [attribute] => 
      [eqpValue] => 
      [dlrSequence] => 10 
      ) 

[2] => InfoOptions Object 
      (
      [description] => 
      [optSequence] => 3 
      [eqpObject] => CUSTOMER TEST 
      [attribute] => CUSTOMER 
      [eqpValue] => Jon Doe 
      [dlrSequence] => 10 
      ) 

Hier ist, was ich bisher:

if (is_array($provisionCVResult->path->infoOptions-_InfoOptions)) {  
    foreach ($provisionCVResult->path->infoOptions ->InfoOptions as $cv_obj) { 
     $CVA = array(); 
     $result = null; 

     foreach ($CV_obj as $value) { 
      if($value['attribute'] == 'CUSTOMER') { 
       $CVA["eqpValue"] = $cv_obj->eqpValue; 
       break; 
      } 
     } 

     $this->cvArrayDataList[] = $CVA; 
    } 
} 

Wohin gehe ich falsch?

+1

Schwer zu sagen, ohne mehr von '$ provisionCVResult' aber' error_reporting (E_ALL); ini_set ('display_errors', '1'); ' – AbraCadaver

+0

was ist die gewünschte Ausgabe? –

+0

Das $ provisionCVResult gibt dieses Array zurück, das gewünschte Ergebnis ist, 'eqpValue' von 'Attribut' zurückzugeben, wenn = an Kunden – LeeBronwin

Antwort

2
  1. Wenn $ provisionCVResult-> Weg-> InfoOptions ein Array ist, ist es nicht sinnvoll $ provisionCVResult-> Weg-> InfoOptions zu schreiben -> InfoOptions im foreach EDIT: I rot in den Kommentaren, dass das Array $ provisionCVResult-> Weg-> InfoOptions-> InfoOptions
  2. ist PHP Groß- und Kleinschreibung so $ cv_obj und $ cV_obj sind zwei verschiedene Variablen
  3. Das zweite foreach ist nicht erforderlich

Also, vorausgesetzt $ provisionCVResult-> Weg-> InfoOptions-> InfoOptions eine Reihe von InfoOptions zurückkehren Objekt, ich glaube, Sie so etwas wie tun soll dies:

if (is_array($provisionCVResult->path->InfoOptions->InfoOptions)) 
{ 

    $result = null; 

    foreach($provisionCVResult->path->InfoOptions->InfoOptions as $cv_obj) 
    { 

     if($cv_obj->attribute == 'CUSTOMER') 
     { 
      $this->cvArrayDataList[] = array("eqpValue" => $cv_obj->eqpValue); 
     } 

    } 

} 
+0

Basierend auf der Antwort von OP in den Kommentaren, klingt es, als gäbe es eine weitere Eigenschaft 'InfoOptions' in' InfoOptions', die das Array enthält. –

+0

OK. Danke, ich habe meine Antwort entsprechend bearbeitet. –

+0

Danke, das habe ich gemacht, sieht jetzt einfach so aus, dass ich es sehe. – LeeBronwin

1

einen kurzen Blick zu haben, versuchen

$value['attribute'] == 'CUSTOMER' 

Um

$value->attribute == 'CUSTOMER' 

Ändern Da das Element eine ist "InfoOptions Objekt" und nicht ein Array.

Hinweis Ich würde auch einen strikten Vergleich empfehlen, z. B. '===' anstelle von '=='.

Verwandte Themen