PHP Array rearrange numeric key as parrent key












0















i want to rearrange a simple multidimensional array.



Array
(
[pieces] => Array
(
[0] => 2
[1] => 9
)
[start] => Array
(
[0] => 0001
[1] => 9901
)
[end] => Array
(
[0] => 0002
[1] => 9909
)
[group] => Array
(
[0] => 0001-0100
[1] => 9901-9999
)
)


to



Array
(
[tokens] => Array
(
[0] => Array
(
[start] => 0001
[end] => 0002
[pieces] => 2
[group] => 0100
)
[1] => Array
(
[start] => 9901
[end] => 9909
[pieces] => 9
[group] => 9901-9999
)
)
)


I have tried something similar this:



$keys = array_keys($array);
foreach ($keys as $key => $val) {
foreach ($array as $k => $v){
foreach($array[$v] as $tk => $tv){
if($val == $k){
$new['tokens'][$val] = $tv;
}
}
}
}


The numeric is the set of tokens which i prosted from my form,



Please can anyone explain me what i do wrong?
I am working some hours with different codes (i know the solution is very simple) but I am a little bit confused :/



Thank you very much!



BR KK










share|improve this question



























    0















    i want to rearrange a simple multidimensional array.



    Array
    (
    [pieces] => Array
    (
    [0] => 2
    [1] => 9
    )
    [start] => Array
    (
    [0] => 0001
    [1] => 9901
    )
    [end] => Array
    (
    [0] => 0002
    [1] => 9909
    )
    [group] => Array
    (
    [0] => 0001-0100
    [1] => 9901-9999
    )
    )


    to



    Array
    (
    [tokens] => Array
    (
    [0] => Array
    (
    [start] => 0001
    [end] => 0002
    [pieces] => 2
    [group] => 0100
    )
    [1] => Array
    (
    [start] => 9901
    [end] => 9909
    [pieces] => 9
    [group] => 9901-9999
    )
    )
    )


    I have tried something similar this:



    $keys = array_keys($array);
    foreach ($keys as $key => $val) {
    foreach ($array as $k => $v){
    foreach($array[$v] as $tk => $tv){
    if($val == $k){
    $new['tokens'][$val] = $tv;
    }
    }
    }
    }


    The numeric is the set of tokens which i prosted from my form,



    Please can anyone explain me what i do wrong?
    I am working some hours with different codes (i know the solution is very simple) but I am a little bit confused :/



    Thank you very much!



    BR KK










    share|improve this question

























      0












      0








      0








      i want to rearrange a simple multidimensional array.



      Array
      (
      [pieces] => Array
      (
      [0] => 2
      [1] => 9
      )
      [start] => Array
      (
      [0] => 0001
      [1] => 9901
      )
      [end] => Array
      (
      [0] => 0002
      [1] => 9909
      )
      [group] => Array
      (
      [0] => 0001-0100
      [1] => 9901-9999
      )
      )


      to



      Array
      (
      [tokens] => Array
      (
      [0] => Array
      (
      [start] => 0001
      [end] => 0002
      [pieces] => 2
      [group] => 0100
      )
      [1] => Array
      (
      [start] => 9901
      [end] => 9909
      [pieces] => 9
      [group] => 9901-9999
      )
      )
      )


      I have tried something similar this:



      $keys = array_keys($array);
      foreach ($keys as $key => $val) {
      foreach ($array as $k => $v){
      foreach($array[$v] as $tk => $tv){
      if($val == $k){
      $new['tokens'][$val] = $tv;
      }
      }
      }
      }


      The numeric is the set of tokens which i prosted from my form,



      Please can anyone explain me what i do wrong?
      I am working some hours with different codes (i know the solution is very simple) but I am a little bit confused :/



      Thank you very much!



      BR KK










      share|improve this question














      i want to rearrange a simple multidimensional array.



      Array
      (
      [pieces] => Array
      (
      [0] => 2
      [1] => 9
      )
      [start] => Array
      (
      [0] => 0001
      [1] => 9901
      )
      [end] => Array
      (
      [0] => 0002
      [1] => 9909
      )
      [group] => Array
      (
      [0] => 0001-0100
      [1] => 9901-9999
      )
      )


      to



      Array
      (
      [tokens] => Array
      (
      [0] => Array
      (
      [start] => 0001
      [end] => 0002
      [pieces] => 2
      [group] => 0100
      )
      [1] => Array
      (
      [start] => 9901
      [end] => 9909
      [pieces] => 9
      [group] => 9901-9999
      )
      )
      )


      I have tried something similar this:



      $keys = array_keys($array);
      foreach ($keys as $key => $val) {
      foreach ($array as $k => $v){
      foreach($array[$v] as $tk => $tv){
      if($val == $k){
      $new['tokens'][$val] = $tv;
      }
      }
      }
      }


      The numeric is the set of tokens which i prosted from my form,



      Please can anyone explain me what i do wrong?
      I am working some hours with different codes (i know the solution is very simple) but I am a little bit confused :/



      Thank you very much!



      BR KK







      php arrays multidimensional-array






      share|improve this question













      share|improve this question











      share|improve this question




      share|improve this question










      asked Jan 2 at 19:55









      user10859447user10859447

      31




      31
























          1 Answer
          1






          active

          oldest

          votes


















          3














          The Fourth Bird's solution is quite rigid in that it:




          • Requires an explicitly-defined key in the loop condition.

          • Enforces that the entire result has no more items than that one key has.

          • Assumes and enforces that the input keys are sequential and zero-indexed.


          The below will work no matter what:



          foreach( $array as $y => $inner ) {
          foreach( $inner as $x => $value ) {
          $new['tokens'][$x][$y] = $value;
          }
          }


          Demo: https://3v4l.org/Rmdtd





          Edit: I think it's worth preserving The Fourth Bird's explanation of the trouble with the posted code from his now-deleted answer:




          You are trying to index into $array[$v], but $v in the case is one
          of the sub arrays. According to the array
          docs:



          Arrays and objects can not be used as keys. Doing so will result in
          a warning: Illegal offset type
          .




          Make sure that you have error_reporting turned up to E_ALL while you're developing code so that you can see non-critical messages that indicate current and/or future problems.






          share|improve this answer


























          • thank you very much! I am so stupid! at first i had the same iteration like yours but i tought i have to do something with "if"-statements and linked the fals vars to $new and was wondering why i do not get the result. Thanks for clarification!

            – user10859447
            Jan 2 at 20:34













          • Thats better indeed! +1

            – The fourth bird
            Jan 2 at 20:37














          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%2f54012368%2fphp-array-rearrange-numeric-key-as-parrent-key%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









          3














          The Fourth Bird's solution is quite rigid in that it:




          • Requires an explicitly-defined key in the loop condition.

          • Enforces that the entire result has no more items than that one key has.

          • Assumes and enforces that the input keys are sequential and zero-indexed.


          The below will work no matter what:



          foreach( $array as $y => $inner ) {
          foreach( $inner as $x => $value ) {
          $new['tokens'][$x][$y] = $value;
          }
          }


          Demo: https://3v4l.org/Rmdtd





          Edit: I think it's worth preserving The Fourth Bird's explanation of the trouble with the posted code from his now-deleted answer:




          You are trying to index into $array[$v], but $v in the case is one
          of the sub arrays. According to the array
          docs:



          Arrays and objects can not be used as keys. Doing so will result in
          a warning: Illegal offset type
          .




          Make sure that you have error_reporting turned up to E_ALL while you're developing code so that you can see non-critical messages that indicate current and/or future problems.






          share|improve this answer


























          • thank you very much! I am so stupid! at first i had the same iteration like yours but i tought i have to do something with "if"-statements and linked the fals vars to $new and was wondering why i do not get the result. Thanks for clarification!

            – user10859447
            Jan 2 at 20:34













          • Thats better indeed! +1

            – The fourth bird
            Jan 2 at 20:37


















          3














          The Fourth Bird's solution is quite rigid in that it:




          • Requires an explicitly-defined key in the loop condition.

          • Enforces that the entire result has no more items than that one key has.

          • Assumes and enforces that the input keys are sequential and zero-indexed.


          The below will work no matter what:



          foreach( $array as $y => $inner ) {
          foreach( $inner as $x => $value ) {
          $new['tokens'][$x][$y] = $value;
          }
          }


          Demo: https://3v4l.org/Rmdtd





          Edit: I think it's worth preserving The Fourth Bird's explanation of the trouble with the posted code from his now-deleted answer:




          You are trying to index into $array[$v], but $v in the case is one
          of the sub arrays. According to the array
          docs:



          Arrays and objects can not be used as keys. Doing so will result in
          a warning: Illegal offset type
          .




          Make sure that you have error_reporting turned up to E_ALL while you're developing code so that you can see non-critical messages that indicate current and/or future problems.






          share|improve this answer


























          • thank you very much! I am so stupid! at first i had the same iteration like yours but i tought i have to do something with "if"-statements and linked the fals vars to $new and was wondering why i do not get the result. Thanks for clarification!

            – user10859447
            Jan 2 at 20:34













          • Thats better indeed! +1

            – The fourth bird
            Jan 2 at 20:37
















          3












          3








          3







          The Fourth Bird's solution is quite rigid in that it:




          • Requires an explicitly-defined key in the loop condition.

          • Enforces that the entire result has no more items than that one key has.

          • Assumes and enforces that the input keys are sequential and zero-indexed.


          The below will work no matter what:



          foreach( $array as $y => $inner ) {
          foreach( $inner as $x => $value ) {
          $new['tokens'][$x][$y] = $value;
          }
          }


          Demo: https://3v4l.org/Rmdtd





          Edit: I think it's worth preserving The Fourth Bird's explanation of the trouble with the posted code from his now-deleted answer:




          You are trying to index into $array[$v], but $v in the case is one
          of the sub arrays. According to the array
          docs:



          Arrays and objects can not be used as keys. Doing so will result in
          a warning: Illegal offset type
          .




          Make sure that you have error_reporting turned up to E_ALL while you're developing code so that you can see non-critical messages that indicate current and/or future problems.






          share|improve this answer















          The Fourth Bird's solution is quite rigid in that it:




          • Requires an explicitly-defined key in the loop condition.

          • Enforces that the entire result has no more items than that one key has.

          • Assumes and enforces that the input keys are sequential and zero-indexed.


          The below will work no matter what:



          foreach( $array as $y => $inner ) {
          foreach( $inner as $x => $value ) {
          $new['tokens'][$x][$y] = $value;
          }
          }


          Demo: https://3v4l.org/Rmdtd





          Edit: I think it's worth preserving The Fourth Bird's explanation of the trouble with the posted code from his now-deleted answer:




          You are trying to index into $array[$v], but $v in the case is one
          of the sub arrays. According to the array
          docs:



          Arrays and objects can not be used as keys. Doing so will result in
          a warning: Illegal offset type
          .




          Make sure that you have error_reporting turned up to E_ALL while you're developing code so that you can see non-critical messages that indicate current and/or future problems.







          share|improve this answer














          share|improve this answer



          share|improve this answer








          edited Jan 2 at 21:00

























          answered Jan 2 at 20:17









          SammitchSammitch

          20.5k42965




          20.5k42965













          • thank you very much! I am so stupid! at first i had the same iteration like yours but i tought i have to do something with "if"-statements and linked the fals vars to $new and was wondering why i do not get the result. Thanks for clarification!

            – user10859447
            Jan 2 at 20:34













          • Thats better indeed! +1

            – The fourth bird
            Jan 2 at 20:37





















          • thank you very much! I am so stupid! at first i had the same iteration like yours but i tought i have to do something with "if"-statements and linked the fals vars to $new and was wondering why i do not get the result. Thanks for clarification!

            – user10859447
            Jan 2 at 20:34













          • Thats better indeed! +1

            – The fourth bird
            Jan 2 at 20:37



















          thank you very much! I am so stupid! at first i had the same iteration like yours but i tought i have to do something with "if"-statements and linked the fals vars to $new and was wondering why i do not get the result. Thanks for clarification!

          – user10859447
          Jan 2 at 20:34







          thank you very much! I am so stupid! at first i had the same iteration like yours but i tought i have to do something with "if"-statements and linked the fals vars to $new and was wondering why i do not get the result. Thanks for clarification!

          – user10859447
          Jan 2 at 20:34















          Thats better indeed! +1

          – The fourth bird
          Jan 2 at 20:37







          Thats better indeed! +1

          – The fourth bird
          Jan 2 at 20:37






















          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%2f54012368%2fphp-array-rearrange-numeric-key-as-parrent-key%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

          MongoDB - Not Authorized To Execute Command

          How to fix TextFormField cause rebuild widget in Flutter

          in spring boot 2.1 many test slices are not allowed anymore due to multiple @BootstrapWith