Php two-dimensional array merging Methods

Source: Internet
Author: User

Yesterday I also wrote a technical article on Array merging, where I introduced one-dimensional array merging. Here I will introduce the php two-dimensional array merging method. I hope it will be helpful to you.

Example 1

Merge Arrays Using custom methods

First look at a two-dimensional array:

The Code is as follows: Copy code

<? Php
Array (

[0] => Array (

[A] => store_name

[B] => store 1

)

[1] => Array (

[A] => store_owner

[B] => Xiaofeng

)

[2] => Array (

[A] => store_name

[B] => store 2

)

[3] => Array (

[A] => store_owner

[B] => Xiao Lei

)

?>

You will find that there are two store_name and store_owner, so I want them to merge them into the following:

<? Php

Array (

[0] => Array

(

[A] => store 1

[B] => Xiaofeng

)

[1] => Array

(

[A] => store 2

[B] => Xiao Lei

)

?>

The merge method is as follows:

The Code is as follows: Copy code

<? Php
$ Stores = array (); // defines an empty array
$ Store_count = count ($ showinfo); // count the number of times displayed. Here, $ showinfo is a variable when I get the database content, and then print it as it was just the first one.
For ($ I = 0; $ I <$ store_count; $ I ++)
{
$ J = $ I + 1; // skip the first level of $ I plus 1, which is equivalent to obtaining an odd key value for $ j.
$ Stores [] = array (
'Name' => $ showinfo [$ I] ['value'],
'Owner' => $ showinfo [$ j] ['value'],
);
$ I = $ j; // The function here is equivalent to getting an even key value from $ I.
}

?>

In this way, we can get the above results!

To help you better understand the structure of the database table, the main fields are as follows:

Key value

Store_name online shop 1

Store_owner Xiaofeng

Store_name online shop 2

Store_owner Xiao Lei

 

After using the above method, the front-end page will be displayed in a row, as shown below:

Name owner

Online Shop yixiaofeng

Shop 2 Xiaolei

Example 3

The Code is as follows: Copy code

<? Php
$ 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 ',
'2017 @ 111 ',
'5 @ 03 ',
),
2 => array (
'1 @ 01,02, 03 ',
'2 @, 05 ',
'3 @ all ',
'4 @ 01,02, 03 ',
'2017 @ 111 ',
'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 => '2017 @ 111 ,',
* 1 => '1 @ 01,02, 03 ,',
* 2 => '2 @ 01,02, 04,03, 05 ,',
* 3 => '3 @ all ',
* 4 => '4 @ all ',
* 5 => '5 @ 01,02, 04,03 ,',
*)
*/
?>

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.