0

I am looking for a function to return an array and all its sub-structure for a specific criteria

Criteria:

  • php 5.6 compatible
  • Return the last instance of array with key name of !ENTITY with all its values in-tact

Sample array:

For the multidimensional array, lets call that $arr, for this example structure it's 6 levels deep, we should not assume it's always 6 levels.

$arr = array("!ENTITY" =>
             array("!ENTITY" =>
                array("!ENTITY" =>
                    array("!ENTITY" =>
                        array("!ENTITY" =>
                            array("!ENTITY" =>
                                array("svg" => 
                                     array(
                                         0 => array("g" => "", "@id" => "Layer_2"),
                                         1 => array("g" => "", "@id" => "Layer_3"),
                                     ),
                                     "@version" => 1.2,
                                     "@id" => "Layer_1",
                                ),
                            "@different" => "layer"
                            ),
                        "@all" => "layer"
                        ),
                    "@here" => "layer"
                    ),
                "@goes" => "layer"
                ),
            "@else" => "layer"
            ),
   "@something" => "layer"
   );

Expected Output:

I would like to return the final array for !ENTITY with it's sub-structure all in-tact. Here is a sample of the expected output:

Array
    (
        [svg] => Array
            (
                [0] => Array
                    (
                        [g] => 
                        [@id] => Layer_2
                    )

                [1] => Array
                    (
                        [g]
                        [@id] => Layer_3
                    )

            )

        [@version] => 1.2
        [@id] => Layer_1
    )

1 Answer 1

2

You will need to recursively traverse the array and return resultant values if found like below:

<?php

function getLastValueForKey($data, $key){
    $res = '';
    foreach($data as $k => $value){
        $sub_res = is_array($value) ? getLastValueForKey($value, $key) : '';
        if($sub_res !== ''){
            $res = $sub_res;
        }else if($k === $key){
          $res = $value;
        }
    }
    return $res;
}
   
print_r(getLastValueForKey($arr, '!ENTITY'));

Online Demo

Sign up to request clarification or add additional context in comments.

5 Comments

this looks great and i love how you set it up to work with other key names too.
@user3436467 Thanks. Generalizing it for any key is most often a good approach since it provides flexibility throughout the application for any other key
and its Fast , Good job
@flakerimi Thanks. Worst case scenario is still visiting all keys in the array since OP wishes to check for last instance of a particular key.
It does the job, and its clean. I like it.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.