0

Say I have the following PHP array:

$test = array(

    'bob' => array(
        'age' => '23',
        'region' => 'Yorkshire',
        'salary' => '£21,000'
    ),

    'sarah' => array(
        'age' => '42',
        'region' => 'Yorkshire',
        'salary' => '£60,000'
    ),

    'jim' => array(
        'age' => '28',
        'region' => 'Yorkshire',
        'salary' => '£35,000'
    )
)

Is it possible to pull a sub array from the multidimentional array using the array key as a reference? I can pull a single sub array using array_slice() however I believe it requires an integer for length and for offset. I was hoping for something like $new_array = array_slice('jim') where

$new_array = array(
    'age' => '28',
    'region' => 'Yorkshire',
    'salary' => '£22,000'
)

thanks.

2
  • 2
    You can't have 2 values in array with the same key 'jim' Commented Jul 20, 2011 at 8:21
  • Sorry, I know this just a poor bit of copy and paste on my behalf, I'll edit to avoid further confusion. Commented Jul 20, 2011 at 9:18

1 Answer 1

3

use:

$new_array = $test['jim'];

(assuming that $test is a valid array, meaning your keys in $test are unique, which they are not in your example)

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

1 Comment

Thankyou! I think I overcomplicated this one massively, am I right in thinking that I can use a variable in the array key like this $new_array = $test[$user];

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.