簡體   English   中英

PHP如何從數組創建新數組

[英]PHP How to create a new array from an array

我有一個帶有Google字體的數組(+600):

$google_fonts = array(
"ABeeZee" => "ABeeZee",
"Abel" => "Abel"
//......)

我需要這個新結構:

$google_fonts = array(
array('label' => 'ABeeZee','value' => 'ABeeZee'),
array('label' => 'Abel','value' => 'Abel')
//......)

我有以下代碼,但無法正常工作:

foreach( $google_fonts as $font ) {
  $google_fonts_array = array(
      'value' => $font,
      'label' => $font,
      'src' => ''
  );    
}
return $google_fonts_array;

此數組應在此處使用(它是使用選擇框的類型):

'choices' => array( //this is one old option
                array(
                    'value' => 'Open Sans',
                    'label' => 'Open Sans',
                    'src' => ''
                ), //and here is how I would retrieve the new array
                $google_fonts_array //or even using it instead of the previous level array, it's not working.
            )

為什么不起作用?

您每次都覆蓋陣列,請執行此操作。 (注意[]

foreach( $google_fonts as $font ) {
  $google_fonts_array[] = array(
      'value' => $font,
      'label' => $font,
      'src' => ''
  );    
}
return $google_fonts_array;

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM