Home > Back-end >  PHP - Need help to handle array output
PHP - Need help to handle array output

Time:06-03

I need some help figuring out how to get the array looking how I want it.It's basically a list of services that my client delivers and they want to have all clients under the services. I have an array looking like this:

Array
(
    [0] => Array
        (
            [service] => Venturi flowmeters
            [name] => Gina Krogh - Flow Packages
        )

    [1] => Array
        (
            [service] => Thermowells
            [name] => Gina Krogh - Flow Packages
        )

    [2] => Array
        (
            [service] => Flow orifices
            [name] => Gina Krogh - Flow Packages
        )

    [3] => Array
        (
            [service] => Instrument Enclosure Systems
            [name] => Gina Krogh - Flow Packages
        )

    [4] => Array
        (
            [service] => Transmitters
            [name] => Gina Krogh - Flow Packages
        )

    [5] => Array
        (
            [service] => Flow orifices
            [name] => Aasta Hansteen
        )

    [6] => Array
        (
            [service] => Restriction orifices
            [name] => Aasta Hansteen
        )

    [7] => Array
        (
            [service] => Temperatur sensors 
            [name] => Aasta Hansteen
        )

    [8] => Array
        (
            [service] => Thermowells 
            [name] => Aasta Hansteen
        )

    [9] => Array
        (
            [service] => PG/ TG 
            [name] => Aasta Hansteen
        )

    [10] => Array
        (
            [service] => Venturi tubes 
            [name] => Aasta Hansteen
        )

    [11] => Array
        (
            [service] => Chemical injection Flowmeters 
            [name] => Aasta Hansteen
        )

    [12] => Array
        (
            [service] => Ultrasonic flowmeters
            [name] => Johan Sverdrup
        )

    [13] => Array
        (
            [service] => Venturi flowmeters
            [name] => Johan Sverdrup
        )

    [14] => Array
        (
            [service] => Instrument weather enclosure for meetering
            [name] => Johan Sverdrup
        )

    [15] => Array
        (
            [service] => Temperatur sensors
            [name] => Johan Sverdrup
        )

    [16] => Array
        (
            [service] => Thermowells
            [name] => Johan Sverdrup
        )

)

What I want it to look like is this:

Array
(
    [0] => Array
        (
            [service] => Venturi flowmeters
            [name] => Gina Krogh - Flow Packages
            [name] => Johan Sverdrup
        )

    [1] => Array
        (
            [service] => Thermowells
            [name] => Gina Krogh - Flow Packages
            [name] => Aasta Hansteen
            [name] => Johan Sverdrup
        )

    [2] => Array
        (
            [service] => Flow orifices
            [name] => Gina Krogh - Flow Packages
            [name] => Aasta Hansteen
        )

    [3] => Array
        (
            [service] => Instrument Enclosure Systems
            [name] => Gina Krogh - Flow Packages
        )

    [4] => Array
        (
            [service] => Transmitters
            [name] => Gina Krogh - Flow Packages
        )


    [5] => Array
        (
            [service] => Restriction orifices
            [name] => Aasta Hansteen
        )

    [6] => Array
        (
            [service] => Temperature sensors 
            [name] => Aasta Hansteen
            [name] => Johan Sverdrup
        )


    [7] => Array
        (
            [service] => PG/ TG 
            [name] => Aasta Hansteen
        )

    [8] => Array
        (
            [service] => Venturi tubes 
            [name] => Aasta Hansteen
        )

    [9] => Array
        (
            [service] => Chemical injection Flowmeters 
            [name] => Aasta Hansteen
        )

    [10] => Array
        (
            [service] => Ultrasonic flowmeters
            [name] => Johan Sverdrup
        )

    [1] => Array
        (
            [service] => Instrument weather enclosure for meetering
            [name] => Johan Sverdrup
        )
)

And I am stuck on how I can do this so any help is appreciated :)

CodePudding user response:

This is impossible. You simply cannot have multiple elements inside an array that share the same key. For obvious reasons. What you can do instead is use an embedded array to hold all those colliding entries:

<?php
$input = [
    [
        'service' => "Venturi flowmeters",
        'name' => "Gina Krogh - Flow Packages"
    ],
    [
        'service' => "Thermowells",
        'name' => "Gina Krogh - Flow Packages"
    ],
    [
        'service' => "Flow orifices",
        'name' => "Gina Krogh - Flow Packages"
    ],
    [
        'service' => "Instrument Enclosure Systems",
        'name' => "Gina Krogh - Flow Packages"
    ],
    [
        'service' => "Transmitters",
        'name' => "Gina Krogh - Flow Packages"
    ],
    [
        'service' => "Flow orifices",
        'name' => "Aasta Hansteen"
    ],
    [
        'service' => "Restriction orifices",
        'name' => "Aasta Hansteen"
    ],
    [
        'service' => "Temperatur sensors ",
        'name' => "Aasta Hansteen"
    ],
    [
        'service' => "Thermowells ",
        'name' => "Aasta Hansteen"
    ],
    [
        'service' => "PG/ TG ",
        'name' => "Aasta Hansteen"
    ],
    [
        'service' => "Venturi tubes ",
        'name' => "Aasta Hansteen"
    ],
    [
        'service' => "Chemical injection Flowmeters ",
        'name' => "Aasta Hansteen"
    ],
    [
        'service' => "Ultrasonic flowmeters",
        'name' => "Johan Sverdrup"
    ],
    [
        'service' => "Venturi flowmeters",
        'name' => "Johan Sverdrup"
    ],
    [
        'service' => "Instrument weather enclosure for meetering",
        'name' => "Johan Sverdrup"
    ],
    [
        'service' => "Temperatur sensors",
        'name' => "Johan Sverdrup"
    ],
    [
        'service' => "Thermowells",
        'name' => "Johan Sverdrup"
    ],
];

