'Transform array, set each array element with parent key php

I am receiving data that is an array of elements that contains an array of tags by language like this

[
  {
    "1": "tag_es1;tag_es2;tag_es3",
    "2": "tag_en1;tag_en2;tag_en3"
  },
  {
    "1": "tag_es1;tag_es2",
    "2": "tag_en1;tag_en2"
  }
]

I need to separate each tag by language, so i usearray_map to transform it like this

[
  {
    "1": [
      "tag_es1",
      "tag_es2",
      "tag_es3"
    ],
    "2": [
      "tag_en1",
      "tag_en2",
      "tag_en3"
    ]
  },
  {
    "1": [
      "tag_es1",
      "tag_es2"
    ],
    "2": [
      "tag_en1",
      "tag_en2"
    ]
  }
]

Bu what i need is the response to be like this

[
  {
    {
      "1" : "tag_es1",
      "2" : "tag_en1"
    },
    {
      "1" : "tag_es2",
      "2" : "tag_en2"
    },
    {
      "1" : "tag_es3",
      "2" : "tag_en3"
    }
  },
  {
    {
      "1" : "tag_es4",
      "2" : "tag_en4"
    },
    {
      "1" : "tag_es5",
      "2" : "tag_en5"
    }

  }
]

I tried using array_combine, array_walk, and manually doing it inside array_map, but with no success, what could i do?



Solution 1:[1]

Solution with special trick with null as callback of array_map:

$arr = json_decode($s, true);
$new_arr = [];

foreach ($arr as $item) {
    $parts1 = explode(';', $item[1]);
    $parts2 = explode(';', $item[2]);

    // $new_arr[] = array_map(null, $parts1, $parts2);

    $tmp_arr = array_map(null, $parts1, $parts2);
    $new_arr[] = array_map(
        function($v) { return array_combine(["1","2"], $v); }, 
        $tmp_arr
    );
}

Solution 2:[2]

You can loop the array and build a temporary array.
This array can then be looped and used array_column on to get the corresponding values to the new array.

$arr = json_decode($json, true);
foreach($arr as $key1 => $sub){
    foreach($sub as $item){
        $temp[] = explode(";", $item);
    }
    foreach($temp[0] as $key2 => $val){
        $new[$key1][]= array_combine([1,2],array_column($temp, $key2));
    }
    $temp =[]; // empty array
}
var_dump($new);

Output:

array(2) {
  [0]=>
  array(3) {
    [0]=>
    array(2) {
      [1]=>
      string(7) "tag_es1"
      [2]=>
      string(7) "tag_en1"
    }
    [1]=>
    array(2) {
      [1]=>
      string(7) "tag_es2"
      [2]=>
      string(7) "tag_en2"
    }
    [2]=>
    array(2) {
      [1]=>
      string(7) "tag_es3"
      [2]=>
      string(7) "tag_en3"
    }
  }
  [1]=>
  array(2) {
    [0]=>
    array(2) {
      [1]=>
      string(7) "tag_es1"
      [2]=>
      string(7) "tag_en1"
    }
    [1]=>
    array(2) {
      [1]=>
      string(7) "tag_es2"
      [2]=>
      string(7) "tag_en2"
    }
  }
}

https://3v4l.org/qgCA1

Added "1","2" as keys

Solution 3:[3]

The leanest, cleanest approach is three nested foreach loops.

Assign the dynamic keys when pushing data in the result array.

Code: (Demo)

$result = [];
foreach (json_decode($json, true) as $i => $row) {
    foreach ($row as $id => $delimited) {
        foreach (explode(';', $delimited) as $key => $value) {
            $result[$i][$key][$id] = $value;
        }
    }
}
var_export($result);

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1
Solution 2
Solution 3 mickmackusa