【问题标题】:PHP Simple way to obtain value from multi level objectPHP 从多级对象中获取值的简单方法
【发布时间】:2023-03-22 04:33:02
【问题描述】:

我知道我可以遍历对象的每个级别,但我想要一个更简单的方法。

QueryResult Object
(
    [queryLocator] => 
    [done] => 1
    [records] => Array
        (
            [0] => SObject Object
                (
                    [type] => type_1
                    [fields] => 
                    [sobjects] => Array
                        (
                            [0] => SObject Object
                                (
                                    [type] => type_2
                                    [fields] => 
                                    [sobjects] => Array
                                        (
                                            [0] => SObject Object
                                                (
                                                    [type] => type_3
                                                    [fields] => 
                                                    [sobjects] => Array
                                                        (
                                                            [0] => SObject Object
                                                                (
                                                                    [type] => type_4
                                                                    [fields] => 
                                                                    [sobjects] => Array
                                                                        (
                                                                            [0] => SObject Object
                                                                                (
                                                                                    [type] => type_5
                                                                                    [fields] => 
                                                                                    [Id] => 12345_I_need_this
                                                                                )

                                                                        )

                                                                )

                                                        )

                                                )

                                        )

                                )

                        )

                )

        )

    [size] => 1
)

我需要这个 type_5 的 Id 值,我怎么能在一个简单的解决方案中得到它。

需要考虑的其他几点:

  • 我不知道数组的对象会有多大或多深,可能大于或小于 5

我听说过递归,但没有找到任何我认为可以使用的东西来保持它的简单性。也许一些更好的教程会帮助我。 如果我确实知道我需要的值在对象数组的哪个部分,我可以直接调用它吗?类似于:$object[5]->id ???

【问题讨论】:

    标签: php recursion object multidimensional-array salesforce


    【解决方案1】:

    很简单:

    class SObject{
    /*...*/
    
        public getId(){
            if(isset($this->Id)){
                return $this->Id;
            } else {
                return $this->sobjects[0]->getId();
            }
        }
    }
    

    然后你打电话

    $id = $query_obj->getId();
    

    【讨论】:

    • SObject 已经是一个类。我正在使用它,但是如果简单的话,我该怎么做: $queryResults->records[0]->sobjects[0]->sobjects[0]->sobjects[0]->sobjects[0]->Id
    • @Phill:您必须使用上述函数 getId 扩展使用的类。你能修改 sobject 类吗?
    • @Phill: 你会调用 $queryResults->records[0]->getId();
    【解决方案2】:

    如果您需要对此结构进行大量查询,请将其转储为 XML 并在其上使用 XPATH

    【讨论】:

      【解决方案3】:

      以下是递归的工作原理(通常)

      function recursiveFunctionName( input ) // returns value;
      {
          //Do something to input to make it new_input
      
          if( isSomethingAccomplished )
          {
              return value;
          }
          else
          {
              return recursiveFunctionName( new_input );
          }
      }
      

      您从输入开始,然后告诉函数继续调用自身,直到它可以返回有效的输出。在你的情况下,你可以这样做:

      function getID( SObject $so )
      {
          // equates to isSomethingAccomplished...  You have found the value
          // you want returned, so pass that out.
          if( $so->id )
          {
              return $so->id;
          }
          else
          {
              // otherwise, this will return the value from the next level, 
              // pass that out.
              # SEE BELOW FOR ONE MORE NOTE HERE.
              return getID( $so->sobjects[ 0 ] );
          }
      }
      

      现在,由于您对 sobjects 使用数组,您可能希望将 #SEE BELOW 下面的行替换为以下内容:

      $objs  = $so->sobjects;
      $count = count( $objs );
      
      // Iterate through all of its children, testing each of the child nodes.
      // (You're actually using iteration and recursion in combination here).
      for( $i = 0; $i < $count; $i++ )
      {
          $curr = getID( $objs[ $i ] );
      
          // This is the same test as above.
          if( $curr )
          {
              return $curr;
          }
      }
      

      【讨论】:

        猜你喜欢
        • 2013-12-23
        • 2019-06-06
        • 1970-01-01
        • 2020-03-06
        • 2010-11-29
        • 1970-01-01
        • 2017-01-28
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多