0

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
)
1
  • You kept the same variable as iterator, $i in both outer and inner for loop. In the inner loop, it increases $i at a point it reached the max value of the outer one ($i <= 1). This won't fix your whole code, but this is a good start Commented May 28, 2020 at 7:00

2 Answers 2

3

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.

Sign up to request clarification or add additional context in comments.

1 Comment

It is totally fine that the result stays as array, since I plan to iterated over the data in later stage.
2
$new_array=[];
foreach($array as $category => $tmp ){

  foreach($tmp as $key => $value){
    $exp = explode('_', $key);
    $exp[] = $value;
    $new_array[ $category ][] = $exp;
    }

}

4 Comments

Posting code without any explanations isn't welcome and doesn't help at all. "Give a man a program, he will suffer one day. Teach him how to program and he will suffer everyday"
@Michel Your suggestion works fine. Please update the answer with some explanation and I will go ahead and approve the answer. For more people to understand your answer and logic I think also you could slightly expand the name convention used, e.g. $exp
Upvoting code-only answers sends a bad message.
Sorry guys, typed the code and had to go in a hurry. Will update today.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.