我有一个包含方法的类。该方法从数据库表中查询所有内容,并以json格式返回结果。
然后我就这样调用这个方法:
<body>
<template id="app" is="dom-bind">
<my-navigation selectedLabel="Home" id="my_navigation"></my-navigation>
<my-scaffold toolbartitle="{{ selectedLabel }}" id="my_scaffold"></my-scaffold>
</template>
<script>
var app = document.querySelector('#app');
app.addEventListener('template-bound', function () {
console.log('Our app is ready to rock!');
});
window.addEventListener('WebComponentsReady', function () {
document.querySelector('body').removeAttribute('unresolved');
var my-navigation = document.querySelector('my-navigation');
// This will add the variable to the 'app' context (template)
app.selectedLabel = my-navigation.selectedLabel;
});
</script>
</body>
返回类似于:
的内容[{&#34; CON_ID&#34;:&#34; 1&#34;&#34; con_firstname&#34;:&#34;保罗&#34;&#34; con_lastname&#34 ;: &#34;布罗格斯&#34;&#34; con_username&#34;:&#34; paul@email.com",&#34; con_password&#34;:&#34;密码&#34;,& #34; con_job_category_id&#34;:&#34; 0&#34;,&#34; con_updated_at&#34;:&#34; 2015-07-08 00:00:00&#34;,&#34; con_created_at&# 34;:&#34; 2015-07-08 00:00:00&#34;},{&#34; con_id&#34;:&#34; 2&#34;,&#34; con_firstname&#34;: &#34;阿伦&#34;&#34; con_lastname&#34;:&#34;布罗格斯&#34;&#34; con_username&#34;:&#34; paul@email.com",& #34; con_password&#34;:&#34;密码&#34;&#34; con_job_category_id&#34;:&#34; 0&#34;&#34; con_updated_at&#34;:&#34; 2015 - 07-08 00:00:00&#34;,&#34; con_created_at&#34;:&#34; 2015-07-08 00:00:00&#34;},{&#34; con_id&#34;: &#34; 3&#34;,&#34; con_firstname&#34;:&#34; Ryan&#34;,&#34; con_lastname&#34;:&#34; Bloggs&#34;,&#34; con_username&#34;:&#34; ryan@email.com",&#34; con_password&#34;:&#34;密码&#34;&#34; con_job_category_id&#34;:&#34 0 #34;&#34; con_updated_at&#34;:&#34; 2015年7月8日00:00:00&#34;,&#34; con_created_at&#34;:&#34; 2015-07-08 00:00:00&#34;}]
我现在需要某种循环来完成此操作,因为我只需要$consultants = new Consultants();
$consultants->getUsersJson();
,con_firstname
和con_lastname
。它还需要看起来与此类似:
con_username
(所以它全部包含在&#34;数据&#34;。这只是一个,循环应该显示所有这些。)
答案 0 :(得分:0)
尝试一下,它可能会失败硬核:
$consultants = new Consultants();
$users = $consultants->getUsersJson();
json_decode($users);
$users = object_to_array($users);
$data = array(
'data'=>array()
);
foreach ($users as $user){
$data['data']['con_firstname'] = $user['con_firstname']
}
function object_to_array($data)
{
if (is_object($data)) {
$data = get_object_vars($data);
}
if (is_array($data)) {
return array_map(__FUNCTION__, $data);
}
else {
return $data;
}
}
json_encode($data);
答案 1 :(得分:0)
好吧,只是为了弥补Kisaragi的答案,你可以使用'json_decode($ users,true)'来直接获得一个数组