Home > Net >  How can I parse fields from a PHP array like this?
How can I parse fields from a PHP array like this?

Time:11-03

I have an array that I need to work with and I can't find anywhere on the internet to explain how I can grab a field and put it in an echo statement in PHP. Here is the array. I'd like to grab "subtitle".

Array
(
    [response] => Array
        (
            [dataInfo] => Array
                (
                    [database] => xxDBxx
                    [layout] => xxLayxx
                    [table] => xxTablexx
                    [totalRecordCount] => 60
                    [foundCount] => 1
                    [returnedCount] => 1
                )

            [data] => Array
                (
                    [0] => Array
                        (
                            [fieldData] => Array
                                (
                                    [title] => Media Links
                                    [subtitle] => Previous NewCo Campaign Media
                                    [permalink] => Media-Links
                                    [reads] => 5233
                                )

                            [portalData] => Array
                                (
                                )

                            [recordId] => 62
                            [modId] => 5333
                        )

                )

        )

    [messages] => Array
        (
            [0] => Array
                (
                    [code] => 0
                    [message] => OK
                )

        )

)

I was thinking that if my results came back as an array stored in $result, then I could pull the field from there, but I can't find what I need. Please help.

CodePudding user response:

When you post question, you have to post your array, not the result from dump, so your array loooks like this:

<?php

$result = [

    "response" => [
        "dataInfo" =>
        [
            "database" => "xxDBxx",
            "layout" => "xxLayxx",
            "table" => "xxTablexx",
            "totalRecordCount" => "60",
            "foundCount" => 1,
            "returnedCount" => 1
        ],

        "data" =>
        [
            "0" =>
            [
                "fieldData" =>
                [
                    "title" => "Media Links",
                    "subtitle" => "Previous NewCo Campaign Media",
                    "permalink" => "Media-Links",
                    "reads" => "5233"
                ],

                "portalData" =>
                [],

                "recordId" => 62,
                "modId" => 5333
            ]

        ]

    ],

    "messages" =>
    [
        "0" =>
        [
            "code" => 0,
            "message" => "OK"
        ]
    ]
];

And you can grab the subtitle, like this:

echo $result["response"]["data"][0]["fieldData"]["subtitle"];
  • Related