Home >Backend Development >PHP Tutorial >thinkphp array merge

thinkphp array merge

WBOY
WBOYOriginal
2016-08-18 09:15:343443browse

Problems encountered when doing infinite classification
According to the ID of the first-level directory, all third-level directories contained in the first-level directory must be read out. After the foreach loop reads out, there are 5 arrays. How can they be merged into one array?
I used the array_merge() function, but the result was still 5 arrays and the merge was not successful.

<code>array (size=3)
  0 => 
    array (size=2)
      'type_name' => string '机油-2型' (length=11)
      'type_id' => string '8' (length=1)
  1 => 
    array (size=2)
      'type_name' => string '机油-3型' (length=11)
      'type_id' => string '9' (length=1)
  2 => 
    array (size=2)
      'type_name' => string '机油-4型' (length=11)
      'type_id' => string '10' (length=2)
array (size=1)
  0 => 
    array (size=2)
      'type_name' => string '菲利普斯' (length=12)
      'type_id' => string '20' (length=2)
array (size=1)
  0 => 
    array (size=2)
      'type_name' => string '菲利普斯1' (length=13)
      'type_id' => string '21' (length=2)
array (size=1)
  0 => 
    array (size=2)
      'type_name' => string '菲利普斯1212' (length=16)
      'type_id' => string '22' (length=2)
array (size=1)
  0 => 
    array (size=2)
      'type_name' => string '汽油泵22型' (length=14)
      'type_id' => string '19' (length=2)</code>
Reply content:

Problems encountered when doing infinite classification

According to the ID of the first-level directory, all third-level directories contained in the first-level directory must be read out. After the foreach loop reads out, there are 5 arrays. How can they be merged into one array?
I used the array_merge() function, but the result was still 5 arrays and the merge was not successful.

<code>array (size=3)
  0 => 
    array (size=2)
      'type_name' => string '机油-2型' (length=11)
      'type_id' => string '8' (length=1)
  1 => 
    array (size=2)
      'type_name' => string '机油-3型' (length=11)
      'type_id' => string '9' (length=1)
  2 => 
    array (size=2)
      'type_name' => string '机油-4型' (length=11)
      'type_id' => string '10' (length=2)
array (size=1)
  0 => 
    array (size=2)
      'type_name' => string '菲利普斯' (length=12)
      'type_id' => string '20' (length=2)
array (size=1)
  0 => 
    array (size=2)
      'type_name' => string '菲利普斯1' (length=13)
      'type_id' => string '21' (length=2)
array (size=1)
  0 => 
    array (size=2)
      'type_name' => string '菲利普斯1212' (length=16)
      'type_id' => string '22' (length=2)
array (size=1)
  0 => 
    array (size=2)
      'type_name' => string '汽油泵22型' (length=14)
      'type_id' => string '19' (length=2)</code>

Use the

array_merge()

function to merge multiple one-dimensional arrays. I just saw it wrong. It turns out that these are two-dimensional arrays. Then you can only assemble the array after foreach.

How many times did the questioner check the database? In fact, you can detect it once and deal with it later.

I don’t know how the poster managed to make the merge still the same as before? Or is it that the structure the poster wants is not what you described?

<code><?php
$array1 = [
    [
        'type_name' => '机油-2型',
        'type_id' => '8'
    ],
    [
        'type_name' => '机油-2型',
        'type_id' => '2'
    ],
    [
        'type_name' => '机油-2型',
        'type_id' => '3'
    ],
];
$array2 = [
    [
        'type_name' => '机油-2型',
        'type_id' => '6'
    ],
    [
        'type_name' => '机油-2型',
        'type_id' => '22'
    ],
];
$arrays = array_merge($array1, $array2);
var_dump($arrays);</code>
Statement:
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