2013-07-20 75 views
1

我已經爲此搜索了很多,並且發現了幾個類似的問題,但都沒有完全解決我想要完成的任務。PHP - 搜索一個鍵值,然後在同一個對象子數組中找到另一個鍵的值

我想寫一個代碼,將搜索PHP(多)多維數組,以查看哪些子數組包含我擁有的唯一鍵值。然後我希望它返回同一對象子數組中不同鍵的值。

$Arraytosearch = Array(
. 
//various other subarrays 
. 
[fields] => Array (
    . 
    . 
    . 
    //I don't know the number of the object subarray 

    [x] => PodioTextItemField Object ( 
     [__attributes] => Array ( 
     [field_id] => 37325091 
     [type] => text 
     [external_id] => id 
     [label] => ID 
     [values] => Array ( 
      [0] => Array ( 
       [value] => REF100019)) 

     [config] => Array ( 
      [description] => [settings] => Array ( 
       [size] => small) 
      [required] => [mapping] => [label] => ID [visible] => 1 [delta] => 2)) 
    . 
    . 
    . 
    //(and so on) 

我想寫,將返回「REF100019」通過提供FIELD_ID的值的函數=> 37325091.

有些事情我已經試過,我不能去工作: foreachnew RecursiveIterator雖然我讀過的教程對我的情況沒有用處。

即使數組看起來很複雜,我認爲這很容易,因爲我已經有了父數組的字段ID。

預先感謝您提供任何指導或示例代碼!

背景:這是我在向API提交請求後從Podio獲得的響應的一部分。我只是不知道如何採取這種反應,並得到我需要的部分(ID),以便我可以回覆用戶)。

編輯:謝謝Orangepill和Barmar的支持。我試過你的代碼。但是得到一個錯誤,這讓我意識到我沒有給你完整的陣列。我想出瞭如何讓Podio響應以更具可讀性的格式顯示(我之前從Podio調試文件中讀取完整的JSON響應,這是非常令人困惑的),並且計算出整個陣列實際上是如下所示的結構。

然後我把你的代碼,並能夠弄清楚如何使它適用於我的方案(見下文)。考慮到我之前從未寫過任何代碼,我對自己感到非常自豪,但如果沒有您的幫助,我無法做到這一點!再次感謝!

$Arraytosearch = Array(
    [items] => Array(
     [0] => PodioItem Object(
       [_attributes] => Array(
         [fields] => Array (
          [x] => PodioTextItemField Object ( 
            [__attributes] => Array( 
             [field_id] => 37325091 
             [values] => Array( 
               [0] => Array( 
                [value] => REF100019)) 

注:對於任何人誰是新的編程像我一樣,想在波迪奧響應(或JSON字符串)在一個像上面的「漂亮」可讀的格式顯示,使用的代碼(從Display an array in a readable/hierarchical format感謝PHENEX ):

print "<pre>"; 
print_r($Arraytoformat); 
print "</pre>"; 

最後,我用(使用下面Orangepill的答案的全部代碼)的搜索對象和數組,給我什麼我一直在尋找了幾天如下:

$Arraytosearch = PodioItem::filter(APP_ID, $filterParams); 
$fieldID = 37325091;  

    function getFirstValueByFieldId($fieldId, $Arraytosearch){ 
     foreach($Arraytosearch["items"][0]->__attributes["fields"] as $textitem){ 
     if ($textitem->__attributes["field_id"] == $fieldId){ 
     return $textitem->__attributes["values"][0]["value"]; 
    }}} 

$refID = getFirstValueByFieldId($fieldID, $Arraytosearch); 
+0

這不僅是一個多維數組,在那裏有一個對象,以及。您需要使用對象符號來訪問其屬性。 – Barmar

回答

0

有很多方法對皮膚這隻貓......這可能是最直接的

function getFirstValueByFieldId($fieldId, $Arraytosearch){ 
    foreach($Arraytosearch["fields"] as $textitem){ 
     if ($textitem->__attributes["field_id"] == $fieldId){ 
      return $textitems->__attributes["values"][0]["value"]; 

     } 
    } 
} 

在你的情況下使用會

echo getFirstValueByFieldId("37325091", $Arraytosearch); 

基本上它走的元素在字段數組,並返回values數組中第一個關聯值中的值,其中field_id等於函數的參數。

+1

您真的可以使用'$ textitem [「__ attributes」]'來訪問對象的屬性嗎?它不應該是'$ textitem-> field_id'嗎? – Barmar

+0

@Barmar把它放在一邊......好在感謝 – Orangepill

+0

應該也可以把這個數組放入功能的範圍.. – Orangepill

2

podio-php庫有內置的方法來處理所有這些。沒有必要自己去混淆__attributes屬性。

您可以在https://github.com/podio/podio-php/blob/master/examples/items.php

看到一堆例子在你的情況:

// Get item collection 
$itemCollection = PodioItem::filter(APP_ID, $filterParams); 
$fieldID = 37325091; 

foreach ($itemCollection['items'] as $item) { 
    // Get the field from the item 
    $field = $item->field($fieldID); 
    // Now you can print the value of that field 
    print $field->humanized_value; 
    // Or if you don't want to have the content sanitized: 
    print $field->values[0]['value']; 
} 
相關問題