1

I have a question about arrays in PHP:

I have many zip files having many data inside and using preg_grep I could get the results below:

(These data are returned from multiple text files in the zip file)

    => Array
            (
                [ZipName] =>  data.zip
                [Age] => 45
            )
    => Array
            (
                [ZipName] =>  data.zip
                [Name] => John
            )
    => Array
            (
                [ZipName] => data2.zip
            )
    => Array
            (
                [ZipName] =>  data1.zip
                [Age] => 25 
            )
     => Array
            (
                [ZipName] =>  data2.zip
                [Age] => 27
            )
     => Array
            (
                [ZipName] =>  data1.zip
                [Name] => Abram
            )
     => Array
            (
                [ZipName] =>  data1.zip
            )
     => Array
            (
                [ZipName] =>  data2.zip
                [City] => London 
            )          
    => Array
            (
                [ZipName] =>  data2.zip
                [Name] => Inna 
            )         
    => Array
            (
                [ZipName] =>  data1.zip
                [City] => London 
            )

So how I can combine/merge all of these arrays using the ZipName Value !

Like in SQL it easy :

SELECT * FROM * WHERE ZipName= 'x';

0 => Array
        (
            [ZipName] =>  data.zip
            [Age] => 45
            [Name] => John
            [City] => Leicester 
        )

1 => Array
        (
            [ZipName] =>  data1.zip
            [Age] => 25
            [Name] => Abram
            [City] => London 
        )

2 => Array
        (
            [ZipName] =>  data2.zip
            [Age] => 27
            [Name] => Inna
            [City] => London
        )
Kadi
  • 37
  • 6
  • is input array multi dimensional array or different - 2 array ?? – Forge Web Design Jun 18 '20 at 01:32
  • @ForgeWebDesign no differenet 0 1 2 3 etc .. the arrays follows the total count of text files which mean each new text file a new array – Kadi Jun 18 '20 at 01:35
  • then you can use array_merge() function – Forge Web Design Jun 18 '20 at 01:38
  • 1
    Does your actual data have different values for `ZipName`? Might be helpful to include that in your question. – Yoshi Jun 18 '20 at 12:24
  • Yes different zipname – Kadi Jun 18 '20 at 12:24
  • @kadi-dz - can you please update your question with more realistic data so we understand what you want? – bestprogrammerintheworld Jun 18 '20 at 12:29
  • 1
    Effectively, you want to do this: https://stackoverflow.com/a/62442134/2943403 but just remove the `unset()` call because you want to keep the zipname in the subarrays. like this: https://3v4l.org/tjtqe – mickmackusa Jun 18 '20 at 13:36
  • @mickmackusa thank you so much that helped lot, I have another question what If I'm getting the ages with a function and names with another one and in return I have two huge arrays list, I could combine it using this ?! with considering that sometimes I get more than one name in the text file of that zip file which returns me many results – Kadi Jun 18 '20 at 14:15
  • I realize that you are new to this place, but I must tell you: You are going to irritate volunteers if you keep extending/changing your requirements. You must only ask one question per page and your requirements must not change. I will suggest that you accept Yoshi's answer (it is the best one on the page right now). As for your new requirements, you should go away and try and research and try, then if you need help come back and ask a new question. – mickmackusa Jun 18 '20 at 14:18

3 Answers3

1

An alternative to using array_merge, array_combine etc.. ...is just two simple foreach loops and setting the value into a new array ($new_arr).

$new_arr = [];
foreach($arr as $inner_arr) {
    foreach($inner_arr as $key=>$value) {
        $new_arr[$key] = $value;
    }
}

UPDATE I was not clear what OP wanted but I hope it's clearer now:

Try this: (Basically adding values to array[name of ZipName][{current key for the inner array}]

$arr = [
    [
        'ZipName' => 'data.zip',
        'Age' => '45'
    ],
    [
        'ZipName' => 'data.zip',
        'Name' => 'John'
    ],
    [
        'ZipName' => 'data2.zip',
        'Age' => '27'
    ],
    [
        'ZipName' => 'data1.zip',
        'Name' => 'Abram'
    ],
    [
        'ZipName' => 'data1.zip'
    ],    
    [
        'ZipName' =>  'data2.zip',
        'City' => 'London' 
    ],    
    [
        'ZipName' =>  'data2.zip',
        'Name' => 'Inna' 
    ],    
    [
        'ZipName' =>  'data1.zip',
        'City' => 'London' 
    ]


];


$new_arr = [];
foreach($arr as $key => $inner_arr) {
   foreach($inner_arr as $ik => $item) {
       $new_arr[$inner_arr['ZipName']][$ik] = $item;
   }
}

Output:

Array
(
    [data.zip] => Array
        (
            [ZipName] => data.zip
            [Age] => 45
            [Name] => John
        )

    [data2.zip] => Array
        (
            [ZipName] => data2.zip
            [Age] => 27
            [City] => London
            [Name] => Inna
        )

    [data1.zip] => Array
        (
            [ZipName] => data1.zip
            [Name] => Abram
            [City] => London
        )

)

When doing:

$new_arr = array_values($new_arr);

the key simply gets do be a numeric index (if it matters)

Array
(
    [0] => Array
        (
            [ZipName] => data.zip
            [Age] => 45
            [Name] => John
        )

    [1] => Array
        (
            [ZipName] => data2.zip
            [Age] => 27
            [City] => London
            [Name] => Inna
        )

    [2] => Array
        (
            [ZipName] => data1.zip
            [Name] => Abram
            [City] => London
        )

)
bestprogrammerintheworld
  • 5,417
  • 7
  • 43
  • 72
0

What you need is to group by the unique values at the index ['ZipName'] and then merge those groups individually. This can be done with array_reduce.

Example:

<?php
declare(strict_types=1);

$input = [
    [
        'ZipName' => 'data_1.zip',
        'Age'     => 45,
    ], [
        'ZipName' => 'data_1.zip',
        'Name'    => 'John',
    ], [
        'ZipName' => 'data_1.zip',
    ], [
        'ZipName' => 'data_1.zip',
        'City'    => 'Leicester',
    ],

    [
        'ZipName' => 'data_2.zip',
        'Age'     => 45,
    ], [
        'ZipName' => 'data_2.zip',
        'Name'    => 'John',
    ], [
        'ZipName' => 'data_2.zip',
    ], [
        'ZipName' => 'data_2.zip',
        'City'    => 'Leicester',
    ],
];

$output = array_values(array_reduce($input, static function (array $acc, array $item): array {
    $acc[$item['ZipName']] = array_merge($acc[$item['ZipName']] ?? [], $item);

    return $acc;
}, []));

print_r($output);
Yoshi
  • 54,081
  • 14
  • 89
  • 103
-2

I think you are going to combine different arrays, So please use array merge.

   $array0 = Array
            (
                "ZipName" =>  data.zip,
                "Age" => 45
            );
   $array1 = Array
            (
                "ZipName" =>  data.zip,
                "Name" => John
            );
   $array2 = Array
            (
                "ZipName" =>  data.zip
            );
   $array3 = Array
            (
                "ZipName" =>  data.zip,
                "City" => Leicester 
            );

  print_r(array_merge($array0, $array1, $array2, $array3));

Out Put:

Array ( [ZipName] => datazip 
        [Age] => 45 
        [Name] => John 
        [City] => Leicester )
Buddhika
  • 163
  • 1
  • 7
  • 1
    Yeah I got something like but what I want is to combine it using the ZipName value ! – Kadi Jun 18 '20 at 12:12