1

I have such array and I need to convert it:

     Array
    (
        [0] => Array
            (
                [0_mass] => 0.00
                [0_arm] => 287.02
                [0_max_limit] => 0.00
                [0_is_dom] => 0
            )
    
        [1] => Array
            (
                [1_mass] => 0.00
                [1_arm] => 269.24
                [1_max_limit] => 0.00
                [1_is_dom] => 0
            )
)

I need to receive the next one: So basically I need to delete "0_" these parts from each key. but it can be even "1000_". Only I think about is cutting string but it bad approach

  Array
    (
        [0] => Array
            (
                [mass] => 0.00
                [arm] => 287.02
                [max_limit] => 0.00
                [is_dom] => 0
            )

        [1] => Array
            (
                [mass] => 0.00
                [arm] => 269.24
                [max_limit] => 0.00
                [is_dom] => 0
            )
)

Thanks!

Mr lucky
  • 11
  • 2

1 Answers1

1

You can use next approach:

$keys = array_map(function ($el) {
    $split = explode("_", $el);
    array_shift($split);
    return implode("_", $split);
}, array_keys($arr[0]));

//var_export($keys);

$res = array_map(function ($el) use ($keys) {
    return array_combine($keys, array_values($el));
}, $arr);

Test PHP code online

P.S. The above code works only in case all sub-arrays have same keys (except preifx)

Slava Rozhnev
  • 9,510
  • 6
  • 23
  • 39