如何为另一个数组中的每个键值分配一个新数组

时间:2017-08-04 18:45:40

标签: php arrays foreach str-replace

数组1:

$data = array(

'@attributes' => array(
    'coolid' => 'V546546',
    'emailaddress' => 'member@gmail.com',
    ));

这是数组2:

$map = array(

'foo' => array(
    'id' => 'foo',
    'tag' => '{my:foo}', 
    'label' => __( 'Foo', 'my_plugin' ),
    'callback' => 'foo'
  ));

我正在尝试为$ data ['@ attributes']中的每个键创建一个新数组。我试图让新数组与$ map具有相同的结构,但用$ keys $'['@ attributes']替换'foo'的提及......这样以下输出:

$desiredArray = array(

'coolid' => array(
    'id' => 'coolid',
    'tag' => '{my:coolid}', 
    'label' => __( 'coolid', 'my_plugin' ),
    'callback' => 'coolid'
  ));

'emailaddress' => array(
    'id' => 'foo',
    'tag' => '{my:emailddress}', 
    'label' => __( 'emailaddress', 'my_plugin' ),
    'callback' => 'emailaddress'
  ));

所有帮助表示赞赏,我已经尝试了这个foreach ..但没有运气

foreach($data['@attributes'] as $key) {    
   $key = array(
    'id' =>  $key,
    'tag' => '{my:'.$key.'}', 
    'label' => __( $key, 'my_plugin' ),
    'callback' => $key
  ));
}

1 个答案:

答案 0 :(得分:0)

$desiredArray = array();
foreach($data['@attributes'] as $key => $value) {    
   $desiredArray[$key] = array(
      'id' =>  $key,
      'tag' => '{my:'.$key.'}', 
      'label' => __( $key, 'my_plugin' ),
      'callback' => $key
   );
}

Check foreach syntax