2 methods for merging two-dimensional PHP arrays

Source: Internet
Author: User

Example 1

Custom Methods Merge Arrays

First look at a two-dimensional array:

The code is as follows Copy Code

<?php
Array (

[0] => Array (

[A] => store_name

[B] => Store A

)

[1] => Array (

[A] => Store_owner

[B] => small Wind

)

[2] => Array (

[A] => store_name

[B] => Store II

)

[3] => Array (

[A] => Store_owner

[B] => Xiao Lei

)

?>

You'll find two store_name and store_owner in there, so I want them to merge into the following.

<?php

Array (

[0] => Array

(

[A] => store a

[B] => small Wind

)

[1] => Array

(

[A] => Store II

[B] => Xiao Lei

)

?>

The merge method is as follows:

The code is as follows Copy Code

<?php
$stores = Array (); Define an empty array
$store _count=count ($showinfo); Statistics show the number of times, the $showinfo here is a variable when I get the contents of the database, and then print it out just the way it started.
for ($i =0; $i < $store _count; $i + +)
{
$j = $i +1; Skipping $i plus 1 is equivalent to $j get a value with an odd key value
$stores []=array (
' Name ' => $showinfo [$i] [' value '],
' Owner ' => $showinfo [$j] [' value '],
);
$i = $j; The effect here is the equivalent of $i get a value that has a key value of even
}

?>

So that we can get the result of the above!

In order for everyone to read more clearly, I write the database table structure as the main fields as follows:

Key value

Store_name Shop A

Store_owner Small Wind

Store_name Shop II

Store_owner Xiao Lei

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

Name owner

Shop a small wind

Shop two small Lei

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 ',
' 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, ',
* )
*/
?>

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.