dougao1542 2017-10-30 04:38
浏览 39
已采纳

如何将新数组插入现有数组?

I draw a googlechart from json string format. I need to append a second array values to the first array values.

This is the query of the json format.

    ///sql query list of alumni applicants by race
$sql = 'SELECT lir.race_id, count(lja.application_id) as count,
SUM(CASE WHEN lja.application_status = 0 THEN 1 ELSE 0 END) as pendingcount,
SUM(CASE WHEN lja.application_status = 1 THEN 1 ELSE 0 END) successcount,
SUM(CASE WHEN lja.application_status = 2 THEN 1 ELSE 0 END) rejectedcount, SUM(CASE WHEN lja.application_status = 9 THEN 1 ELSE 0 END) deletedcount,
lir.race_name FROM {local_info_race} lir INNER JOIN 
{local_info_userprofile} liu ON lir.race_id = liu.userprofile_raceid 
INNER JOIN {cohort_members} cm ON liu.userprofile_userid = cm.userid 
INNER JOIN {local_jobs_application} lja ON cm.userid = lja.application_applicantid 
INNER JOIN {local_jobs_job} ljj ON lja.application_jobid = ljj.job_id 
LEFT JOIN {local_jobs_level} ljl ON ljj.job_levelid = ljl.level_id 
LEFT JOIN {local_cohortrole} lc on cm.cohortid = lc.cohortid 
LEFT JOIN {role} r ON lc.roleid = r.id WHERE shortname = "graduates" 
GROUP BY lir.race_name';

//get the query into record
$data = $DB->get_records_sql($sql);

//put the query into array
$rows = array();

$rows = array_map(function($item) {
    return (object) ['c' => [
        (object) ['v' => $item->race_name, 'f' => null],
        (object) ['v' => intval($item->pendingcount), 'f' => null],
        (object) ['v' => intval($item->successcount), 'f' => null],
        (object) ['v' => intval($item->rejectedcount), 'f' => null]
    ]];
}, array_values($data));


 //sql query list of alumni applicants by category
$sql2 = 'SELECT liu.userprofile_userid, count(liu.userprofile_userid) as count, 
SUM(CASE WHEN lja.application_status = 0 THEN 1 ELSE 0 END) as pendingcount, 
SUM(CASE WHEN lja.application_status = 1 THEN 1 ELSE 0 END) successcount,
SUM(CASE WHEN lja.application_status = 2 THEN 1 ELSE 0 END) rejectedcount, SUM(CASE WHEN lja.application_status = 9 THEN 1 ELSE 0 END) deletedcount,
liu.userprofile_raceid, liu.userprofile_race FROM {local_info_userprofile} liu 
INNER JOIN {local_jobs_application} lja ON liu.userprofile_userid = lja.application_applicantid 
INNER JOIN {local_jobs_job} ljj ON lja.application_jobid = ljj.job_id 
LEFT JOIN {local_jobs_category} ljc ON ljj.job_categoryid = ljc.category_id 
LEFT JOIN {cohort_members} cm ON lja.application_applicantid = cm.userid 
LEFT JOIN {local_cohortrole} lc on cm.cohortid = lc.cohortid 
LEFT JOIN {role} r ON lc.roleid = r.id 
WHERE r.shortname = "graduates" AND liu.userprofile_raceid = 0';

  //get the query into record
$data2 = $DB->get_records_sql($sql2);


 //change the label name for undefine race
foreach($data2 as $k => $val) {
  if($data2[$k]->{'userprofile_raceid'} == 0) {
    $data2[$k]->{'userprofile_race'} = 'Undefined';
  } 
}

$rows2 = array();

foreach($data2 as $k => $i) {

          $rows2[]=(object) ['c' => [
            (object) ['v' => $i->userprofile_race, 'f' => null],
            (object) ['v' => intval($i->pendingcount), 'f' => null],
            (object) ['v' => intval($i->successcount), 'f' => null],
            (object) ['v' => intval($i->rejectedcount), 'f' => null]
        ]];

    }

array_push($rows, $rows2);

$cols = [
    (object) ['id' => '', 'label' => 'RACE', 'pattern' => '', 'type' => 'string'],
    (object) ['id' => '', 'label' => 'Pending', 'pattern' => '', 'type' => 'number'],
    (object) ['id' => '', 'label' => 'Success', 'pattern' => '', 'type' => 'number'],
    (object) ['id' => '', 'label' => 'Rejected', 'pattern' => '', 'type' => 'number']
];

$returndata = new stdClass;
$returndata->cols = $cols;
$returndata->rows = $rows;


echo json_encode($returndata);

I use array push to combine the array but the json format doesn't acceptable by the googlechart because the second value is attached as an array.

My output is

{"cols":[{"id":"","label":"RACE","pattern":"","type":"string"},
{"id":"","label":"Pending","pattern":"","type":"number"},
{"id":"","label":"Success","pattern":"","type":"number"},
{"id":"","label":"Rejected","pattern":"","type":"number"}],
"rows":[{"c":[{"v":"Malay","f":null},{"v":1,"f":null},{"v":1,"f":null},
{"v":1,"f":null}]},[{"c":[{"v":"Undefined","f":null},{"v":3,"f":null},
{"v":1,"f":null},{"v":1,"f":null}]}]]}

note: the undefined values is a new array.

I want it to be like this so that googlechart can read the json format:

{"cols":[{"id":"","label":"RACE","pattern":"","type":"string"},
{"id":"","label":"Pending","pattern":"","type":"number"},
{"id":"","label":"Success","pattern":"","type":"number"},
{"id":"","label":"Rejected","pattern":"","type":"number"}],
"rows":[{"c":[{"v":"Malay","f":null},{"v":1,"f":null},{"v":1,"f":null},
{"v":1,"f":null}]},{"c":[{"v":"Undefined","f":null},{"v":3,"f":null},
{"v":1,"f":null},{"v":1,"f":null}]}]}

How to append the values of $rows2 into $rows?

  • 写回答

1条回答 默认 最新

  • douling0053 2017-10-30 04:50
    关注

    you are pushing an object in the array hence the extra square bracket. Try the following:

    $rows2 = array('c' => [
            (object) ['v' => $i->userprofile_race, 'f' => null],
            (object) ['v' => intval($i->pendingcount), 'f' => null],
            (object) ['v' => intval($i->successcount), 'f' => null],
            (object) ['v' => intval($i->rejectedcount), 'f' => null]
        ]);
    

    Hope this helps.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 javaweb项目无法正常跳转
  • ¥15 VMBox虚拟机无法访问
  • ¥15 skd显示找不到头文件
  • ¥15 机器视觉中图片中长度与真实长度的关系
  • ¥15 fastreport table 怎么只让每页的最下面和最顶部有横线
  • ¥15 R语言卸载之后无法重装,显示电脑存在下载某些较大二进制文件行为,怎么办
  • ¥15 java 的protected权限 ,问题在注释里
  • ¥15 这个是哪里有问题啊?
  • ¥15 关于#vue.js#的问题:修改用户信息功能图片无法回显,数据库中只存了一张图片(相关搜索:字符串)
  • ¥15 texstudio的问题,