Search code examples
phparraysmultidimensional-arrayphp-7.3

Explode data and build new array


I need to explode the key values of an array and build a new array which has the exploded result. Below code works with one sub-array and I think I am missing a for loop to take care of the array values iteration.

The solution should also process the 'finance' subarray data, to be exploded and visible in the new array.

I would have 9 sub-arrays in later stage, thus the reason of needing to explode the data and move result into new array.

My code

<?php

$array = [
  'company_info' => [
    'country_period_0'  => 10,
    'currency_period_0' => 20
  ],
  'finance'      => [
    'values_period_0' => 30
  ]
];

$newArray = [];

for ($i=0; $i <= 1 ; $i++) {

  $array_1     = $array['company_info'];
  $arrayKeys   = array_keys($array_1);
  $arrayValues = array_values($array_1);

  $keySplits   = explode("_", $arrayKeys[$i]);

  for ($i=0; $i <= 2 ; $i++) {
    $newArray[] = $keySplits[$i];
  }
    $newArray[3] = $arrayValues[0];

}

print_r($newArray);

Result

Array(
    [0] => country
    [1] => period
    [2] => 0
    [3] => 10
)

Wanted result

['company_info]
Array(
    [0] => country
    [1] => period
    [2] => 0
    [3] => 10
)
Array(
    [0] => currency
    [1] => period
    [2] => 0
    [3] => 20
)
['finance']
Array(
    [0] => values
    [1] => period
    [2] => 0
    [3] => 30
)

Solution

  • You can simplify it a lot using foreach loops, especially fetching the key each time along with the value to help with the array building.

    This also uses explode() and adds the result to the $newArray with the key from the first level using $newArray[$mainKey][], but also just adds the value to the end of the array using []...

    foreach ( $array as $mainKey => $elements )  {
        foreach ( $elements as $subKey => $value ){
            $newData = explode("_", $subKey);
            $newData[] = $value;
            $newArray[$mainKey][] = $newData;
        }
    }
    

    with your test data gives...

    Array
    (
        [company_info] => Array
            (
                [0] => Array
                    (
                        [0] => country
                        [1] => period
                        [2] => 0
                        [3] => 10
                    )
    
                [1] => Array
                    (
                        [0] => currency
                        [1] => period
                        [2] => 0
                        [3] => 20
                    )
    
            )
    
        [finance] => Array
            (
                [0] => Array
                    (
                        [0] => values
                        [1] => period
                        [2] => 0
                        [3] => 30
                    )
    
            )
    
    )
    

    I just noticed that I was missing the second company_info data, so this means the values will always be arrays, unless you really need them only to be arrays when needed.