簡體   English   中英

將混合對象/數組轉換為JSON

[英]Converting mixed object/array to JSON

我已經在SO上搜索了有關此問題的答案,但是也許我沒有問正確的問題,因為我沒有找到答案(但我知道可能有很多例子)。

我有這個對象數組:

array(1) {
  [0]=>
  object(stdClass)#424 (3) {
    ["AC"]=>
    string(6) "Active"
    ["CL"]=>
    string(6) "Closed"
    ["OH"]=>
    string(7) "On Hold"
  }
}

我試圖重新格式化它,所以最終結果是這樣,因此可以將其插入到我的多選小部件數據源中:

[
    {
        "value": "AC",
        "text": "Active"
    },
    {
        "value": "OH",
        "text": "On Hold"
    },
    {
        "value": "CL",
        "text": "Closed"
    }
]

用這個:

$newObjectsArray = array();
foreach($objectsArray as $index => $object){
    foreach($object as $key => $value){
        $newObjectsArray[$index][] = array("value" => $key, "text" => $value);
    }
    $newObjectsArray[$index] = json_encode($newObjectsArray[$index]);
}

$newObjectsArray現在保存轉換后的對象

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM