angularjs - php stdClass/object build -
i want build object (for angular) so
user:{ {education: {high school:'some school'}, {faculty:'faculty of science'} }, {skills: {skill1:'sk1'}, {skill2:'sk2'} } }
and php , mysql query:
$testarr = array(); $user = new stdclass(); $params = db::select( db::raw("select param.*, sys_param_values.*,param_value.*,type_user.*, param.name paramname, doc_param.name docparamname param left join doc_param on param.doc_param_id = doc_param.id left join sys_param_values on param.id = sys_param_values.param_id left join param_value on sys_param_values.value_ref = param_value.id left join type_user on sys_param_values.ref_user_id = type_user.id"));
query result:
"params":[{"id":21,"name":"faculty","type_id":5,"doc_param_id":14,"created_at":"2015-05-17 14:13:12","updated_at":"2015-06-04 08:19:43","doc_type":12,"ref_user_id":21,"param_id":48,"iteration":null,"value_short":null,"value_long":null,"value_ref":74,"value":"some high","type":"tech-admin","email":"xxxxxxx8@gmail.com","password":"$2y$10$6l8voj3dgzuadhzlabh4jei\/u.svvdcn4b02xfc9mf\/p8m5rpfjtg","password_new":null,"first_name":"jon","last_name":"snow","street_1":"shiv","street_2":"tey","city":"123456","state":"aa","zipcode":"47252","country":"usa","phone_1":"123456","phone_2":"123456","mobile":"123456789","date_of_birth":"2015-05-18 11:25:42","registration":"0000-00-00 00:00:00","last_login":"2015-05-20 09:14:52","send_newsletters":1,"send_notifications":1,"remember_token":"dtlimlnzbwdcxcqkr7nddblmiafkzoxywn4jjuac53v7ni4e1t6eokjxdsoy","paramname":"faculty","docparamname":"education"}
after query witch good, loop :
foreach($params $k=>$v) { $paramname = $v->paramname; $value = $v->value; $testarr[$v->docparamname] = array(); $testarr[$v->docparamname][$paramname] = $v->value; }
and result :
"test":{ "education": {"high_school":"some high"}, "skills": {"skill_1":"skill_1 value"}, "experience": {"xp_1":"xp1 value"} }
the thing have more if these parameters example "skill_2" , "skill_3" suppose go in "skills" object. loop anly gets last parameter..
where suppose specify key or of skills go skills object , on..
you need define this:-
foreach($params $k=>$v) { $paramname = $v->paramname; $value = $v->value; // $testarr[$v->docparamname] = array(); // comment line $testarr[$v->docparamname][$paramname] = $v->value; }
note:- line commented because defined $testarr
array before foreach
loop.thanks
Comments
Post a Comment