duangou1953 2015-02-21 15:35
浏览 42
已采纳

PHP ARRAY获取特定值的项目索引

I have these 2 arrays $fonts['google'] and $data['value'] with the following content:

var_dump ($fonts['google']) outputs
array(4) {
    [0]=> array(3) { ["family"]=> string(7) "ABeeZee" ["variants"]=> array(2) { [0]=> string(7) "regular" [1]=> string(6) "italic" } ["subsets"]=> array(1) { [0]=> string(5) "latin" } } 
    [1]=> array(3) { ["family"]=> string(4) "Abel" ["variants"]=> array(1) { [0]=> string(7) "regular" } ["subsets"]=> array(1) { [0]=> string(5) "latin" } } 
    [2]=> array(3) { ["family"]=> string(13) "Abril Fatface" ["variants"]=> array(1) { [0]=> string(7) "regular" } ["subsets"]=> array(2) { [0]=> string(5) "latin" [1]=> string(9) "latin-ext" } } 
    [3]=> array(3) { ["family"]=> string(8) "Aclonica" ["variants"]=> array(1) { [0]=> string(7) "regular" } ["subsets"]=> array(1) { [0]=> string(5) "latin" } }
}


var_dump ($data['value']) outputs
array(4) {
    ["size"]=>  int(17) 
    ["family"]=>  string(3) "Exo"
    ["style"]=>  string(3) "200"
    ["subsets"]=>  string(5) "latin"
}       

Now I get the $data['value']['family'] = 'Abel' from my database.

Questions:

  • How can I get the ['variants'] for the given $data['value']['family'] value?
  • How can I get the index in $fonts['google'] for the sub-array where the $data['value']['family'] value is?
  • 写回答

1条回答 默认 最新

  • dpdkqls6399 2015-02-21 18:05
    关注

    PHP supports Associative Arrays which let you use a (string) key rather than a numeric index for each element. These arrays are akin to javascript objects, Objective-C dictionaries, java HashMaps, etc. That makes scenarios like this easy. Do you have control over building the original data array? If you can refactor your storage, set up the arrays like this:

    $fonts['google'] = [
        ["ABeeZee"] => [
            ["variants"]=>["regular", "italic"],
            ["subsets"]=>["latin"]
        ],
        ["Abel"] => [
            ["variants"]=>["regular"],
            ["subsets"]=>["latin"]
        ],
        ["Abril Fatface"] => [
            ["variants"]=>["regular"],
            ["subsets"]=>["latin", "latin-ext"]
        ],
        ["Aclonica"] => [
            ["variants"]=>["regular"],
            ["subsets"]=>["latin"]
        ]
    ]
    

    extra credit: if you have the original data as in the post, you could convert it:

    $newArray = array(); // or just [] in PHP >= 5.3 I believe
    foreach($fonts['google'] as $index=>$fontArray) {
        $newArray[$fontArray['family']] = $fontArray;
        // this leaves a redundant copy of the family name in the subarray
        unset $newArray[$fontArray['family']]['family']; // if you want to remove the extra copy
    }
    

    Then it becomes trivial. Given a font family name, you just access $fonts['google'][$fontFamilyName] (or $newArray[$fontFamilyName]) using the family name as the array index.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥35 平滑拟合曲线该如何生成
  • ¥100 c语言,请帮蒟蒻写一个题的范例作参考
  • ¥15 名为“Product”的列已属于此 DataTable
  • ¥15 安卓adb backup备份应用数据失败
  • ¥15 eclipse运行项目时遇到的问题
  • ¥15 关于#c##的问题:最近需要用CAT工具Trados进行一些开发
  • ¥15 南大pa1 小游戏没有界面,并且报了如下错误,尝试过换显卡驱动,但是好像不行
  • ¥15 没有证书,nginx怎么反向代理到只能接受https的公网网站
  • ¥50 成都蓉城足球俱乐部小程序抢票
  • ¥15 yolov7训练自己的数据集