【问题标题】:Convert array with subarrays to stdClass将带有子数组的数组转换为 stdClass
【发布时间】:2014-07-26 08:49:59
【问题描述】:

我有一个带有子数组的数组:

$test = array("hello" => "world", "object" => array("bye" => "world"));

我想把它转换成对象:

$obj = (object) $test;

父数组变成对象,子数组还是数组:

object(stdClass)[1]
  public 'hello' => string 'world' (length=5)
  public 'object' => 
    array (size=1)
      'bye' => string 'world' (length=5)

但我想得到这样的东西:

object(stdClass)[1]
  public 'hello' => string 'world' (length=5)
  public 'object' => 
    object(stdClass)[2]
      public 'bye' => string 'world' (length=5)

可以通过以下代码实现:

$testObj = json_decode(json_encode($test));

但这是不好的做法。我怎样才能达到这个结果?

【问题讨论】:

    标签: php arrays stdclass


    【解决方案1】:

    试试这可能会有所帮助。

    how to convert multidimensional array to object in php?

    function convert_array_to_obj_recursive($a) {
    if (is_array($a) ) {
        foreach($a as $k => $v) {
            if (is_integer($k)) {
                // only need this if you want to keep the array indexes separate
                // from the object notation: eg. $o->{1}
                $a['index'][$k] = convert_array_to_obj_recursive($v);
            }
            else {
                $a[$k] = convert_array_to_obj_recursive($v);
            }
        }
    
        return (object) $a;
    }
    
    // else maintain the type of $a
    return $a; 
    }
    

    让我知道它是否有效。

    【讨论】:

      【解决方案2】:

      您可以通过 foreach 和条件使用这种方式:

      $array = array("hello" => "world", "object" => array("bye" => "world"));
      
      foreach($array as $key => $val) {
      
          if(is_array($val)) {
              $aa[$key] = (object) $val; 
          }
          else {
              $aa[$key] = $val;
          }
      
          $obj  = (object) $aa;
      }
      
      echo "<pre>";
      var_dump($obj);
      echo "</pre>";
      

      【讨论】:

        【解决方案3】:

        试试这个:

        function cast($array) {
            if (!is_array($array)) return $array;
            foreach ($array as &$v) {
                $v = cast($v);
            }
            return (object) $array;
        }
        $result = cast($test);
        

        Demo

        【讨论】:

          【解决方案4】:
          function arrayToObject($arr) {
              if (is_array($arr)) 
                  return (object) array_map(__FUNCTION__, $arr);
          
              else
                  return $arr;
          }
          

          arrayToObject($test) 的输出将是,

          stdClass Object
          (
              [hello] => world
              [object] => stdClass Object
                  (
                      [bye] => world
                  )
          
          )
          

          【讨论】:

            【解决方案5】:

            我想你正在寻找这个

            $object = new stdClass();
            foreach ($array as $key => $value)
            {
                $object->$key = $value;
            }
            

            并使用内置 json $object = json_decode(json_encode($array), FALSE);..它将所有子数组转换为对象..

            如果这不是您期望的答案,请在下方评论

            【讨论】:

              猜你喜欢
              • 2020-03-20
              • 2016-12-25
              • 1970-01-01
              • 2012-05-15
              • 2011-11-08
              • 2013-11-21
              • 1970-01-01
              • 2023-03-09
              • 2018-08-09
              相关资源
              最近更新 更多