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 Python爬取指定微博话题下的内容,保存为txt
  • ¥15 vue2登录调用后端接口如何实现
  • ¥65 永磁型步进电机PID算法
  • ¥15 sqlite 附加(attach database)加密数据库时,返回26是什么原因呢?
  • ¥88 找成都本地经验丰富懂小程序开发的技术大咖
  • ¥15 如何处理复杂数据表格的除法运算
  • ¥15 如何用stc8h1k08的片子做485数据透传的功能?(关键词-串口)
  • ¥15 有兄弟姐妹会用word插图功能制作类似citespace的图片吗?
  • ¥15 latex怎么处理论文引理引用参考文献
  • ¥15 请教:如何用postman调用本地虚拟机区块链接上的合约?