如何使用WHERE子句合并数组?

时间:2015-03-25 05:41:05

标签: php arrays codeigniter

如何将第二个数组添加到[myid]匹配的第一个数组中。

第一阵列

Array
    (
            [0] => Array
                (
                    [myid] => 70
                    [realname] => Kishore
                    [full_name] => Kishore Chandra
                    [category] => professional
                    [firm_name] => Yes
                    [designation] => Mechanical
                    [address] => Dwarakanagar 5th lane
                    [city] => Vishakhapatnam
                    [email] => yesapps.india@gmail.com
                )

            [1] => Array
               (
                    [myid] => 75
                    [realname] => Vinod kumar
                    [full_name] => Kishore Chandra
                    [category] => professional
                    [firm_name] => 
                    [designation] => 
                    [address] => 
                    [city] => 
                    [email] => vinod.k.alluri@gmail.com
               )
     )

第二阵列

需要将这些项目添加到数组1

Array
        (
                [0] => Array
                    (
                        [myid] => 70
                        [projects] => 20
                    )

                [1] => Array
                   (
                        [myid] => 75
                        [projects] => 43
                   )
         )

我曾尝试合并数组,但没有用,我在这个数组中又增加了两个对象

我希望我的要求清晰可读,如果没有请在评论中提及,我可以解释一下。

答案可以在codeigniter中,我也在使用Codeigniter框架

2 个答案:

答案 0 :(得分:0)

foreach($array1 as $key => $array){
  foreach($array as $key2 => $value){
    $results[$key]['myid'] = $value;
  }
}

foreach($array2 as $key => $array){
  foreach($array as $key3 => $value){
    $results[$key3]['projects'] = $value;
  }
}
print_r($results);

答案 1 :(得分:0)

假设您希望结果数组将项目合并到第一个数组

你希望结果集像这样

 Array
 (
    [0] => Array
        (
            [myid] => 70
            [realname] => Kishore
            [full_name] => Kishore Chandra
            [category] => professional
            [firm_name] => Yes
            [designation] => Mechanical
            [address] => Dwarakanagar 5th lane
            [city] => Vishakhapatnam
            [email] => yesapps.india@gmail.com
            [projects] => 20
        )

    [1] => Array
        (
            [myid] => 75
            [realname] => Vinod kumar
            [full_name] => Kishore Chandra
            [category] => professional
            [firm_name] => 
            [designation] => 
            [address] => 
            [city] => 
            [email] => vinod.k.alluri@gmail.com
            [projects] => 43
        )

)

所以试试这段代码

foreach($b as $key => $val){        
    if(isset($a[$key]) && $a[$key]->myid == $val->myid){
        $a[$key]->projects = $val->projects; 
    }       
}

如果将stdArray转换为Array json_decode($ A,TRUE) 和json_decode($ b,TRUE)

$a = Array
(
        0 => Array
            (
                'myid' => 70,
                'realname' => 'Kishore',
                'full_name' => 'Kishore Chandra',
                'category' => 'professional',
                'firm_name' => 'Yes',
                'designation' => 'Mechanical',
                'address' => 'Dwarakanagar 5th lane',
                'city' => 'Vishakhapatnam',
                'email' => 'yesapps.india@gmail.com',
            ),

        1 => Array
           (
                'myid' => 75,
                'realname' => 'Vinod kumar',
                'full_name' => 'Kishore Chandra',
                'category' => 'professional',
                'firm_name' => '',
                'designation' => '',
                'address' => '',
                'city' => '',
                'email' => 'vinod.k.alluri@gmail.com'
           )
 );

 $b = Array ( 0 => Array(
                    'myid' => 70,
                    'projects' => 20
                    ),
            1 => Array(
                    'myid' => 75,
                    'projects' => 43
                    )
            );

foreach($b as $key => $val){        
    if(isset($a[$key]) && $a[$key]['myid'] == $val['myid']){
        $a[$key]['projects'] = $val['projects']; 
    }       
}
print_r($a);

注意:

  1. $ b是第二个数组
  2. $ a是第一个数组