2 Ways to merge PHP two-dimensional arrays _php tutorial

Source: Internet
Author: User
Yesterday also wrote a technical article on the integration of arrays, where I introduced a one-dimensional array merge, here to introduce the PHP two-dimensional arrays of the merger method, I hope that the students will help you oh.

Example 1

Custom Method Merging arrays

Let's look at a two-dimensional array:

code as follows copy code

"!--? php
array (

[0] => ; Array (

[a] = store_name

[B] = = store one

)

[1] = = Array (

[A] = store_owner

[b] = small wind

)

[2] = = Array (

[A] = store_name

[b] + shop two

)

[3] = > Array (

[A] + store_owner

[B] = = Small lei

)

;

You'll find two store_name and Store_own. Er, so I want them to be merged into the following

!--? php

Array (

[0] = + Array

(

[A] = shop one

[B ] = Small wind

)

[1] = + Array

(

[A] + shop two

[B] = = Small lei

)

;

The merge method is as follows:

The code is as follows Copy Code

$stores = Array (); Define an empty array
$store _count=count ($showinfo); Statistics show the number of times, here the $showinfo is I get the database content of a variable, and then print out is just the beginning of the appearance
for ($i =0; $i < $store _count; $i + +)
{
$j = $i +1; The $i plus 1 skips a level equivalent to $j obtained is a value with an odd key value
$stores []=array (
' Name ' = $showinfo [$i] [' value '],
' Owner ' = $showinfo [$j] [' value '],
);
$i = $j; The effect here is equivalent to $i get a value with an even number of keys
}

?>

This will give you the results above!

To make it clearer, I wrote out the database table structure, the main fields are as follows:

Key value

Store_name Shop One

Store_owner Small Wind

Store_name Shop II

Store_owner Small Lei

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

Name owner

Shop a Little Wind

Shop two small Lei

Example 3

The code is as follows 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 ',
), br> 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.html www.bkjia.com true http://www.bkjia.com/PHPjc/632666.html techarticle yesterday also wrote a technical article on the integration of arrays, where I introduced a one-dimensional array merge, here to introduce the PHP two-dimensional array of the merger method, I hope that the alumni have some help ...

  • Contact Us

    The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

    If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

    A Free Trial That Lets You Build Big!

    Start building with 50+ products and up to 12 months usage for Elastic Compute Service

    • Sales Support

      1 on 1 presale consultation

    • After-Sales Support

      24/7 Technical Support 6 Free Tickets per Quarter Faster Response

    • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.