$output = [];
array_walk($input, function($entry) use (&$output) {
    $service = trim($entry['service']);
    $name = trim($entry['name']);
    if (array_key_exists($service, $output)) {
        $output[$service]['name'][] = $name;
    } else {
        $output[$service] = [
            'service' => $service,
            'name' => [
                $name
            ]
        ];
    }
});

print_r($output);

The output is:

Array
(
    [Venturi flowmeters] => Array
        (
            [service] => Venturi flowmeters
            [name] => Array
                (
                    [0] => Gina Krogh - Flow Packages
                    [1] => Johan Sverdrup
                )

        )

    [Thermowells] => Array
        (
            [service] => Thermowells
            [name] => Array
                (
                    [0] => Gina Krogh - Flow Packages
                    [1] => Aasta Hansteen
                    [2] => Johan Sverdrup
                )

        )

    [Flow orifices] => Array
        (
            [service] => Flow orifices
            [name] => Array
                (
                    [0] => Gina Krogh - Flow Packages
                    [1] => Aasta Hansteen
                )

        )

    [Instrument Enclosure Systems] => Array
        (
            [service] => Instrument Enclosure Systems
            [name] => Array
                (
                    [0] => Gina Krogh - Flow Packages
                )

        )

    [Transmitters] => Array
        (
            [service] => Transmitters
            [name] => Array
                (
                    [0] => Gina Krogh - Flow Packages
                )

        )

    [Restriction orifices] => Array
        (
            [service] => Restriction orifices
            [name] => Array
                (
                    [0] => Aasta Hansteen
                )

        )

    [Temperatur sensors] => Array
        (
            [service] => Temperatur sensors
            [name] => Array
                (
                    [0] => Aasta Hansteen
                    [1] => Johan Sverdrup
                )

        )

    [PG/ TG] => Array
        (
            [service] => PG/ TG
            [name] => Array
                (
                    [0] => Aasta Hansteen
                )

        )

    [Venturi tubes] => Array
        (
            [service] => Venturi tubes
            [name] => Array
                (
                    [0] => Aasta Hansteen
                )

        )

    [Chemical injection Flowmeters] => Array
        (
            [service] => Chemical injection Flowmeters
            [name] => Array
                (
                    [0] => Aasta Hansteen
                )

        )

    [Ultrasonic flowmeters] => Array
        (
            [service] => Ultrasonic flowmeters
            [name] => Array
                (
                    [0] => Johan Sverdrup
                )

        )

    [Instrument weather enclosure for meetering] => Array
        (
            [service] => Instrument weather enclosure for meetering
            [name] => Array
                (
                    [0] => Johan Sverdrup
                )

        )

)

CodePudding user response:

As @arkascha has stated, the output you want is not possible. Here is an alternative approach to @arkascha's solution:

$input = [
    [ 'service' => 'Venturi flowmeters', 'name' => 'Gina Krogh - Flow Packages' ],
    [ 'service' => 'Thermowells', 'name' => 'Gina Krogh - Flow Packages' ],
    [ 'service' => 'Flow orifices', 'name' => 'Gina Krogh - Flow Packages' ],
    [ 'service' => 'Instrument Enclosure Systems', 'name' => 'Gina Krogh - Flow Packages' ],
    [ 'service' => 'Transmitters', 'name' => 'Gina Krogh - Flow Packages' ],
    [ 'service' => 'Flow orifices', 'name' => 'Aasta Hansteen' ],
    [ 'service' => 'Restriction orifices', 'name' => 'Aasta Hansteen' ],
    [ 'service' => 'Temperatur sensors', 'name' => 'Aasta Hansteen' ],
    [ 'service' => 'Thermowells', 'name' => 'Aasta Hansteen' ],
    [ 'service' => 'PG/ TG', 'name' => 'Aasta Hansteen' ],
    [ 'service' => 'Venturi tubes', 'name' => 'Aasta Hansteen' ],
    [ 'service' => 'Chemical injection Flowmeters', 'name' => 'Aasta Hansteen' ],
    [ 'service' => 'Ultrasonic flowmeters', 'name' => 'Johan Sverdrup' ],
    [ 'service' => 'Venturi flowmeters', 'name' => 'Johan Sverdrup' ],
    [ 'service' => 'Instrument weather enclosure for meetering', 'name' => 'Johan Sverdrup' ],
    [ 'service' => 'Temperatur sensors', 'name' => 'Johan Sverdrup' ],
    [ 'service' => 'Thermowells', 'name' => 'Johan Sverdrup' ],
];

$result = [];
foreach ($input as $item) {
  $result[trim($item['service'])][] = $item['name'];
}
print_r($result);

$thermowells = $result['Thermowells'];
echo "Names for service 'Thermowells': \n";
print_r($thermowells);

Output:

Array
(
    [Venturi flowmeters] => Array
        (
            [0] => Gina Krogh - Flow Packages
            [1] => Johan Sverdrup
        )

    [Thermowells] => Array
        (
            [0] => Gina Krogh - Flow Packages
            [1] => Aasta Hansteen
            [2] => Johan Sverdrup
        )

    [Flow orifices] => Array
        ...
)
Names for service 'Thermowells': 
Array
(
    [0] => Gina Krogh - Flow Packages
    [1] => Aasta Hansteen
    [2] => Johan Sverdrup
)

    
  • Related