flatten a multi-directional array keys with delimiters recursively





.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty{ height:90px;width:728px;box-sizing:border-box;
}







2















I am trying to convert a multi-directional to an array of keys with delimiter with a recursive php function. Here is what i have so far. Not sure why its returning partial array.



$array = [
'arr' => 1,
'arr2' => [
'_arr2' => 'test',
'_arr2_2' => 'test 2',
'_arr2_3' => 3,
'_arr2_4' => [
'__arr2' => 'tt',
'__arr2_2' => 'ttww',
'sub-sub-field' => [
'ww' => 1
]
],
'_arr2_5' => [
'__arr2_5' => 'some'
]
],
'arr3' => 'test',
'arr4' => null,
'arr5' => [
'_arr5' => [
'___arr5' => 1
],
'_arr5_1' => null
],
];

function print_array_reccur ($array, $str = '')
{
if (empty($array)) return null;

$arr = ;

foreach ($array as $key => $val)
{
if (is_array($val))
{
if ($str == '')
{
$arr = print_array_reccur($val, $key);
}
else
{
$arr = print_array_reccur($val, $str . '/' . $key);
}
}
else
{
if ($str == '')
{
$arr[$key] = $val;
}
else
{
$arr[$str . '/' . $key] = $val;
}
}
}

return $arr;
}


currently i am getting the below partial array output. Any idea as to why this is happening?



   Array
(
[arr5/_arr5/___arr5] => 1
[arr5/_arr5_1] =>
)









