roikles
roikles

Reputation: 28

extract a sub-array from a multidimentional array based on the array key

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.

Upvotes: 0

Views: 1986

Answers (1)

Select0r
Select0r

Reputation: 12638

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)

Upvotes: 3

Related Questions