如何通过PHP中的键读取多维数组

时间:2018-08-05 15:32:01

标签: php arrays json multidimensional-array

这是我对服务器的要求

Array ( [id] => 123 [status] => pending [recipient] => Array ( [account_id] => 5000 [gateway_id] => 51111 ) [amount] => Array ( [value] => 2 [currency] => RUB ) [description] => order test 1 [payment_method] => Array ( [type] => bank_card [id] => 123 [saved] => ) [created_at] => 2018-08-05T14:44:59+00:00 [confirmation] => Array ( [enforce] => [confirmation_url] => https://money.com/1234566 [type] => redirect ) [paid] => ) 

我如何获取 confirmation_url 字符串?

tnx

2 个答案:

答案 0 :(得分:0)

您可以像

一样访问
$array['confirmation']['confirmation_url']

答案 1 :(得分:0)

首先,您需要将其另存为变量。因此,假设该数组来自名为“ validateTransaction()”的CURL函数;

const functions = require('firebase-functions');
const admin = require('firebase-admin');
admin.initializeApp();

exports.sendNotification = functions.database.ref('/Notifications/{receiver_id}/{notification_id}').onWrite((change, context) => {

const user_id = context.params.user_id;
const notification_id = context.params.notification_id;

console.log('We have a notification : ', user_id);

const afterData = change.after.val();

if (!afterData){
    return null;
}

const fromUser = admin.database().ref(`/Notifications/${user_id}/${notification_id}`).once('value');
return fromUser.then(fromUserResult => {

    const from_user_id = fromUserResult.val().from;


    console.log('You have a new notification from: ', from_user_id);

    const deviceToken = admin.database().ref(`/Users/${user_id}/device_token`).once('value');

    return deviceToken.then(result => {

        const token_id = result.val();

        const payload = {
            notification: {
                title: "New Friend Request",
                body: "You've received a new Friend Request",
                icon: "default"
            }
        };

        return admin.messaging().sendToDevice(token_id, payload).then(response => {
            console.log('This was the notification feature');
        });

    });

});

});

作为专家提示-我更喜欢使用'json_encode($ result);'将数组以可读的格式放置,然后将输入内容放入一个名为http://jsonselector.com的精彩站点,您可以在其中选择值并查看适当的“选择器”以从数组中获取值。

当然,这并没有真正告诉我们如何您正在获得数据响应。

如果将其格式化为XML并使用SimpleXmlElement进行解析,则可以执行json_decode(json_encode($ result),true);将XML元素转换为“适当”的数组。