开发者

How to remove particular element from an Array and merge all other content together in php?

开发者 https://www.devze.com 2022-12-20 01:37 出处:网络
I have input array as: Input Array: array 324545433 => array 0 => array \'Age\' => int 0 \'Status\' => string \'INSTALLED/GOOD\'

I have input array as:

Input Array:

array
  324545433 => 
    array
      0 => 
        array
          'Age' => int 0
          'Status' => string 'INSTALLED/GOOD' 
          'Id' => string '830026495' 
          'name' => string 'TV' 
      1 => 
        array
          'Age' => int 0
          'Status' => string 'INSTALLED/GOOD' 
          'Id' => string '553718299' 
          'name' => string 'Computer'         
array
  324545432 => 
        array
          0 => 
            array
              'Age' => int 0
              'Status' => string 'INSTALLED/GOOD' 
              'Id' => string '830026495' 
              'name' => string 'TV' 
          1 => 
            array
              'Age' => int 0
              'Status' => string 'INSTALLED/GOOD' 
              'Id' => string '553718299' 
              'name' => string 'Computer'
array
  324545431 => 
        array
          0 => 
            array
              'Age' => int 0
              'Status' => string 'INSTALLED/GOOD' 
              'Id' => string '830026495' 
              'name' => string 'TV' 
          1 => 
            array
              'Age' => int 0
              'Status' => string 'INSTALLED/GOOD' 
              'Id' => string '553718299' 
              'name' => string 'Computer'
array
  324545434 => 
        array
          0 => 
            array
              'Age' => int 0
              'Status' => string 'INSTALLED/GOOD' 
              'Id' => string '830026495' 
              'name' => string 'TV' 
          1 => 
            array
              'Age' => int 0
              'Status' => string 'INSTALLED/GOOD' 
              'Id' => string '553718299' 
              'name' => string 'Computer'
array
  324545430 => 
        array
          0 => 
            array
              'Age' => int 0
              'Status' => string 'INSTALLED/GOOD' 
              'Id' => string '830026495' 
              'name' => string 'TV' 
          1 => 
            array
              'Age' => int 0
              'Status' => string 'INSTALLED/GOOD' 
              'Id' => string '553718299' 
              'name' => string 'Computer'

. . . . .

Output Required:

    array
      0 => 
        array
          'Age' => int 0
          'Status' => string 'INSTALLED/GOOD' 
          '开发者_运维百科Id' => string '830026495' 
          'name' => string 'TV' 
      1 => 
        array
          'Age' => int 0
          'Status' => string 'INSTALLED/GOOD' 
          'Id' => string '553718299' 
          'name' => string 'Computer'
     2 => 
        array
          'Age' => int 0
          'Status' => string 'INSTALLED/GOOD' 
          'Id' => string '830026495' 
          'name' => string 'TV' 
      3 => 
        array
          'Age' => int 0
          'Status' => string 'INSTALLED/GOOD' 
          'Id' => string '553718299' 
          'name' => string 'Computer'
      4 => 
        array
          'Age' => int 0
          'Status' => string 'INSTALLED/GOOD' 
          'Id' => string '830026495' 
          'name' => string 'TV' 
      5 => 
        array
          'Age' => int 0
          'Status' => string 'INSTALLED/GOOD' 
          'Id' => string '553718299' 
          'name' => string 'Computer'
      6 => 
        array
          'Age' => int 0
          'Status' => string 'INSTALLED/GOOD' 
          'Id' => string '830026495' 
          'name' => string 'TV' 
      7 => 
        array
          'Age' => int 0
          'Status' => string 'INSTALLED/GOOD' 
          'Id' => string '553718299' 
          'name' => string 'Computer'     
      8 => 
        array
          'Age' => int 0
          'Status' => string 'INSTALLED/GOOD' 
          'Id' => string '830026495' 
          'name' => string 'TV' 
      9 => 
        array
          'Age' => int 0
          'Status' => string 'INSTALLED/GOOD' 
          'Id' => string '553718299' 
          'name' => string 'Computer'

How can we achieve this ? Thing to note is that we can have many layers of '3242524621' like numbers which are associative array. Will Recursive function useful in here ?

Thanks.

Note: Question was asked by me earlier but it was not the same as this and so please consider this question as valid.

Thanks.


This should work:

$final = array();
foreach ($input as $arr)
{
    $final = array_merge($final, $arr);
}

EDIT: actually this may be faster instead of continually merging arrays, I'm not sure:

$final = array();
foreach ($input as $group)
{
    foreach ($group as $arr)
    {
        $final[] = $arr;
    }
}


I would do something like this:

  1. Create an empty array (e.g. $merged_array)
  2. Use a foreach function to parse through the top level arrays and use array_merge() to merge the inner arrays into $merged_array

it would look something like:

foreach($example_array as $inner){
    $merged_array = $array_merge($inner);
}

where $example_array is one of the top level arrays like '324545432'

0

精彩评论

暂无评论...
验证码 换一张
取 消