我有一个具有这种结构的数组
$font = Array
(
["family"] => ABeeZee
["category"] => sans-serif
["variants"] => Array
(
[0] => regular
[1] => italic
)
["subsets"] => Array
(
[0] => latin
)
)
我想获得像 $ family = ABeeZee 这样的元素 ,$ variants = regular,italic
答案 0 :(得分:0)
请看一下附件摘录。
在这里查看。 https://eval.in/600521
<?php
$arr=array(
"family" => 'ABeeZee',
"category" => "sans-serif",
"variants" => array("regular","italic"),
"subsets" => array("latin"));
foreach($arr as $key=>$arr){
if(is_array($arr)){
echo "$".$key ."=". implode(",",$arr);
}else{
echo "$".$key ."=". $arr;
}
echo "<br>";
}
?>
以上代码段将返回以下输出。
$family=ABeeZee
$category=sans-serif
$variants=regular,italic
$subsets=latin
答案 1 :(得分:0)
见这个
s
答案 2 :(得分:0)
我假设您要在数组中搜索符合特定条件的元素。
我已经提供了一种按变体或变体搜索字体的方法。
<?php
$fonts =
[
[
'family' => 'Foo',
'category' => 'sans-serif',
'variants' =>
[
'regular',
'italic'
]
],
[
'family' => 'Bar',
'category' => 'cursive',
'variants' =>
[
'regular',
'bold'
]
],
[
'family' => 'Baz',
'category' => 'monospace',
'variants' =>
[
'italic',
'regular',
'bold'
]
]
];
// Build some indexes for search functions.
$family_index = [];
$variants_index = [];
foreach($fonts as $index => $font) {
$family_index[$font['family']] = $index;
foreach($font['variants'] as $variant) {
$variants_index[$variant][] = $index;
}
}
$filter_by_family = function($family) use ($fonts, $family_index) {
return isset($family_index[$family]) ? $fonts[$family_index[$family]] : null;
};
$filter_by_variants = function(array $variants) use ($fonts, $variants_index) {
$font_index_matches = array();
foreach($variants as $variant) {
if(isset($variants_index[$variant])) {
$font_index_matches[] = $variants_index[$variant];
}
}
if(!count($font_index_matches)) return;
$matches = count($font_index_matches) > 1
? call_user_func_array('array_intersect', $font_index_matches)
: $font_index_matches[0];
return array_intersect_key($fonts, array_flip($matches));
};
var_dump($filter_by_family('Bar'));
var_dump($filter_by_variants(['italic']));
var_dump($filter_by_variants(['regular','bold']));
输出:
array (size=3)
'family' => string 'Bar' (length=3)
'category' => string 'cursive' (length=7)
'variants' =>
array (size=2)
0 => string 'regular' (length=7)
1 => string 'bold' (length=4)
array (size=2)
0 =>
array (size=3)
'family' => string 'Foo' (length=3)
'category' => string 'sans-serif' (length=10)
'variants' =>
array (size=2)
0 => string 'regular' (length=7)
1 => string 'italic' (length=6)
2 =>
array (size=3)
'family' => string 'Baz' (length=3)
'category' => string 'monospace' (length=9)
'variants' =>
array (size=3)
0 => string 'italic' (length=6)
1 => string 'regular' (length=7)
2 => string 'bold' (length=4)
array (size=2)
1 =>
array (size=3)
'family' => string 'Bar' (length=3)
'category' => string 'cursive' (length=7)
'variants' =>
array (size=2)
0 => string 'regular' (length=7)
1 => string 'bold' (length=4)
2 =>
array (size=3)
'family' => string 'Baz' (length=3)
'category' => string 'monospace' (length=9)
'variants' =>
array (size=3)
0 => string 'italic' (length=6)
1 => string 'regular' (length=7)
2 => string 'bold' (length=4)