将值放到关联的值数组中

时间:2013-10-27 12:26:18

标签: php arrays

我有一个关于PHP数组的问题,我想要做的是将值放到关联数组中。

//before
$a = array(
       array(
         'name' => 'test1',
         'email' => 'test@test.com',
         'desc' => 'Who know1',
         'button_link' => 'yest2'
        ),
       array(
         'name' => 'test2',
         'email' => 'test2@test.com',
         'desc' => 'Who know2',
         'button_link' => 'yest2'
    ) 
);

$b = array(
      array(
        'item1' => 'value1',
        'item2' => 'value2'
       ),
      array(
        'item1' => 'value3',
        'item2' => 'value4'
    )
 );

我想将$b['item1']$b['item2']一个值放入$ a ['desc'] ,.如下......

//after
$after_combile = array(
   array(
     'name' => 'test1',
     'email' => 'test@test.com',
     'desc' => 'Who know1 $b["item1"] $b["item2"]',
     'button_link' => 'yest2'
    ),
   array(
     'name' => 'test2',
     'email' => 'test2@test.com',
     'desc' => 'Who know2 $b["item1"] $b["item2"]',
     'button_link' => 'yest2'
   ) 
);

我尝试了许多方法,例如array_walk等等。

我想输出正好在

之下

*的输出

array (size=2)
  0 => 
    array 
        'name' => 'test1',
        'email' => 'test@test.com',
        'desc' => 'Who know1 value1' value2',
        'button_link' => 'yest2'
  1 => 
    array 
     '  name' => 'test2',
        'email' => 'test2@test.com',
        'desc' => 'Who know2 value3 value1',
        'button_link' => 'yest2'

抱歉,我的问题不明确。

5 个答案:

答案 0 :(得分:1)

$a = array(
    array(
        'name' => 'test1',
        'email' => 'test@test.com',
        'desc' => 'Who know1',
        'button_link' => 'yest2'
    ),
    array(
        'name' => 'test2',
        'email' => 'test2@test.com',
        'desc' => 'Who know2',
        'button_link' => 'yest2'
    )
);

$b = array(
    array(
        'item1' => 'value1',
        'item2' => 'value2'
    ),
    array(
        'item1' => 'value3',
        'item2' => 'value4'
    )
);

$after_combile = array();

foreach($a as $k => $array) {
    $array['desc'] .= implode(' ', $b[$k]);
    $after_combile[] = $array;
}

答案 1 :(得分:1)

$result = array_map(function($a,$b){
   $a['desc'] .= ' '.implode(', ',$b);
   return $a;
},$a,$b);

答案 2 :(得分:0)

假设您使用的是类似的密钥:

for($i=0;$i<count($a);$i++) {
    $a[$i]['desc'] = $a[$i]['desc'].$b[$i]['item1'].$b[$i]['item2'];
}

答案 3 :(得分:0)

使用:

$after_combile = array();

foreach($a as $key=>$array) {
    $array["desc"] .= " ".implode(" ",$b[$key]); 
    $after_combile[] = $array;
}

print_r($after_combile);

答案 4 :(得分:0)

使用array_walk除了不脏之外还有更好的性能:

<?php
$a = array(
        array(
            'name' => 'test1',
            'email' => 'test@test.com',
            'desc' => 'Who know1',
            'button_link' => 'yest2'
        ),
        array(
            'name' => 'test2',
            'email' => 'test2@test.com',
            'desc' => 'Who know2',
            'button_link' => 'yest2'
        ) 
);
$b = array(
        array(
            'item1' => 'value1',
            'item2' => 'value2'
        ),
        array(
            'item1' => 'value3',
            'item2' => 'value4'
        )
);
array_walk($a, function(&$arr, $key){
    global $b;
    $arr['desc'] .= " ".implode(" ", $b[$key]);
});
?>