我已经在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
现在保存转换后的对象