Home > Enterprise >  Reduce/Flatten Array with PHP
Reduce/Flatten Array with PHP

Time:09-01

I would like to remove some elements from this array and reduce the nesting. What is the best way to do this? But unfortunately I lack the experience with. My attempts have all been unsuccessful so far.

Only the data [text] should be included at the end, I don't need the other data and would like to remove it....

Array
(
    [0] => Array
        (
            [extraction_method] => lattice
            [top] => 10
            [left] => 20
            [width] => 20
            [height] => 10
            [data] => Array
                (
                    [0] => Array
                        (
                            [0] => Array
                                (
                                    [top] => 0
                                    [left] => 0
                                    [width] => 0
                                    [height] => 0
                                    [text] => 
                                )

                            [1] => Array
                                (
                                    [top] => 10
                                    [left] => 10
                                    [width] => 20
                                    [height] => 20
                                    [text] => Monday
                                )

                            [2] => Array
                                (
                                    [top] => 10
                                    [left] => 20
                                    [width] => 120
                                    [height] => 20
                                    [text] => Thuesday
                                )

                            [3] => Array
                                (
                                    [top] => 10
                                    [left] => 393.5906
                                    [width] => 129.60000610352
                                    [height] => 20
                                    [text] => Wednesday
                                )



                        )

                    [1] => Array
                        (
                            [0] => Array
                                (
                                    [top] => 165.02405
                                    [left] => 66.83375
                                    [width] => 71.240821838379
                                    [height] => 31.655975341797
                                    [text] => Starter
                                )

                            [1] => Array
                                (
                                    [top] => 165.02405
                                    [left] => 10
                                    [width] => 20
                                    [height] => 31.655975341797
                                    [text] =>Soup
                                )

                            [2] => Array
                                (
                                    [top] => 165.02405
                                    [left] => 20
                                    [width] => 120
                                    [height] => 31.655975341797
                                    [text] => Salad
                                )

                            [3] => Array
                                (
                                    [top] => 165.02405
                                    [left] => 393.5906
                                    [width] => 129.60000610352
                                    [height] => 31.655975341797
                                    [text] => Pizza
                                )


                        )

                    [2] => Array
                        (
                            [0] => Array
                                (
                                    [top] => 196.68002
                                    [left] => 66.83375
                                    [width] => 71.240821838379
                                    [height] => 31.139999389648
                                    [text] => aaaaaa
                                )

                            [1] => Array
                                (
                                    [top] => 196.68002
                                    [left] => 10
                                    [width] => 20
                                    [height] => 31.139999389648
                                    [text] => bbbbb
                                )

                            [2] => Array
                                (
                                    [top] => 196.68002
                                    [left] => 20
                                    [width] => 120
                                    [height] => 31.139999389648
                                    [text] => ccccc
                                )

                            [3] => Array
                                (
                                    [top] => 196.68002
                                    [left] => 393.5906
                                    [width] => 129.60000610352
                                    [height] => 31.139999389648
                                    [text] =>ddddd
                                )



                        )

                    [3] => Array
                        (
                            [0] => Array
                                (
                                    [top] => 227.82002
                                    [left] => 66.83375
                                    [width] => 71.240821838379
                                    [height] => 31.679977416992
                                    [text] => ggg
                                )

                            [1] => Array
                                (
                                    [top] => 227.82002
                                    [left] => 10
                                    [width] => 20
                                    [height] => 31.679977416992
                                    [text] => hhh

                                )

                            [2] => Array
                                (
                                    [top] => 227.82002
                                    [left] => 20
                                    [width] => 120
                                    [height] => 31.679977416992
                                    [text] => iiii
                                )

                            [3] => Array
                                (
                                    [top] => 227.82002
                                    [left] => 393.5906
                                    [width] => 129.60000610352
                                    [height] => 31.679977416992
                                    [text] => jjjjj
                                )


          

                        )

                    [4] => Array
                        (
                            [0] => Array
                                (
                                    [top] => 259.5
                                    [left] => 66.83375
                                    [width] => 71.240821838379
                                    [height] => 41.579986572266
                                    [text] => mmmmm
                                )

                            [1] => Array
                                (
                                    [top] => 259.5
                                    [left] => 10
                                    [width] => 20
                                    [height] => 41.579986572266
                                    [text] => nnnn

                                )

                            [2] => Array
                                (
                                    [top] => 259.5
                                    [left] => 20
                                    [width] => 120
                                    [height] => 41.579986572266
                                    [text] => ooooo
                                )

                            [3] => Array
                                (
                                    [top] => 259.5
                                    [left] => 393.5906
                                    [width] => 129.60000610352
                                    [height] => 41.579986572266
                                    [text] => pppp

                                )

            

               

                        )

                    [5] => Array
                        (
                            [0] => Array
                                (
                                    [top] => 301.08
                                    [left] => 66.83375
                                    [width] => 71.240821838379
                                    [height] => 43.739990234375
                                    [text] => xxxxxxx
                                )

                            [1] => Array
                                (
                                    [top] => 301.08
                                    [left] => 10
                                    [width] => 20
                                    [height] => 43.739990234375
                                    [text] => xxxxxxx

                            [2] => Array
                                (
                                    [top] => 301.08
                                    [left] => 20
                                    [width] => 120
                                    [height] => 43.739990234375
                                    [text] => xxxxxxx
                                )

                            [3] => Array
                                (
                                    [top] => 301.08
                                    [left] => 393.5906
                                    [width] => 129.60000610352
                                    [height] => 43.739990234375
                                    [text] => xxxxxxx
                                )


               

                        )

                    [6] => Array
                        (
                            [0] => Array
                                (
                                    [top] => 344.81998
                                    [left] => 66.83375
                                    [width] => 71.240821838379
                                    [height] => 53.055053710938
                                    [text] => ttttt
                                )

                            [1] => Array
                                (
                                    [top] => 344.81998
                                    [left] => 10
                                    [width] => 20
                                    [height] => 53.055053710938
                                    [text] => sssssss
                                )

                            [2] => Array
                                (
                                    [top] => 344.81998
                                    [left] => 20
                                    [width] => 120
                                    [height] => 53.055053710938
                                    [text] =>uuuu
                                )

                            [3] => Array
                                (
                                    [top] => 344.81998
                                    [left] => 393.5906
                                    [width] => 129.60000610352
                                    [height] => 53.055053710938
                                    [text] => vvvv
                                )



                        )

                    [7] => Array
                        (
                            [0] => Array
                                (
                                    [top] => 397.87503
                                    [left] => 66.83375
                                    [width] => 71.240821838379
                                    [height] => 56.557312011719
                                    [text] => zzzzzzz
                                )

                            [1] => Array
                                (
                                    [top] => 397.87503
                                    [left] => 10
                                    [width] => 20
                                    [height] => 56.557312011719
                                    [text] => hzhzhzhzhzh
                                )

                            [2] => Array
                                (
                                    [top] => 397.87503
                                    [left] => 20
                                    [width] => 120
                                    [height] => 56.557312011719
                                    [text] => gtgtgtgtgtgt
                                )

                            [3] => Array
                                (
                                    [top] => 397.87503
                                    [left] => 393.5906
                                    [width] => 129.60000610352
                                    [height] => 56.557312011719
                                    [text] => frfrfrfrf
                                )



                        )

                )

            [spec_index] => 0
        )

)

In the end, it should look like this.

What I need:

Array
(

                    [0] => Array
                        (
                            [0] => Array
                                (

                                    [text] => 
                                )

                            [1] => Array
                                (

                                    [text] => Monday
                                )

                            [2] => Array
                                (

                                    [text] => Thuesday
                                )

                            [3] => Array
                                (

                                    [text] => Wednesday
                                )

                          

                        )

                    [1] => Array
                        (
                            [0] => Array
                                (

                                    [text] => Starter
                                )

                            [1] => Array
                                (

                                    [text] =>Soup
                                )

                            [2] => Array
                                (
                                    [text] => Salad
                                )

                            [3] => Array
                                (

                                    [text] => Pizza
                                )

               

                        )

                    [2] => Array
                        (
                            [0] => Array
                                (

                                    [text] => aaaaaa
                                )

                            [1] => Array
                                (

                                    [text] => bbbbb
                                )

                            [2] => Array
                                (

                                    [text] => ccccc
                                )

                            [3] => Array
                                (

                                    [text] =>ddddd
                                )

                

                        )

                    [3] => Array
                        (
                            [0] => Array
                                (
                                    [text] => ggg
                                )

                            [1] => Array
                                (

                                    [text] => hhh
                                )

                            [2] => Array
                                (

                                    [text] => iiii
                                )

                            [3] => Array
                                (

                                    [text] => jjjjj
                                )

                            [4] => Array
                   

                        )

                    [4] => Array
                        (
                            [0] => Array
                                (

                                    [text] => mmmmm
                                )

                            [1] => Array
                                (

                                    [text] => nnnn

                                )

                            [2] => Array
                                (

                                    [text] => ooooo
                                )

                            [3] => Array
                                (

                                    [text] => pppp
                                )

                    


                        )

                    [5] => Array
                        (
                            [0] => Array
                                (
   
                                    [text] => xxxxxxx
                                )

                            [1] => Array
                                (

                                    [text] => xxxxxxx

                            [2] => Array
                                (

                                    [text] => xxxxxxx
                                )

                            [3] => Array
                                (

                                    [text] => xxxxxxx
                                )

                   


                        )

                    [6] => Array
                        (
                            [0] => Array
                                (

                                    [text] => ttttt
                                )

                            [1] => Array
                                (

                                    [text] => sssssss
                                )

                            [2] => Array
                                (

                                    [text] =>uuuu
                                )

                            [3] => Array
                                (

                                    [text] => vvvv
                                )

                      

                        )

                    [7] => Array
                        (
                            [0] => Array
                                (

                                    [text] => zzzzzzz
                                )

                            [1] => Array
                                (

                                    [text] => hzhzhzhzhzh
                                )

                            [2] => Array
                                (

                                    [text] => gtgtgtgtgtgt
                                )

                            [3] => Array
                                (

                                    [text] => frfrfrfrf
                                )

                  

                        )

                )


        
        )

CodePudding user response:

As suggested, a set of carefully nested foreach loops does the trick.


$array = array (
    0 => 
    array (
      'extraction_method' => 'lattice',
      'top' => 142.5603800000000092040863819420337677001953125,
      'left' => 66.8337499999999948840923025272786617279052734375,
      'width' => 712.9727783203125,
      'height' => 311.8719482421875,
      'data' => 
      array (
        0 => 
        array (
          0 => 
          array (
            'top' => 0.0,
            'left' => 0.0,
            'width' => 0.0,
            'height' => 0.0,
            'text' => '',
          ),

 . . .

        ),
      ),
      'spec_index' => 0,
    ),
);


$newrray = [];
foreach ($array as $a){
    foreach ( $a['data'] as $b) {
        foreach ( $b as $c){
            # lose the empty occurances and remove newlines
            # you dont have to do this it just made the result easier to check
            if ( $c['text'] !== '') {
                $newArray[] = str_replace( "\n", ' ', $c['text']);
            }
        }
     }        
}    
print_r($newArray);

RESULTS

Array
(
    [0] => 29.08.2022   Montag
    [1] => 30.08.2022   Dienstag
    [2] => 31.08.2022   Mittwoch
    [3] => 01.09.2022   Donnerstag
    [4] => 02.09.2022   Freitag
    [5] => Vorspeise oder   Vorsuppe
    [6] => Cevapcici (R) mit Chilidip   Gemüsesuppe
    [7] => Gurken- / Kidneybohnensalat   mit Essig-Öl-Dressing   Hühnernudelsuppe
    [8] => Radieschensalat mit   Frühlingszwiebel und Apfel   Blumenkohlcremesuppe
    [9] => Chinakohl mit Mandarinen-   Joghurtdressing   Minestrone
    [10] => Gnocchi mit Frischkäse-   Kräuterfüllung und Spinatsoße   Erbsencremesuppe
    [11] => Fitnesskost
    [12] => Nudel-Gemüseauflauf mit   Tomatensoße
    [13] => Gekochtes Rindfleisch mit   Meerrettichsoße, Petersilien-   kartoffeln und Wirsinggemüse
    [14] => Burger mit Gemüsebratling
    [15] => Fisch-Kartoffel-Curry mit   Kerbel und Koriander
    [16] => Brokkoli-Haselnuss-Ecken mit   Honig-Senfdip, Sesamkartoffeln   und gedünstetes Gartengemüse
    [17] => Vegetarisches   Gericht
    [18] => Spaghetti mit Soja-   Bolognese-Soße und   Blumenkohlröschen
    [19] => Gemüsestrudel mit   brauner Soße und Reis
    [20] => Schmorgemüse mit Tofu   und Vollkornreis
    [21] => Kohlroulade mit Füllung aus   Soja- und Weizeneiweiß, Braune   Soße und Reis
    [22] => Germknödel gefüllt mit   Pflaumenmus, dazu Vanillesoße
    [23] => Tagesgericht I
    [24] => Schmorsteak (R) mit   Steinpilzrahmsoße,   gebackene Kartoffeln und   dicke Bohnen
    [25] => Gemischtes Gulasch (R/S),   Nudeln " Penne Rigate" und   Pariser Karotten
    [26] => Zwiebelsteak (S) mit brauner   Soße, Salzkartoffeln und   Prinzessbohnen
    [27] => Wurstgulasch mit Geflügellyoner,   Farfalle und Kohlrabigemüse
    [28] => Tagesgericht II
    [29] => Pizza mit Schinken (S)   und Ananas
    [30] => Gegrilltes Zanderfilet mit   Dillsoße und Reis
    [31] => Hähnchenschnitzel "Cordon Bleu"   mit Geflügelsoße, Kartoffelrösti   und Kaisergemüse
    [32] => Lammkeule mit Zwiebeln   und Rosmarin, Farfalle und   Ratatouillegemüse
    [33] => Hähnchenfiletspieß mit scharfer   Tomatensoße und   Kartoffelkroketten
    [34] => Nachspeise
    [35] => Obstsortiment   Quarkspeise mit Waldbeeren   Vanille-Creme mit Apfelmus   Schokoladenpudding
    [36] => Obstsortiment   Helle Creme mit Mango   Fruchtjoghurt   Blaubeermuffin
    [37] => Obstsortiment   Quarkspeise mit Honig   Quark mit Früchten
    [38] => Obstsortiment   Himbeer-Joghurt   Milchreis mit Früchten   Apfelstreuselkuchen
    [39] => Obstsortiment   Kirschjoghurt mit Schokostreuseln   Grießspeise
    [40] => Fitness-   vorschlag   (ca. 700 kcal)
    [41] => Gemüsesuppe   Nudel-Gemüseauflauf mit   Tomatensoße   Vanille-Creme mit Apfelmus
    [42] => Gurken- / Kidneybohnensalat mit   Essig-Öl-Dressing   Gekochtes Rindfleisch mit   Meerrettichsoße, Petersilien-   kartoffeln und Wirsing, Obst
    [43] => Radieschensalat mit   Frühlingszwiebel und Apfel   Burger mit Gemüsebratling   Obstsortiment
    [44] => Chinakohl mit Mandarinen-   Joghurtdressing   Fisch-Kartoffel-Curry mit Kerbel   und Koriander   Himbeer-Joghurt
    [45] => Erbsencremesuppe   Brokkoli-Haselnuss-Ecken mit   Honig-Senfdip, Sesamkartoffeln   und gedünstetes Gartengemüse   Kirschjoghurt mit Schokostreuseln
)

CodePudding user response:

You can get the output you want with the following method

function getColumns(array $data, string $key, bool $keyStatus = false)
{
    return array_map(function ($items) use ($key, $keyStatus) {
        if (is_array($items)) {
            return !$keyStatus
                ? array_column($items, $key)
                : array_map(function ($item) use ($key) {
                    return [$key => $item[$key] ?? ''];
                }, $items);
        }
        return [];
    }, $data);
}

use of

print_r(getColumns($array['data'], 'text', true));

Result

Array
(
    [0] => Array
        (
            [0] => Array
                (
                    [text] => 
                )

            [1] => Array
                (
                    [text] => Monday
                )

            [2] => Array
                (
                    [text] => Thuesday
                )

            [3] => Array
                (
                    [text] => Wednesday
                )

        )

    [1] => Array
        (
            [0] => Array
                (
                    [text] => Starter
                )

            [1] => Array
                (
                    [text] => Soup
                )

            [2] => Array
                (
                    [text] => Salad
                )

            [3] => Array
                (
                    [text] => Pizza
                )

        )
.......

For a more regular output

print_r(getColumns($array['data'], 'text'));

result

Array
(
    [0] => Array
        (
            [0] => 
            [1] => Monday
            [2] => Thuesday
            [3] => Wednesday
        )

    [1] => Array
        (
            [0] => Starter
            [1] => Soup
            [2] => Salad
            [3] => Pizza
        )
.......
  • Related