【问题标题】:Extracting a Subset Key from an Array从数组中提取子集键
【发布时间】:2011-12-23 18:36:49
【问题描述】:

我正在处理主题选项页面并尝试将css 密钥发送到我的php css 文件。我可以从父数组中选择array key,但我不知道如何从子集中获取css key。任何人都可以帮助我吗?我对php 很陌生,并且正在努力学习。

谢谢!

$fonts = array(  
'Arial' => array(  
    'name' => 'Arial',  
    'font' => '',  
    'css' => "font-family: Arial, sans-serif;"  
),  
'ubuntu' => array(  
    'name' => 'Ubuntu',  
    'font' => '@import url(http://fonts.googleapis.com/css?family=Ubuntu);',  
    'css' => "font-family: 'Ubuntu', sans-serif;"  
),  
'lobster' => array(  
    'name' => 'Lobster',  
    'font' => '@import url(http://fonts.googleapis.com/css?family=Lobster);',  
    'css' => "font-family: 'Lobster', cursive;"  
),  
'Lato' => array(
    'name' => 'Lobster',  
    'font' => '@import url(http://fonts.googleapis.com/css?family=Lato);',  
    'css' => "font-family: 'Lato', sans-serif;"
),
'Droid Sans' => array(
    'name' => 'Droid Sans',  
    'font' => '@import url(http://fonts.googleapis.com/css?family=Droid+Sans);',  
    'css' => "font-family: 'Droid Sans', sans-serif;"
),
'Tachoma' => array(  
    'name' => 'Tachoma',  
    'font' => '',  
    'css' => "font-family: Tachoma, Geneva, sans-serif;"  
),  
'Verdana' => array(  
    'name' => 'Verdana',  
    'font' => '',  
    'css' => "font-family: Verdana, Verdana, Geneva, sans-serif;"  
),  
'Times New Roman' => Array(
    'name' => 'Times New Roman',
    'font' => '',
    'css' => "font-family: Times New Roman, Times New Roman, serif;"
),
'Georgia' => array(  
    'name' => 'Georgia',  
    'font' => '',  
    'css' => "font-family: Georgia, serif;"  
),
'Custom Font' => array(
    'name' => $custom_family,
    'font' => $custom_font,
    'css' => "font-family:" . $custom_family . "sans-serif;",
    )  
);
$custom_font = get_option('ideate_custom_font');    
//Extract the values from the URL String that are separated by '='
$name_extract = explode('=', $custom_font);

//Find these characters in the URL string
$find = array("+", ");");
//Add Space or Null the value
$replace = array(" ","");
//Take the Family Value from the String and Remove any Special Characters from 'Find' array
$custom_family_string = str_replace($find,$replace,$name_extract[1]);
//Take Value from String Replacement and Add Quotes Around it
$custom_family = "\"" .$custom_family_string. "\"";
$font_array = array_keys($fonts);

【问题讨论】:

    标签: php arrays key multidimensional-array


    【解决方案1】:

    我想你只是想要:

    $font = 'Arial';
    $css = $fonts[$font]['css'];
    

    【讨论】:

    • 这在 PHP 文件中本地工作.. 无论如何要将此值传递给另一个文件?
    • 我已将数组放在一个单独的文件中,并将其包含在两个文件的标题中。我似乎无法在文件中调用 $css 变量。它使输出为空。
    【解决方案2】:

    我建议你阅读PHP documentation on arrays

    在您的具体情况下,您可以通过以下方式访问数据:

    $font_name = 'Verdana';
    $fonts[$font_name]['css'] //"font-family: Verdana, Verdana, Geneva, sans-serif;" 
    

    【讨论】:

      【解决方案3】:

      这应该可以工作 - $fonts['Arial']['css']。只需将“Arial”替换为您要查找的任何字体即可。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2011-07-18
        • 2010-10-08
        • 1970-01-01
        • 1970-01-01
        • 2018-10-03
        • 2017-03-15
        • 2018-12-17
        相关资源
        最近更新 更多