douza1373
douza1373
2016-07-05 06:10

从多维数组中获取元素

已采纳

i have an array with this structure

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

["subsets"] => Array
    (
        [0] => latin
    )

)

i want to get the elements like $family = ABeeZee ,$variants = regular, italic

  • 点赞
  • 写回答
  • 关注问题
  • 收藏
  • 复制链接分享
  • 邀请回答

3条回答

  • dptj13337 dptj13337 5年前

    Please have a look attached snippet.

    Check it over here. 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>";
    }
    ?>
    

    Above snippet will return following output.

    $family=ABeeZee
    $category=sans-serif
    $variants=regular,italic
    $subsets=latin
    
    点赞 评论 复制链接分享
  • dongshi3361 dongshi3361 5年前

    I assume you want to search your array for elements that match certain criteria.

    I've included a method to search for fonts by variant or variants.

    <?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']));
    

    Output:

    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)
    
    点赞 评论 复制链接分享
  • donglin7383 donglin7383 5年前

    See this one

    <?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;
    ?>
    
    点赞 评论 复制链接分享