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

时间:2017-10-30 04:38:44

标签: php arrays json pygooglechart

我从json字符串格式中绘制了一个googlechart。我需要将第二个数组值附加到第一个数组值。

这是json格式的查询。

    ///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);

我使用数组推送来组合数组,但googlechart不接受json格式,因为第二个值作为数组附加。

我的输出是

{"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}]}]]}

注意:未定义的值是一个新数组。

我希望它像这样,以便googlechart可以读取json格式:

{"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}]}]}

如何将$ rows2的值附加到$ rows?

1 个答案:

答案 0 :(得分:1)

您正在推送数组中的对象,因此需要额外的方括号。请尝试以下方法:

$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]
    ]);

希望这有帮助。