0

I have multiple arrays which I'm trying to explode and divide all information inside. For some reason, I'm losing the information about main arrays when I do that.

Arrays

[0] => Array (
    [0] => 2020-11-01 / 2020-11-30
    [1] => 2020-12-01 / 2020-12-31
    )
[1] => Array (
    [0] => 2020-11-01 / 2020-11-30
    [1] => 2020-12-01 / 2020-12-31
    )

I used the following code:

foreach ($list as $line) {
    foreach ($line as $text) {
        $parts[] = explode(' / ', $text);
    }
}

print_r($parts) will return the following:

[0] => Array (
    [0] => 2020-11-01
    [1] => 2020-11-30
    )
[1] => Array (
    [0] => 2020-12-01
    [1] => 2020-12-31
    )
[2] => Array (
    [0] => 2020-11-01
    [1] => 2020-11-30
    )
[3] => Array (
    [0] => 2020-12-01
    [1] => 2020-12-31
    )

but this is unfortunately not what I'm looking for, what I need is this:

[0] => Array (
    [0] => Array (
        [0] => 2020-11-01
        [1] => 2020-11-30
        )
    [1] => Array (
        [0] => 2020-12-01
        [1] => 2020-12-31
        )
    )
[1] => Array (
    [0] => Array (
        [0] => 2020-11-01
        [1] => 2020-11-30
        )
    [1] => Array (
        [0] => 2020-12-01
        [1] => 2020-12-31
        )
    )

I searched all around but did not find an easy way for that. hope somebody can help. thank you!

2
  • what is $z variable? Commented Dec 19, 2020 at 22:50
  • that was a mistake, sorry. already fixed :) Commented Dec 19, 2020 at 22:50

3 Answers 3

2

You are creating a new array instead of writing in the same indices that you are exploding, which you would do like this:

$arr = [
    0 => [
        0 => '2020-11-01 / 2020-11-30',
        1 => '2020-12-01 / 2020-12-31',
    ],
    1 => [
        0 => '2020-11-01 / 2020-11-30',
        1 => '2020-12-01 / 2020-12-31',
    ]
];


foreach ($arr as $bulkIndex => $bulks) {
    foreach ($bulks as $lineIndex => $line) {
        $arr[$bulkIndex][$lineIndex] = explode(' / ', $line);
    }
}
Sign up to request clarification or add additional context in comments.

Comments

1

You can achieve the result you want with a nested array_map to explode the data in each value:

$data = array(
  array(
    "2020-11-01 / 2020-11-30",
    "2020-11-01 / 2020-11-30",
  ),
  array(
    "2020-11-01 / 2020-11-30",
    "2020-11-01 / 2020-11-30",
  )
);

$result = array_map(function ($a) { 
    return array_map(function ($v) { 
        return explode(' / ', $v ); 
    }, $a); 
}, $data);
print_r($result);

Comments

0

If you really want keep your original array intact. A solution is use another array to put your exploded array inside:

<?php

$list = [
  [
    "2020-11-01 / 2020-11-30",
    "2020-11-01 / 2020-11-30",
  ],
  [
    "2020-11-01 / 2020-11-30",
    "2020-11-01 / 2020-11-30",
  ]
];

$newArray = [];
foreach ($list as $line) {
  $parts = [];
  foreach ($line as $text) {
      $parts[] = explode(' / ', $text);
  }
  $newArray[] = $parts;
}
var_dump($newArray);

/*
array (size=2)
  0 => 
    array (size=2)
      0 => 
        array (size=2)
          0 => string '2020-11-01' (length=10)
          1 => string '2020-11-30' (length=10)
      1 => 
        array (size=2)
          0 => string '2020-11-01' (length=10)
          1 => string '2020-11-30' (length=10)
  1 => 
    array (size=2)
      0 => 
        array (size=2)
          0 => string '2020-11-01' (length=10)
          1 => string '2020-11-30' (length=10)
      1 => 
        array (size=2)
          0 => string '2020-11-01' (length=10)
          1 => string '2020-11-30' (length=10)
*/

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.