flattern array

    0

    5

    jvit
    $list = [
      'xxx' => [
        'a' => 'A',
        'b' => 'B',
        'c' => 'C'
      ]
    ];
    
    => array(3) {
      ["xxx.a"]=>
      string(1) "A"
      ["xxx.b"]=>
      string(1) "B"
      ["xxx.c"]=>
      string(1) "C"
    }
    
    

    Shortcut: arr.flattern

    /**
    * convert deep array to flattern mode
    *
    */
    function flattenArray(array $array, string $prefix = "", string $seperator = "."): array
    {
      $result = [];
      foreach ($array as $key => $value) {
        $key = rtrim($key, $seperator);
    
        if (!is_array($value)) {
          $result[$prefix . $key] = $value;
          continue;
        }
    
        $result = array_merge($result, flattenArray($value, $prefix . $key . $seperator));
      }
      ksort($result);
      return $result;
    }
    Codiga Logo
    Codiga Hub
    • Rulesets
    • Explore
    • Cookbooks
    • Playground
    soc-2 icon

    We are SOC-2 Compliance Certified

    G2 high performer medal

    Codiga – All rights reserved 2022.