从多维数组中获取元素



我有一个这样结构的数组

$font = Array
(
["family"] => ABeeZee
["category"] => sans-serif
["variants"] => Array
    (
        [0] => regular
        [1] => italic
    )
["subsets"] => Array
    (
        [0] => latin
    )
)

我想要得到像$family = ABeeZee,变异=美元定期,斜体

请看附件中的代码片段。

看这里。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

我假设您想在数组中搜索符合特定条件的元素。

我已经包含了一个按变体或变体搜索字体的方法。

<?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)

看这个

<?php
 $arr=array(
 "family" => 'ABeeZee',
 "category" => "sans-serif",
 "variants" => array("regular","italic"),
 "subsets" => array("latin")
 );
 $family =  $arr['family'];
 $variants = '';
 foreach($arr['variants'] as $val){
  if($variants != ''){
   $variants = $variants.',';
   }
  $variants = $variants.$val;
 }
echo $family.PHP_EOL;
echo $variants;
?>

相关内容

  • 没有找到相关文章

最新更新