PHP - 搜索键值,然后在同一个对象子阵列中查找另一个键的值

时间:2013-07-20 03:41:34

标签: php search object multidimensional-array podio

我为此搜索了很多,发现了几个类似的问题,但没有一个能解决我想要完成的事情。

我正在尝试编写一个代码来搜索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)

我想通过提供field_id =>的值来编写一个返回“REF100019”的函数。 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 ) ) 

注意:对于像我这样的新编程并希望Podio响应(或任何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);

2 个答案:

答案 0 :(得分: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'];
}

答案 1 :(得分: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);

基本上它遍历fields数组中的元素并返回values数组中第一个关联值的值,其中field_id等于函数的参数。

相关问题