search
Homephp教程php手册PHP把2个二维数组合并一个二维数组2种解法

题目:有以下2个二维数组

1
$a=Array(0 => Array(id => 66,class_name => www.iiwnet.com),1 => Array(id => 67,class_name => linux ));
2
$b=Array(0 => Array(class_count=> 8),1 => Array(class_count => 2));
两个数组内容如上,如何把$a和$b合并成为一个新的二维数组,要求新数组内容如下:

1
Array(0 => Array(id => 66,class_name => www.iiwnet.com,class_count=> 8),1 => Array(id => 67,class_name => linux ,class_count => 2));
这个题目一定有人遇到过 正好今天群里有人问 就给出2种解法
以下代码由 PHP教程网 原创提供
01
方法一:
02
 
03
    function arrpreg(){
04
        $a=Array(0 => Array(id => 66,class_name => www.iiwnet.com),1 => Array(id => 67,class_name => linux ));
05
        $b=Array(0 => Array(class_count=> 8),1 => Array(class_count => 2));
06
        $arr = array();
07
        foreach ($a as $k => $r) {
08
            foreach($r as $k1 => $r1){
09
                $arr[$k][$k1] = $r1;
10
            }
11
        }  
12
        foreach ($b as $k => $r) {
13
            foreach($r as $k1 => $r1){
14
                $arr[$k][$k1] = $r1;
15
            }
16
        }
17
        return $arr;
18
    }
19
    $ar= arrpreg();  
20
    echo '

';<br>
21<br>
     print_r($ar);<br>
22<br>
    echo '
';
23
    ?>
24
 
25
方法二:
26
 
27
    $a = array(0 => Array(id => 66,class_name => www.iiwnet.com),1 => Array(id => 67,class_name => linux ));
28
    $b = array(0 => Array(class_count=> 8),1 => Array(class_count => 2));
29
    $arr = array();
30
    foreach($a as $k=>$r){
31
        $arr[] = array_merge($r,$b[$k]);
32
    }
33
    echo '
';<br>
34<br>
    print_r($arr);<br>
35<br>
    echo '
';
Statement
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Tools

SAP NetWeaver Server Adapter for Eclipse

SAP NetWeaver Server Adapter for Eclipse

Integrate Eclipse with SAP NetWeaver application server.

Atom editor mac version download

Atom editor mac version download

The most popular open source editor

EditPlus Chinese cracked version

EditPlus Chinese cracked version

Small size, syntax highlighting, does not support code prompt function

SublimeText3 English version

SublimeText3 English version

Recommended: Win version, supports code prompts!

MantisBT

MantisBT

Mantis is an easy-to-deploy web-based defect tracking tool designed to aid in product defect tracking. It requires PHP, MySQL and a web server. Check out our demo and hosting services.