PHP implements a method of reorganizing a MySQL duplicate ID two-dimensional array into a three-dimensional array _php techniques

Source: Internet
Author: User
Tags php programming php regular expression

This article describes the PHP implementation of the MySQL duplicate ID two-dimensional array into a three-dimensional array of methods. Share to everyone for your reference, specific as follows:

Application Scenarios

MySQL when using associated query, such as product table and Product Picture Table Association, a product multiple product picture, the associated query results are as follows:

$arr =[
  [' id ' =>1, ' img ' => ' img1 '],
  [' id ' =>1, ' img ' => ' img2 '],
  [' id ' =>1, ' img ' => ' IMG3 '] , [' id '
  =>2, ' img ' => ' img1 '],
  [' id ' =>2, ' img ' => ' img2 '],
  [' id ' =>2, ' img ' => ' IMG3 '],
  [' id ' =>3, ' img ' => ' img1 '],
  [' ID ' =>3, ' img ' => ' img2 '],
  [' id ' =>3, ' img ' => ' IMG3 '],
]

So, the result we want is generally this, as follows:

$arr =[
  [' id ' =>1, ' img ' =>[' img1 ', ' img2 ', ' IMG3 ']],
  [' id ' =>2, ' img ' =>[' img1 ', ' img2 ', ' IMG3 ']],
  [' id ' =>3, ' img ' =>[' img1 ', ' img2 ', ' IMG3 ']]


Solution

$arr =[
    [' id ' =>1, ' img ' => ' img1 '],
    [' id ' =>1, ' img ' => ' img2 '],
    [' id ' =>1, ' img ' => ' IMG3 '] , [' id '
    =>2, ' img ' => ' img1 '],
    [' id ' =>2, ' img ' => ' img2 '],
    [' id ' =>2, ' img ' => ' IMG3 '],
    [' id ' =>3, ' img ' => ' img1 '],
    [' ID ' =>3, ' img ' => ' img2 '],
    [' id ' =>3, ' img ' => ' IMG3 '],
]
$arr 1=array ();
foreach ($arr as $key => $value) {
    if (In_array ($value [' id '], $value)) {
     $arr 1[$value [' id ']][' id ']= $value [ ' id '];
     $arr 1[$value [' ID ']][' img '][]= $value [' img '];
    }
Var_dump ($arr 1);

More about PHP Interested readers can view the site topics: "PHP array" Operation tips Daquan, "PHP Sorting algorithm Summary", "PHP common traversal algorithm and skills summary", "PHP Data structure and algorithm tutorial", "PHP Programming Algorithm Summary", " PHP Mathematical Calculation Skills Summary, "PHP Regular Expression Usage summary", "PHP operation and operator Usage Summary", "PHP string (String) Usage summary" and "PHP common database Operation skill Summary"

I hope this article will help you with the PHP program design.

Related Article

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.