PHP中用于JSON的第三级数组

时间:2019-06-25 09:54:14

标签: php json

我正在尝试将属性发送到具有三个属性之一的第三级深度数组的Open Athens API。 我有一个名为request_json的数组,所有元素都插入其中。这些元素之一是属性:

#Getting base image
#FROM node:10.16.0-jessie-slim
FROM node:alpine
#Maitainer
run apk add --no-cache git
MAINTAINER Rajath
#run apt-get -y update && apt-get install -y git-core
RUN git clone https://github.com/IBM-Bluemix/bluemix-hello-node /app
WORKDIR /app
RUN npm install 
EXPOSE 3000
CMD ["npm", "start"]

我需要做的是将Department / Speciality和Department2 / Speciality2作为具有两个值的一个属性传递,但是我在定义新数组时遇到问题。

这是我到目前为止尝试过的:

$request_json["attributes"] = array( //Add attributes that we send to Open Athens
    "Userid" => str_replace(" ","",$user_uid),
    "Fullname" => $jsonRestData->RECORD[0]->FULLNAME,
    "Email" => $jsonRestData->RECORD[0]->STUDENT_EMAIL,
    "Role" => $jsonRestData->RECORD[0]->LOGIN_STATUS_TEXT,
    "Year" => $jsonRestData->RECORD[0]->STUDENT_ACADEMIC_YEAR,
    "Department" => $jsonRestData->RECORD[0]->STUDENT_DEPARTMENT,
    "Speciality" => $jsonRestData->RECORD[0]->STUDENT_SPECIALITY,
    "Department2" => $jsonRestData->RECORD[1]->STUDENT_DEPARTMENT,
    "Speciality2" => $jsonRestData->RECORD[1]->STUDENT_SPECIALITY,
    "permissionSets" => "qsm#default"
);  

也:

$request_json["attributes"] = array( //Add attributes that we send to Open Athens
    "Userid" => str_replace(" ","",$user_uid),
    "Fullname" => $jsonRestData->RECORD[0]->FULLNAME,
    "Email" => $jsonRestData->RECORD[0]->STUDENT_EMAIL,
    "Role" => $jsonRestData->RECORD[0]->LOGIN_STATUS_TEXT,
    "Year" => $jsonRestData->RECORD[0]->STUDENT_ACADEMIC_YEAR,
    "Department"[] => ($jsonRestData->RECORD[0]->STUDENT_DEPARTMENT, $jsonRestData->RECORD[1]->STUDENT_DEPARTMENT),
    "Speciality"[] => ($jsonRestData->RECORD[0]->STUDENT_SPECIALITY, $jsonRestData->RECORD[1]->STUDENT_SPECIALITY)
    "permissionSets" => "qsm#default"
);  

并且:

$request_json["attributes"] = array( //Add attributes that we send to Open Athens
    "Userid" => str_replace(" ","",$user_uid),
    "Fullname" => $jsonRestData->RECORD[0]->FULLNAME,
    "Email" => $jsonRestData->RECORD[0]->STUDENT_EMAIL,
    "Role" => $jsonRestData->RECORD[0]->LOGIN_STATUS_TEXT,
    "Year" => $jsonRestData->RECORD[0]->STUDENT_ACADEMIC_YEAR,
    "Department" => array ($jsonRestData->RECORD[0]->STUDENT_DEPARTMENT, $jsonRestData->RECORD[1]->STUDENT_DEPARTMENT),
    "Speciality" => array ($jsonRestData->RECORD[0]->STUDENT_SPECIALITY, $jsonRestData->RECORD[1]->STUDENT_SPECIALITY),
    "permissionSets" => "qsm#default"
);  

并且:

$request_json["attributes"] = array( //Add attributes that we send to Open Athens
    "Userid" => str_replace(" ","",$user_uid),
    "Fullname" => $jsonRestData->RECORD[0]->FULLNAME,
    "Email" => $jsonRestData->RECORD[0]->STUDENT_EMAIL,
    "Role" => $jsonRestData->RECORD[0]->LOGIN_STATUS_TEXT,
    "Year" => $jsonRestData->RECORD[0]->STUDENT_ACADEMIC_YEAR,
    "Department" => ($jsonRestData->RECORD[0]->STUDENT_DEPARTMENT, $jsonRestData->RECORD[1]->STUDENT_DEPARTMENT),
    "Speciality" => ($jsonRestData->RECORD[0]->STUDENT_SPECIALITY, $jsonRestData->RECORD[1]->STUDENT_SPECIALITY),
    "permissionSets" => "qsm#default"
);  

如何为“部门”和“特殊性”定义几个值?

1 个答案:

答案 0 :(得分:-1)

好的,我找到了解决方案。 我需要将所有值包装在[]中,并将每个值包装在()中:

$request_json["attributes"] = array( //Add attributes that we send to Open Athens
        "Userid" => str_replace(" ","",$user_uid),
        "Fullname" => $jsonRestData->RECORD[0]->FULLNAME,
        "Email" => $jsonRestData->RECORD[0]->STUDENT_EMAIL,
        "Role" => $jsonRestData->RECORD[0]->LOGIN_STATUS_TEXT,
        "Year" => $jsonRestData->RECORD[0]->STUDENT_ACADEMIC_YEAR,
        "Department" => [($jsonRestData->RECORD[0]->STUDENT_DEPARTMENT), ($jsonRestData->RECORD[1]->STUDENT_DEPARTMENT)],
        "Speciality" => [($jsonRestData->RECORD[0]->STUDENT_SPECIALITY), ($jsonRestData->RECORD[1]->STUDENT_SPECIALITY)],
        "permissionSets" => "qsm#default"
    );