share|improve this question





























    2















    I am trying to convert a multi-directional to an array of keys with delimiter with a recursive php function. Here is what i have so far. Not sure why its returning partial array.



    $array = [
    'arr' => 1,
    'arr2' => [
    '_arr2' => 'test',
    '_arr2_2' => 'test 2',
    '_arr2_3' => 3,
    '_arr2_4' => [
    '__arr2' => 'tt',
    '__arr2_2' => 'ttww',
    'sub-sub-field' => [
    'ww' => 1
    ]
    ],
    '_arr2_5' => [
    '__arr2_5' => 'some'
    ]
    ],
    'arr3' => 'test',
    'arr4' => null,
    'arr5' => [
    '_arr5' => [
    '___arr5' => 1
    ],
    '_arr5_1' => null
    ],
    ];

    function print_array_reccur ($array, $str = '')
    {
    if (empty($array)) return null;

    $arr = ;

    foreach ($array as $key => $val)
    {
    if (is_array($val))
    {
    if ($str == '')
    {
    $arr = print_array_reccur($val, $key);
    }
    else
    {
    $arr = print_array_reccur($val, $str . '/' . $key);
    }
    }
    else
    {
    if ($str == '')
    {
    $arr[$key] = $val;
    }
    else
    {
    $arr[$str . '/' . $key] = $val;
    }
    }
    }

    return $arr;
    }


    currently i am getting the below partial array output. Any idea as to why this is happening?



       Array
    (
    [arr5/_arr5/___arr5] => 1
    [arr5/_arr5_1] =>
    )









    share|improve this question

























      2












      2








      2








      I am trying to convert a multi-directional to an array of keys with delimiter with a recursive php function. Here is what i have so far. Not sure why its returning partial array.



      $array = [
      'arr' => 1,
      'arr2' => [
      '_arr2' => 'test',
      '_arr2_2' => 'test 2',
      '_arr2_3' => 3,
      '_arr2_4' => [
      '__arr2' => 'tt',
      '__arr2_2' => 'ttww',
      'sub-sub-field' => [
      'ww' => 1
      ]
      ],
      '_arr2_5' => [
      '__arr2_5' => 'some'
      ]
      ],
      'arr3' => 'test',
      'arr4' => null,
      'arr5' => [
      '_arr5' => [
      '___arr5' => 1
      ],
      '_arr5_1' => null
      ],
      ];

      function print_array_reccur ($array, $str = '')
      {
      if (empty($array)) return null;

      $arr = ;

      foreach ($array as $key => $val)
      {
      if (is_array($val))
      {
      if ($str == '')
      {
      $arr = print_array_reccur($val, $key);
      }
      else
      {
      $arr = print_array_reccur($val, $str . '/' . $key);
      }
      }
      else
      {
      if ($str == '')
      {
      $arr[$key] = $val;
      }
      else
      {
      $arr[$str . '/' . $key] = $val;
      }
      }
      }

      return $arr;
      }


      currently i am getting the below partial array output. Any idea as to why this is happening?



         Array
      (
      [arr5/_arr5/___arr5] => 1
      [arr5/_arr5_1] =>
      )









      share|improve this question














      I am trying to convert a multi-directional to an array of keys with delimiter with a recursive php function. Here is what i have so far. Not sure why its returning partial array.



      $array = [
      'arr' => 1,
      'arr2' => [
      '_arr2' => 'test',
      '_arr2_2' => 'test 2',
      '_arr2_3' => 3,
      '_arr2_4' => [
      '__arr2' => 'tt',
      '__arr2_2' => 'ttww',
      'sub-sub-field' => [
      'ww' => 1
      ]
      ],
      '_arr2_5' => [
      '__arr2_5' => 'some'
      ]
      ],
      'arr3' => 'test',
      'arr4' => null,
      'arr5' => [
      '_arr5' => [
      '___arr5' => 1
      ],
      '_arr5_1' => null
      ],
      ];

      function print_array_reccur ($array, $str = '')
      {
      if (empty($array)) return null;

      $arr = ;

      foreach ($array as $key => $val)
      {
      if (is_array($val))
      {
      if ($str == '')
      {
      $arr = print_array_reccur($val, $key);
      }
      else
      {
      $arr = print_array_reccur($val, $str . '/' . $key);
      }
      }
      else
      {
      if ($str == '')
      {
      $arr[$key] = $val;
      }
      else
      {
      $arr[$str . '/' . $key] = $val;
      }
      }
      }

      return $arr;
      }


      currently i am getting the below partial array output. Any idea as to why this is happening?



         Array
      (
      [arr5/_arr5/___arr5] => 1
      [arr5/_arr5_1] =>
      )






      php






      share|improve this question













      share|improve this question











      share|improve this question




      share|improve this question










      asked Nov 16 '18 at 19:04









      Julia FoxJulia Fox

      132




      132
























          1 Answer
          1






          active

          oldest

          votes


















          0














          From what I can tell (and with the help of your test case - which always helps :), the problem is that when you were returning the new values from the sub array processing, this was overwriting the existing contents. I've commented the two lines but just that I've used += to add the new data to the existing array data...



          function print_array_reccur ($array, $str = '')
          {
          if (empty($array)) return null;

          $arr = ;

          foreach ($array as $key => $val)
          {
          if (is_array($val))
          {
          if ($str == '')
          {
          // Add new data into the existing array using +=
          $arr += print_array_reccur($val, $key);
          }
          else
          {
          // Add new data into the existing array using +=
          $arr += print_array_reccur($val, $str . '/' . $key);
          }
          }
          else
          {
          if ($str == '')
          {
          $arr[$key] = $val;
          }
          else
          {
          $arr[$str . '/' . $key] = $val;
          }
          }
          }

          return $arr;
          }


          Hopefully the return value is what your after...



          Array
          (
          [arr] => 1
          [arr2/_arr2] => test
          [arr2/_arr2_2] => test 2
          [arr2/_arr2_3] => 3
          [arr2/_arr2_4/__arr2] => tt
          [arr2/_arr2_4/__arr2_2] => ttww
          [arr2/_arr2_4/sub-sub-field/ww] => 1
          [arr2/_arr2_5/__arr2_5] => some
          [arr3] => test
          [arr4] =>
          [arr5/_arr5/___arr5] => 1
          [arr5/_arr5_1] =>
          )





          share|improve this answer
























          • This is exactly what i wanted. I also now realize my mistake. You are correct. Thanks for helping

            – Julia Fox
            Nov 16 '18 at 19:33












          Your Answer






          StackExchange.ifUsing("editor", function () {
          StackExchange.using("externalEditor", function () {
          StackExchange.using("snippets", function () {
          StackExchange.snippets.init();
          });
          });
          }, "code-snippets");

          StackExchange.ready(function() {
          var channelOptions = {
          tags: "".split(" "),
          id: "1"
          };
          initTagRenderer("".split(" "), "".split(" "), channelOptions);

          StackExchange.using("externalEditor", function() {
          // Have to fire editor after snippets, if snippets enabled
          if (StackExchange.settings.snippets.snippetsEnabled) {
          StackExchange.using("snippets", function() {
          createEditor();
          });
          }
          else {
          createEditor();
          }
          });

          function createEditor() {
          StackExchange.prepareEditor({
          heartbeatType: 'answer',
          autoActivateHeartbeat: false,
          convertImagesToLinks: true,
          noModals: true,
          showLowRepImageUploadWarning: true,
          reputationToPostImages: 10,
          bindNavPrevention: true,
          postfix: "",
          imageUploader: {
          brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
          contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
          allowUrls: true
          },
          onDemand: true,
          discardSelector: ".discard-answer"
          ,immediatelyShowMarkdownHelp:true
          });


          }
          });














          draft saved

          draft discarded


















          StackExchange.ready(
          function () {
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53343931%2fflatten-a-multi-directional-array-keys-with-delimiters-recursively%23new-answer', 'question_page');
          }
          );

          Post as a guest















          Required, but never shown

























          1 Answer
          1






          active

          oldest

          votes








          1 Answer
          1






          active

          oldest

          votes









          active

          oldest

          votes






          active

          oldest

          votes









          0














          From what I can tell (and with the help of your test case - which always helps :), the problem is that when you were returning the new values from the sub array processing, this was overwriting the existing contents. I've commented the two lines but just that I've used += to add the new data to the existing array data...



          function print_array_reccur ($array, $str = '')
          {
          if (empty($array)) return null;

          $arr = ;

          foreach ($array as $key => $val)
          {
          if (is_array($val))
          {
          if ($str == '')
          {
          // Add new data into the existing array using +=
          $arr += print_array_reccur($val, $key);
          }
          else
          {
          // Add new data into the existing array using +=
          $arr += print_array_reccur($val, $str . '/' . $key);
          }
          }
          else
          {
          if ($str == '')
          {
          $arr[$key] = $val;
          }
          else
          {
          $arr[$str . '/' . $key] = $val;
          }
          }
          }

          return $arr;
          }


          Hopefully the return value is what your after...



          Array
          (
          [arr] => 1
          [arr2/_arr2] => test
          [arr2/_arr2_2] => test 2
          [arr2/_arr2_3] => 3
          [arr2/_arr2_4/__arr2] => tt
          [arr2/_arr2_4/__arr2_2] => ttww
          [arr2/_arr2_4/sub-sub-field/ww] => 1
          [arr2/_arr2_5/__arr2_5] => some
          [arr3] => test
          [arr4] =>
          [arr5/_arr5/___arr5] => 1
          [arr5/_arr5_1] =>
          )





          share|improve this answer
























          • This is exactly what i wanted. I also now realize my mistake. You are correct. Thanks for helping

            – Julia Fox
            Nov 16 '18 at 19:33
















          0














          From what I can tell (and with the help of your test case - which always helps :), the problem is that when you were returning the new values from the sub array processing, this was overwriting the existing contents. I've commented the two lines but just that I've used += to add the new data to the existing array data...



          function print_array_reccur ($array, $str = '')
          {
          if (empty($array)) return null;

          $arr = ;

          foreach ($array as $key => $val)
          {
          if (is_array($val))
          {
          if ($str == '')
          {
          // Add new data into the existing array using +=
          $arr += print_array_reccur($val, $key);
          }
          else
          {
          // Add new data into the existing array using +=
          $arr += print_array_reccur($val, $str . '/' . $key);
          }
          }
          else
          {
          if ($str == '')
          {
          $arr[$key] = $val;
          }
          else
          {
          $arr[$str . '/' . $key] = $val;
          }
          }
          }

          return $arr;
          }


          Hopefully the return value is what your after...



          Array
          (
          [arr] => 1
          [arr2/_arr2] => test
          [arr2/_arr2_2] => test 2
          [arr2/_arr2_3] => 3
          [arr2/_arr2_4/__arr2] => tt
          [arr2/_arr2_4/__arr2_2] => ttww
          [arr2/_arr2_4/sub-sub-field/ww] => 1
          [arr2/_arr2_5/__arr2_5] => some
          [arr3] => test
          [arr4] =>
          [arr5/_arr5/___arr5] => 1
          [arr5/_arr5_1] =>
          )





          share|improve this answer
























          • This is exactly what i wanted. I also now realize my mistake. You are correct. Thanks for helping

            – Julia Fox
            Nov 16 '18 at 19:33














          0












          0








          0







          From what I can tell (and with the help of your test case - which always helps :), the problem is that when you were returning the new values from the sub array processing, this was overwriting the existing contents. I've commented the two lines but just that I've used += to add the new data to the existing array data...



          function print_array_reccur ($array, $str = '')
          {
          if (empty($array)) return null;

          $arr = ;

          foreach ($array as $key => $val)
          {
          if (is_array($val))
          {
          if ($str == '')
          {
          // Add new data into the existing array using +=
          $arr += print_array_reccur($val, $key);
          }
          else
          {
          // Add new data into the existing array using +=
          $arr += print_array_reccur($val, $str . '/' . $key);
          }
          }
          else
          {
          if ($str == '')
          {
          $arr[$key] = $val;
          }
          else
          {
          $arr[$str . '/' . $key] = $val;
          }
          }
          }

          return $arr;
          }


          Hopefully the return value is what your after...



          Array
          (
          [arr] => 1
          [arr2/_arr2] => test
          [arr2/_arr2_2] => test 2
          [arr2/_arr2_3] => 3
          [arr2/_arr2_4/__arr2] => tt
          [arr2/_arr2_4/__arr2_2] => ttww
          [arr2/_arr2_4/sub-sub-field/ww] => 1
          [arr2/_arr2_5/__arr2_5] => some
          [arr3] => test
          [arr4] =>
          [arr5/_arr5/___arr5] => 1
          [arr5/_arr5_1] =>
          )





          share|improve this answer













          From what I can tell (and with the help of your test case - which always helps :), the problem is that when you were returning the new values from the sub array processing, this was overwriting the existing contents. I've commented the two lines but just that I've used += to add the new data to the existing array data...



          function print_array_reccur ($array, $str = '')
          {
          if (empty($array)) return null;

          $arr = ;

          foreach ($array as $key => $val)
          {
          if (is_array($val))
          {
          if ($str == '')
          {
          // Add new data into the existing array using +=
          $arr += print_array_reccur($val, $key);
          }
          else
          {
          // Add new data into the existing array using +=
          $arr += print_array_reccur($val, $str . '/' . $key);
          }
          }
          else
          {
          if ($str == '')
          {
          $arr[$key] = $val;
          }
          else
          {
          $arr[$str . '/' . $key] = $val;
          }
          }
          }

          return $arr;
          }


          Hopefully the return value is what your after...



          Array
          (
          [arr] => 1
          [arr2/_arr2] => test
          [arr2/_arr2_2] => test 2
          [arr2/_arr2_3] => 3
          [arr2/_arr2_4/__arr2] => tt
          [arr2/_arr2_4/__arr2_2] => ttww
          [arr2/_arr2_4/sub-sub-field/ww] => 1
          [arr2/_arr2_5/__arr2_5] => some
          [arr3] => test
          [arr4] =>
          [arr5/_arr5/___arr5] => 1
          [arr5/_arr5_1] =>
          )






          share|improve this answer












          share|improve this answer



          share|improve this answer










          answered Nov 16 '18 at 19:18









          Nigel RenNigel Ren

          28.8k62034




          28.8k62034













          • This is exactly what i wanted. I also now realize my mistake. You are correct. Thanks for helping

            – Julia Fox
            Nov 16 '18 at 19:33



















          • This is exactly what i wanted. I also now realize my mistake. You are correct. Thanks for helping

            – Julia Fox
            Nov 16 '18 at 19:33

















          This is exactly what i wanted. I also now realize my mistake. You are correct. Thanks for helping

          – Julia Fox
          Nov 16 '18 at 19:33





          This is exactly what i wanted. I also now realize my mistake. You are correct. Thanks for helping

          – Julia Fox
          Nov 16 '18 at 19:33




















          draft saved

          draft discarded




















































          Thanks for contributing an answer to Stack Overflow!


          • Please be sure to answer the question. Provide details and share your research!

          But avoid



          • Asking for help, clarification, or responding to other answers.

          • Making statements based on opinion; back them up with references or personal experience.


          To learn more, see our tips on writing great answers.




          draft saved


          draft discarded














          StackExchange.ready(
          function () {
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53343931%2fflatten-a-multi-directional-array-keys-with-delimiters-recursively%23new-answer', 'question_page');
          }
          );

          Post as a guest















          Required, but never shown





















































          Required, but never shown














          Required, but never shown












          Required, but never shown







          Required, but never shown

































          Required, but never shown














          Required, but never shown












          Required, but never shown







          Required, but never shown







          Popular posts from this blog

          Florida Star v. B. J. F.

          Danny Elfman

          Lugert, Oklahoma