And*_*gan 1 php arrays wordpress
我有两个数组:
$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' )
);
Run Code Online (Sandbox Code Playgroud)
和
$b = array( 'standard', 'sea', 'air', 'satchexp', 'satchreg', 'satchpla' )
Run Code Online (Sandbox Code Playgroud)
我如何创建一个新的数组$ c,其中只有数组$ a中的元素出现在数组$ b中?
它被称为阵列交集
您可以通过键或值来完成.在$ a的情况下你有(标准','海','空'......)作为键,但在$ b中这些单词实际上是值,键是(0,1,2 ......)
您可以轻松地翻转数组$ b以生成单词数组键.然后你可以通过键交叉数组
$c = array_intersect_key($a, array_flip($b));
Run Code Online (Sandbox Code Playgroud)
归档时间: |
|
查看次数: |
62 次 |
最近记录: |