开发者

Merging arrays of different types

i have two arrays:

    $a =array(   
        'standard'      => (object) array( 'id' => 'standard',      'title' => 'AustPost Standard' ), 
        'registered'    => (object) array( 'id' => 'registered',    'title' => 'AustPost Registered' ), 
        'insured'       => (object) array( 'id' => 'insured',       'title' => 'AustPost Insured' ),
        'express'       => (object) array( 'id' => 'express',       'title' => 'AustPost Express' ),
        'satchexp'      => (object) array( 'id' => 'satchexp',      'title' => 'AustPost Satchel Express' ),
        'satchreg'      => (object) array( 'id' => 'satchreg',      'title' => 'AustPost Satchel Registered' ),
        'satchpla'      => (object) array( 'id' => 'satchpla',      'title' => 'AustPost Satchel Platnium' )
    );

and

$b = array( 'standard', 'sea', 'air', 'satchexp', 'satchreg', 'satchpla' )

how can i create a new array $c that has only elements from 开发者_开发问答array $a that appear in array $b?


It's called array intersection

You can do it either by key or value. In your case in $a you have (standard', 'sea', 'air'..) as keys, but in $b those words are actually values and the keys are (0, 1, 2...)

You can easilly flip the array $b to make the words array keys. Then you can intersect arrays by keys

$c = array_intersect_key($a, array_flip($b));
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