Home > OS >  Filtering and mapping a multidimensional PHP array with a specific structure
Filtering and mapping a multidimensional PHP array with a specific structure

Time:07-13

I am looking for a way to filter a PHP multidimensional array (it is a table). The array looks similar to this:

array (
  0 => 
  array (
    'Standort' => '',
    'Letzte Meldung' => '',
    'On-/Offline seit' => '04.05.2022 11:03',
    'Online' => '',
  ),
  1 => 
  array (
    'Standort' => 'Schweiz',
    'Letzte Meldung' => '',
    'On-/Offline seit' => '11.02.2022 14:59',
    'Online' => '',
  ),
)

The keys inside the array are identical each time. My goal is to loop through each sub array and remove any key that is empty in each sub array. I have tried it with this:

$table_row_array = array_filter(array_map('array_filter', $table_row_array));

however that's not the solution, because then sometimes my tablerows just don't have a matching key.

The expected result would be:

array (
  0 => 
  array (
    'Standort' => '',
    'On-/Offline seit' => '04.05.2022 11:03',
  ),
  1 => 
  array (
    'Standort' => 'Schweiz',
    'On-/Offline seit' => '11.02.2022 14:59',
  ),
)

CodePudding user response:

Loop over the keys of your first array element. For each key, use array_values to extract all the values under that key, make them unique, and then check if there is only one left, and it is an empty string - if so, you have found a key that needs to be removed.
Afterwards, loop over the full array, and unset those keys from the individual items.

$data = array (
  0 => 
  array (
    'Standort' => '',
    'Letzte Meldung' => '',
    'On-/Offline seit' => '04.05.2022 11:03',
    'Online' => '',
  ),
  1 => 
  array (
    'Standort' => 'Schweiz',
    'Letzte Meldung' => '',
    'On-/Offline seit' => '11.02.2022 14:59',
    'Online' => '',
  ),
);

$emptyKeys = [];
foreach(array_keys($data[0]) as $key) {
    $uniqueValues = array_unique(array_column($data, $key));
    if(count($uniqueValues) == 1 && $uniqueValues[0] === '') {
        $emptyKeys[] = $key;
    }
}
foreach($emptyKeys as $emptyKey) {
    foreach($data as &$item) { // pass $item by reference here, so that we
                               // manipulate the original array, and not just a copy
        unset($item[$emptyKey]); // remove the key
    }
    unset($item); // unset the reference itself
}

var_dump($data);

You can of course also nest the loops, and do away with the extra $emptyKeys array:

foreach(array_keys($data[0]) as $key) {
    $uniqueValues = array_unique(array_column($data, $key));
    if(count($uniqueValues) == 1 && $uniqueValues[0] === '') {
        foreach($data as &$item) {
            unset($item[$key]);
        }
        unset($item);
    }
}

CodePudding user response:

switch keys and values with array_flip, unset empty one, will only work with different values in one array as similar values will be overwritten:

$laData = array (
  0 => 
  array (
    'Standort' => '',
    'Letzte Meldung' => '',
    'On-/Offline seit' => '04.05.2022 11:03',
    'Online' => '',
  ),
  1 => 
  array (
    'Standort' => 'Schweiz',
    'Letzte Meldung' => '',
    'On-/Offline seit' => '11.02.2022 14:59',
    'Online' => '',
  ),
);

$laResult = array();
foreach($laData as $laSingleData) {
    $laSingleData = array_flip($laSingleData);
    unset($laSingleData['']);
    $laSingleData = array_flip($laSingleData);
    $laResult[] = $laSingleData;
}

print_r($laResult);

CodePudding user response:

$input = [
    [
        'Standort'         => '',
        'Letzte Meldung'   => '',
        'On-/Offline seit' => '04.05.2022 11:03',
        'Online'           => '',
    ],
    [
        'Standort'         => 'Schweiz',
        'Letzte Meldung'   => '',
        'On-/Offline seit' => '11.02.2022 14:59',
        'Online'           => '',
    ]
];

$keys = array_filter(
    array_keys($input[0]),
    fn($key) => array_filter(array_column($input, $key), fn($value) => !empty($value))
);

$result = array_map(
    fn($item) => array_values(array_intersect_key($item, array_flip($keys))),
    $input
);

print_r($result);

Output:

Array
(
    [0] => Array
        (
            [0] => 
            [1] => 04.05.2022 11:03
        )

    [1] => Array
        (
            [0] => Schweiz
            [1] => 11.02.2022 14:59
        )

)
  • Related