我有以下数组:
Array
(
[utype_id] => 1
[username] => admin
[policy] => Array
(
[0] => stdClass Object
(
[totalCount] => 7
[status] => 1
)
[1] => stdClass Object
(
[totalCount] => 1
[status] => 2
)
[2] => stdClass Object
(
[totalCount] => 5
[status] => 3
)
)
)
我从 $ returnDtls [" policy"] 获取上述数组。现在我想用下面的代码转换这个数组:
Array
(
[utype_id] => 1
[username] => admin
[policy] => Array
(
[1] => 7
[2] => 1
[3] => 5
)
)
我遇到了这个问题。我用以下代码尝试了这个: -
$arr = array();
foreach($returnDtls["policy"] as $key=>$val)
{
$arr[$val["status"]] = $val["totalCount"];
}
$data = array_merge($data,$arr);
但没有成功。它给了我以下错误: -
致命错误:无法在第53行的D:\ xampp \ htdocs \ tristar \ application \ modules \ manage \ controllers \ dashboard.php中使用stdClass类型的对象
有人请帮助吗...
答案 0 :(得分:1)
试试这段代码:
$result = array();
foreach ($array['policy'] as $object) {
$result[$object->status] = $object->totalCount;
}
$array['policy'] = $result;
检查codepad.org;
上的结果答案 1 :(得分:0)
尝试这样的事情
for ($i = 0; $i < sizeof($arr['policy']); $i++){
$arr['policy'][$i+1] = $arr['policy'][$i]['totalCount'];
}
答案 2 :(得分:0)
经过长时间的尝试,我能够自己做到这一点。所以我自己回答我的问题。请查看以下内容: -
$arr = array();
foreach($returnDtls["policy"] as $key=>$val)
{
$arr["policy"][$val->status] = $val->totalCount;
}
$data = array_merge($data,$arr);