How to dynamically set array keys in php

前端 未结 6 2468
星月不相逢
星月不相逢 2021-02-20 18:48

I have some logic that is being used to sort data but depending on the user input the data is grouped differently. Right now I have five different functions that contain the sa

6条回答
  •  醉梦人生
    2021-02-20 19:31

    You can use this library to get or set value in multidimensional array using array of keys:

    Arr::getNestedElement($calcs, [
        $meter['Resource']['name'], 
        $meter['UnitType']['name'], 
        date('Y-m', $start)
    ]);
    

    to get value or:

    Arr::handleNestedElement($calcs, [
        $meter['Resource']['name'], 
        $meter['UnitType']['name'], 
        date('Y-m', $start)
    ], $data);
    

    to set $data as value.

提交回复
热议问题