Home > OS >  PHP Multidimensional Array - Get value of element in last index?
PHP Multidimensional Array - Get value of element in last index?

Time:11-16

Not sure if I've worded that correctly however, the below could illustrate what I'm trying to achieve better..

I have a multidimensional array where I would like to capture a specific element from the last index within an array foreach

Array:

    [Something] => Array
(
    [Something1] => Array
    (
        [0] => Array
        (
            [@attributes] => Array
                (
                    [DataID] => Data
                    [DataID] => Data
                    [DataID] => Data
                    [DataID] => Data
                )

            [Something1.1] => Array
            (
                [Something1.1.1] => Array
                (
                    [0] => Array
                    (
                        [DataID] => Data
                        [Date] => YYYY-MM-DD
                        [DataID] => Data
                        [DataID] => Data
                        [DataID] => Data
                        [DataID] => Data
                    )
                    [1] => Array
                    (
                        [DataID] => Data
                        [Date] => YYYY-MM-DD
                        [DataID] => Data
                        [DataID] => Data
                        [DataID] => Data
                        [DataID] => Data

                    )
                    [2] => Array (last returned)
                    (
                        [DataID] => Data
                        [DateLASTRETURNED] => YYYY-MM-DD
                        [DataID] => Data
                        [DataID] => Data
                        [DataID] => Data
                        [DataID] => Data

                    )
                )
            )
            [Something1.2] => Array
            (
                [Something.1.2.1] => Array
                    (
                        [0] => Array
                    (
                        [DataID] => Data
                        [Date] => YYYY-MM-DD
                        [DataID] => Data
                        [DataID] => Data
                        [DataID] => Data
                        [DataID] => Data
                    )
                    [1] => Array
                    (
                        [DataID] => Data
                        [Date] => YYYY-MM-DD
                        [DataID] => Data
                        [DataID] => Data
                        [DataID] => Data
                        [DataID] => Data

                    )
                    [2] => Array (last returned)
                    (
                        [DataID] => Data
                        [DateLASTRETURNED] => YYYY-MM-DD
                        [DataID] => Data
                        [DataID] => Data
                        [DataID] => Data
                        [DataID] => Data
                    )
                )
            )
        )
    )
)

As you can see in brackets (last returned), I need the value within a specified element from the last returned in foreach "Something1.*) - Specifically, This field holds Date YYYY-MM-DD and would like to store into $ to reuse elsewhere.

Hopefully that makes sense, look forward to some help!

CodePudding user response:

If you want to get the latest element from each element inside "Something1.*". You can try something like this:

$array = [
    'Something' => [
        'Something1' => [
            0 => [
                "@attributes" => array(
                    'DataID' => "Data",
                    "DataID" => "Data",
                    "DataID" => "Data",
                ),
                "Something1.1" => array(
                    "Something1.1.1" => array(
                        0 => array(
                            "DataID" => "data",
                            "Date" => "YYYY-MM-DD",
                            "DataID" => "data",
                            "DataID" => "data",
                        ),
                        1 => array(
                            "DataID" => "data",
                            "Date" => "YYYY-MM-DD",
                            "DataID" => "data",
                            "DataID" => "data",
                        ),
                        2 => array(
                            "DataID" => "data",
                            "Date" => "YYYY-MM-DD last",
                            "DataID" => "data",
                            "DataID" => "data",
                        ),
                    ),
                ),
                "Something1.2" => array(
                    "Something1.2.1" => array(
                        0 => array(
                            "DataID" => "data",
                            "Date" => "YYYY-MM-DD",
                            "DataID" => "data",
                            "DataID" => "data",
                        ),
                        1 => array(
                            "DataID" => "data",
                            "Date" => "YYYY-MM-DD",
                            "DataID" => "data",
                            "DataID" => "data",
                        ),
                        2 => array(
                            "DataID" => "data",
                            "Date" => "YYYY-MM-DD last",
                            "DataID" => "data",
                            "DataID" => "data",
                        ),
                    ),
                ),
            ],
        ],
    ],
];

$something1 = $array['Something']['Something1']; 

foreach ($something1 as $something_1) {                 // loop through 'Something1'
    foreach ($something_1 as $key => $something_1_1) {  // loop through each element of 'Something1'.
        if (str_starts_with($key, 'Something1.')) {     // if key starts with 'Something1.' PHP >= 8.0

            foreach ($something_1_1 as $key => $something_1_1_1) {
                $last_element = end($something_1_1_1);  // get last element of array
                echo $last_element['Date'];             // OUTPUT: YYYY-MM-DD last
            }
        }
    }
}

Please note these functions:
str_starts_with() is availabel since PHP8.0.
end() Set the internal pointer of an array to its last element.

CodePudding user response:

You can do something like this. First accessing until the desired level, then pick the last array of each, check if it is an array and then print the value "Date".

Assuming that the multi-array in the example is stored in a variable named $array:

foreach($array["something"]["Something1"] as $value) {
    foreach($value as $value1){
        if(is_array(end($value1))) {
            echo(end($value1)["Date"]);
        }
    }
}

In case your multi-array deviates from the example you have indicated maybe you should adding some extra validation.

  • Related