Convert a PHP object to an associative array

后端 未结 30 2292
走了就别回头了
走了就别回头了 2020-11-22 02:18

I\'m integrating an API to my website which works with data stored in objects while my code is written using arrays.

I\'d like a quick-and-dirty function to convert

30条回答
  •  佛祖请我去吃肉
    2020-11-22 02:50

    Since a lot of people find this question because of having trouble with dynamically access attributes of an object, I will just point out that you can do this in PHP: $valueRow->{"valueName"}

    In context (removed HTML output for readability):

    $valueRows = json_decode("{...}"); // Rows of unordered values decoded from a JSON object
    
    foreach ($valueRows as $valueRow) {
    
        foreach ($references as $reference) {
    
            if (isset($valueRow->{$reference->valueName})) {
                $tableHtml .= $valueRow->{$reference->valueName};
            }
            else {
                $tableHtml .= " ";
            }
        }
    }
    

提交回复
热议问题