2

The following array is a print_r of an array called $result. I would like to know the best way to change the array to a set of variables that I can accesss any of these items as a variable for example if I want to see [queued_at] value I can just use a variable called $queued_at.

Array ( [response] => Array ( [success] => 1 [result] => Array ( [success] => Array ( [0] => Array ( [id] => 16623749 [device_id] => 22233 [message] => hi gg 2 [status] => pending [send_at] => 1463693469 [queued_at] => 0 [sent_at] => 0 [delivered_at] => 0 [expires_at] => 1463697069 [canceled_at] => 0 [failed_at] => 0 [received_at] => 0 [error] => [created_at] => 1463693469 [contact] => Array ( [id] => 3801452 [name] => Gg [number] => +64210419805 ) ) ) [fails] => Array ( ) ) ) [status] => 200 )

Here is a formatted version of array:

Array
(
    [response] => Array
        (
            [success] => 1
            [result] => Array
                (
                    [success] => Array
                        (
                            [0] => Array
                                (
                                    [id] => 16627521
                                    [device_id] => 22269
                                    [message] => test 10:37
                                    [status] => pending
                                    [send_at] => 1463700123
                                    [queued_at] => 0
                                    [sent_at] => 0
                                    [delivered_at] => 0
                                    [expires_at] => 1463703723
                                    [canceled_at] => 0
                                    [failed_at] => 0
                                    [received_at] => 0
                                    [error] => 
                                    [created_at] => 1463700123
                                    [contact] => Array
                                        (
                                            [id] => 3801855
                                            [name] => +64212465478
                                            [number] => +64212465478
                                        )

                                )

                        )

                    [fails] => Array
                        (
                        )

                )

        )

    [status] => 200
)
3
  • 1
    Please format your array. This is difficult to read. Commented May 19, 2016 at 23:14
  • 3
    Of course, the real question is "why?" Commented May 19, 2016 at 23:17
  • here is a formatted version of array: Commented May 19, 2016 at 23:22

3 Answers 3

4

You can use extract() to do that, eg. extract($array, EXTR_OVERWRITE), but you need to note a couple of things:

  1. If there are two items with the same key - one has to be overwritten or skipped. That's a limitation of your own task.

  2. I see that you have nested arrays, extract() may be not smart enough to work those, but you can try a nested_extract() method proposed here: http://php.net/manual/en/function.extract.php (see the comments section)

  3. I wouldn't recommend doing it anyway. If you just need an easy way to access data inside of your array - just write a smart finder function that looks for a specific key in nested arrays.

Alternative solution for you - instead of creating extra, temporary variables, simply access data inside your array based on key name:

// This function will convert multi-dimensional array to a single dimensional one
// If two items have the same keys - the former will be overwritten
function flatten(array $array)
{
    $return = array();
    array_walk_recursive($array, function($a, $key) use (&$return) { $return[$key] = $a; });
    return $return;
}

// This is a wrapper for the whole feature - flatten array and look for a key
function nested_key_search(array $array, $key)
{
    $flat = flatten($array);
    return $flat[$key] ?: false;
}

print_r(nested_key_search($array, 'coffee'));
Sign up to request clarification or add additional context in comments.

3 Comments

I know this is probably a simple question - so apologies in advance. What is the best way to just simply get the value for [device_id] in the array - I am just a bit confused due to the nesting levels.
$array['response']['result']['success'][0]['device_id']
@dkmarsh sure, see the new second part of the answer
1

You can use the extract function. This will dump all your array keys into the current variable pool. There are several flags available to determine what to do in the case of collisions. For example:

extract($result, EXTR_PREFIX_ALL, "r_");

will give you access to all of your array keys as variables prefixed with "r_".

2 Comments

thanks d amos - can I please clarify this so if I wanted to get device_id would I do the following ? extract($result, EXTR_PREFIX_ALL, "r_"); echo $r_device_id;
No, that wouldn't work actually. The key is too deep in the array. I think most of us misunderstood your original question, or at least didn't realize what it was exactly you had in mind. The second part of @Denis Mysenko is potentially a better answer for you.
0

I think that by using variable variable within ordinary foreach loop will give You what You want.

Example :

$array = $result['response']['result']['success'][0];

foreach($array as $varname => $value)
{
  if(is_string($value)) $$varname = $value;
}

Comments

Your Answer

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