2017-06-25 6 views
0

から特定のキーからすべての値を取得する:PHPは、私は、SOAP Webサービスからこのようなオブジェクトを取得し、配列+オブジェクト

object(stdClass)#2 (1) { 
    ["tasks"]=> 
    array(3) { 
    [0]=> 
    object(stdClass)#3 (9) { 
     ["externalId"]=> 
     string(0) "" 
     ["name"]=> 
     string(10) "Customer A" 
     ["description"]=> 
     string(0) "" 
     ["bookable"]=> 
     bool(true) 
     ["billable"]=> 
     bool(true) 
     ["subtasks"]=> 
     array(2) { 
     [0]=> 
     object(stdClass)#4 (9) { 
      ["externalId"]=> 
      string(1) "2" 
      ["name"]=> 
      string(5) "Job 1" 
      ["description"]=> 
      string(0) "" 
      ["bookable"]=> 
      bool(true) 
      ["billable"]=> 
      bool(true) 
      ["subtasks"]=> 
      object(stdClass)#5 (9) { 
      ["externalId"]=> 
      string(0) "" 
      ["name"]=> 
      string(7) "Job 1.1" 
      ["description"]=> 
      string(0) "" 
      ["bookable"]=> 
      bool(true) 
      ["billable"]=> 
      bool(true) 
      ["subtasks"]=> 
      array(2) { 
       [0]=> 
       object(stdClass)#6 (8) { 
       ["externalId"]=> 
       string(0) "" 
       ["name"]=> 
       string(9) "Job 1.1.1" 
       ["description"]=> 
       string(0) "" 
       ["bookable"]=> 
       bool(true) 
       ["billable"]=> 
       bool(true) 
       ["customField1"]=> 
       string(0) "" 
       ["customField2"]=> 
       string(0) "" 
       ["customField3"]=> 
       string(0) "" 
       } 
       [1]=> 
       object(stdClass)#7 (8) { 
       ["externalId"]=> 
       string(0) "" 
       ["name"]=> 
       string(9) "Job 1.1.2" 
       ["description"]=> 
       string(0) "" 
       ["bookable"]=> 
       bool(true) 
       ["billable"]=> 
       bool(true) 
       ["customField1"]=> 
       string(0) "" 
       ["customField2"]=> 
       string(0) "" 
       ["customField3"]=> 
       string(0) "" 
       } 
      } 
      ["customField1"]=> 
      string(0) "" 
      ["customField2"]=> 
      string(0) "" 
      ["customField3"]=> 
      string(0) "" 
      } 
      ["customField1"]=> 
      string(0) "" 
      ["customField2"]=> 
      string(0) "" 
      ["customField3"]=> 
      string(0) "" 
     } 
     [1]=> 
     object(stdClass)#8 (9) { 
      ["externalId"]=> 
      string(0) "" 
      ["name"]=> 
      string(5) "Job 2" 
      ["description"]=> 
      string(0) "" 
      ["bookable"]=> 
      bool(true) 
      ["billable"]=> 
      bool(true) 
      ["subtasks"]=> 
      object(stdClass)#9 (8) { 
      ["externalId"]=> 
      string(2) "55" 
      ["name"]=> 
      string(7) "Job 2.1" 
      ["description"]=> 
      string(0) "" 
      ["bookable"]=> 
      bool(true) 
      ["billable"]=> 
      bool(true) 
      ["customField1"]=> 
      string(0) "" 
      ["customField2"]=> 
      string(0) "" 
      ["customField3"]=> 
      string(0) "" 
      } 
      ["customField1"]=> 
      string(0) "" 
      ["customField2"]=> 
      string(0) "" 
      ["customField3"]=> 
      string(0) "" 
     } 
     } 
     ["customField1"]=> 
     string(0) "" 
     ["customField2"]=> 
     string(0) "" 
     ["customField3"]=> 
     string(0) "" 
    } 
    [1]=> 
    object(stdClass)#10 (8) { 
     ["externalId"]=> 
     string(3) "123" 
     ["name"]=> 
     string(10) "Customer B" 
     ["description"]=> 
     string(0) "" 
     ["bookable"]=> 
     bool(true) 
     ["billable"]=> 
     bool(true) 
     ["customField1"]=> 
     string(0) "" 
     ["customField2"]=> 
     string(0) "" 
     ["customField3"]=> 
     string(0) "" 
    } 
    [2]=> 
    object(stdClass)#11 (9) { 
     ["externalId"]=> 
     string(0) "" 
     ["name"]=> 
     string(10) "Customer C" 
     ["description"]=> 
     string(0) "" 
     ["bookable"]=> 
     bool(true) 
     ["billable"]=> 
     bool(true) 
     ["subtasks"]=> 
     object(stdClass)#12 (8) { 
     ["externalId"]=> 
     string(3) "918" 
     ["name"]=> 
     string(5) "Job 1" 
     ["description"]=> 
     string(0) "" 
     ["bookable"]=> 
     bool(true) 
     ["billable"]=> 
     bool(true) 
     ["customField1"]=> 
     string(0) "" 
     ["customField2"]=> 
     string(0) "" 
     ["customField3"]=> 
     string(0) "" 
     } 
     ["customField1"]=> 
     string(0) "" 
     ["customField2"]=> 
     string(0) "" 
     ["customField3"]=> 
     string(0) "" 
    } 
    } 
} 

は、私は外部IDのすべての要素をチェックするだけ愚かな機能を必要としています。

if(["externalId"] != "") 
{ 
    echo "value found! ".["externalId"]."<br>"; 
} 

私はarray_walk_recursiveを使用しようとしましたが、オブジェクトは配列とオブジェクトが混在しています。

["subtasks"]要素を比較... SOAP呼び出しでSOAP_SINGLE_ELEMENT_ARRAYSを試してみました。違いはありません。 「マジック」はありますか?私は解決策がかなりシンプルだと思うが、まだそれを得ることはない:-(

注意:再帰レベルの制限はありません - すべての["subtasks"]には["subtasks"]以上が含まれています

答えて

0

この

<?php 

/** 
* @param \stdClass $root 
* 
* @return array 
*/ 
function externalIdentifiersFrom(\stdClass $root) 
{ 
    static $properties = [ 
     'tasks', 
     'subtasks', 
    ]; 

    $identifiers = []; 

    if (property_exists($root, 'externalId') 
     && is_string($root->externalId) 
     && '' !== trim($root->externalId) 
    ) { 
     $identifiers[] = $root->externalId; 
    } 

    foreach ($properties as $property) { 
     if (property_exists($root, $property) && is_array($root->{$property})) { 
      $objects = $root->{$property}; 

      foreach ($objects as $object) { 
       if (!$object instanceof \stdClass) { 
        continue; 
       } 

       $identifiers = array_merge(
        $identifiers, 
        externalIdentifiersFrom($object) 
       ); 
      } 
     } 
    } 

    return array_unique($identifiers); 
} 

var_dump(externalIdentifiersFrom($object)); 
を試していただき、ありがとうございます
関連する問題