Home > Backend Development > PHP Tutorial > Two methods of merging two-dimensional arrays in PHP_PHP tutorial

Two methods of merging two-dimensional arrays in PHP_PHP tutorial

WBOY
Release: 2016-07-13 10:50:08
Original
965 people have browsed it

Yesterday I also wrote a technical article about array merging. There I introduced the merging of one-dimensional arrays. Here I will introduce the merging method of two-dimensional arrays in PHP. I hope it will be helpful to all students.

Example 1

Custom method to merge arrays

Look at a two-dimensional array first:

The code is as follows Copy code
 代码如下 复制代码

Array (

[0] => Array (

                              [A] => store_name

                              [B] => 商店一

                             )

          [1] => Array (

                              [A] => store_owner

                              [B] => 小风

                              )

          [2] => Array (

                              [A] => store_name

                              [B] => 商店二

                              )

          [3] => Array (

                               [A] => store_owner

                               [B] => 小磊

                               )

?>

大家会发现里面有两个 store_name 和 store_owner ,所以我想让他们合并成下面这样

Array (

[0] => Array

         (

           [A] => 商店一

           [B] => 小风

          )

      [1] => Array

         (

           [A] => 商店二

           [B] => 小磊

          )

?>

Array (<🎜> <🎜>         [0] => Array (<🎜> <🎜>                                                                                                                                                                                                                          <🎜>                                                                                                                                                                                                                                             <🎜> )<🎜> <🎜>         [1] => Array (<🎜> <🎜>                                                                                                                                                                                                                              ​  <🎜>                                                                                                                                                                                                               <🎜> )<🎜> <🎜>         [2] => Array (<🎜> <🎜>                                                                                                                                                                                                                          <🎜>                                                                                                                                                                                                                                                     <🎜> )<🎜> <🎜>         [3] => Array (<🎜> <🎜>                                                                                                                                                                                                                                       ​  <🎜>                                                                                                                                                                                                                                                           <🎜> )<🎜> <🎜>?> You will find that there are two store_name and store_owner inside, so I want to merge them into something like this <🎜>Array (<🎜> <🎜> [0] => Array<🎜> <🎜> (<🎜> <🎜>                                                                                                                                                                                                                                ​ > ) [1] => Array (                                                                                                                                                                                                                             ​                                                     [B] => ) ?>

The merging method is as follows:

The code is as follows Copy code
 代码如下 复制代码

$stores = array(); //定义一个空数组
$store_count=count($showinfo); //统计显示的次数,这里的$showinfo是我获取数据库内容时的一个变量,然后打印出来就是上面刚开始的样子
for($i=0;$i<$store_count;$i++)
{
$j=$i+1; //将 $i 加 1 跳过 一级 相当于 $j 取得是 键值为 奇数的值
$stores[]=array(
'name' =>$showinfo[$i]['value'],
                          'owner'=>$showinfo[$j]['value'],
                          );
            $i = $j;  //  这里的作用是 相当于 $i 取得是 键值为 偶数的值
        }

?>

$stores = array(); //Define an empty array
$store_count=count($showinfo); //Count the number of times displayed. $showinfo here is a variable when I get the database content, and then print it out as it did at the beginning
for($i=0;$i<$store_count;$i++)
                                  {
                                                                                                                                                                                                                                                                        $stores[]=array(
‘name’ =>$showinfo[$i]['value'],
‘owner’=>$showinfo[$j]['value'],
);
                  $i = $j; // The function here is equivalent to $i getting the value whose key value is an even number
} ?>

In this way you can get the above results!

In order to let everyone see it more clearly, I wrote out the database table structure. The main fields are as follows:

key value

store_name online store one

store_owner Xiaofeng

store_name online store 2

store_owner Xiao Lei

After using the above method, the front page can be displayed in a row, as follows

name owner

Online Shop 1 Xiaofeng

Online Shop 2 Xiao Lei

Example 3

The code is as follows
 代码如下 复制代码

$arr = array
(
0 => array(
    '1@01,02',
    '2@01,02',
    '4@ALL',
    '3@01',
    '5@01,02,04',
    ),

    1 => array(
    '1@01,02,03',
    '2@01,02,04',
    '3@ALL',
    '4@01,02',
    '111@01,05',
    '5@03',
    ),
    2 => array(
    '1@01,02,03',
    '2@02,03,05',
    '3@ALL',
    '4@01,02,03',
    '111@01,05',
    '5@03',
    ),
);
$result = array();
foreach($arr as $items){
    if(is_array($items)){
        foreach($items as $item){
            $item = explode('@', $item);
            if(count($item) != 2){
                continue ;
            }
            $result[$item[0]] .= $item[1].',';
        }
    }
}
function reJoin(&$item,$key,$seq){
    $list = array_unique(explode($seq,$item));
    if (in_array('ALL', $list)){
        $item = $key.'@ALL';
    }else{
        $item = $key.'@'.join($seq,$list);
    }
}
array_walk($result, 'reJoin',',');
sort($result);
var_export($result);
/**
 * array (
  * 0 => '111@01,05,',
  * 1 => '1@01,02,03,',
  * 2 => '2@01,02,04,03,05,',
  * 3 => '3@ALL',
  * 4 => '4@ALL',
  * 5 => '5@01,02,04,03,',
  * )
 */
?>

Copy code
$arr = array
(
0 => array(
'1@01,02',
'2@01,02',
'4@ALL',
'3@01',
'5@01,02,04',
),

1 => array(
'1@01,02,03',
'2@01,02,04',
'3@ALL',
'4@01,02',
'111@01,05',
'5@03',
),
2 => array(
'1@01,02,03',
'2@02,03,05',
'3@ALL',
'4@01,02,03',
'111@01,05',
'5@03',
),
);
$result = array();
foreach($arr as $items){
If(is_array($items)){
          foreach($items as $item){
               $item = explode('@', $item);
If(count($item) != 2){
                                                                                                                                                                                  continue ;
             }
                $result[$item[0]] .= $item[1].',';
         }
}
}
function reJoin(&$item,$key,$seq){
$list = array_unique(explode($seq,$item));
If (in_array('ALL', $list)){
         $item = $key.'@ALL';
}else{
          $item = $key.'@'.join($seq,$list);
}
}
array_walk($result, 'reJoin',',');
sort($result);
var_export($result);
/**
 * array (
  * 0 => '111@01,05,',
  * 1 => '1@01,02,03,',
  * 2 => '2@01,02,04,03,05,',
  * 3 => '3@ALL',
  * 4 => '4@ALL',
  * 5 => '5@01,02,04,03,',
  * )
 */
?>

http://www.bkjia.com/PHPjc/632666.htmlwww.bkjia.com
true
http: //www.bkjia.com/PHPjc/632666.html
TechArticleYesterday I also wrote a technical article about array merging, where I introduced one-dimensional array merging. Here is an introduction to the merging method of two-dimensional arrays in PHP. I hope it will be helpful to all students...
source:php.cn
Statement of this Website
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template