PHP Array Formatting for array inner array without using RecursiveIteratorIterator












0















How can I get this format? Result should be dynamic:



1 
a
b
0


This is my original array:



$arr = array(0 => 1,
array(
0 => 'a',
1 => 'b'
),
'c' => '0'
);


I've tried:



$kArr = array();
for($i=0; $i<count($arr); $i++ ){
$iArr = array();
echo $arr[$i];
foreach($arr[$i] as $key => $value){
if(!is_array($value)) continue;

foreach($value as $subKey => $subValue){
$iArr = $subValue;
}
}

if(count($iArr) > 0 )
$kArr[$i] = $iArr;
}

echo '<pre>'; print_r($kArr);


Where I am wrong? if there is another method then please let me know.










share|improve this question

























  • What exactly is "this format"? Are you looking for a way to recursively print the values?

    – Nico Haase
    Nov 27 '18 at 10:47











  • are you just trying to convert multi-dimensional array into a 2d array?

    – treyBake
    Nov 27 '18 at 10:49











  • Yes, I mentioned the result which format I want. If the array has inner array then the result must be print as per my formatted.

    – Vipul Jethva
    Nov 27 '18 at 10:49






  • 4





    Possible duplicate of How to Flatten a Multidimensional Array?

    – ponury-kostek
    Nov 27 '18 at 10:50











  • @ponury-kostek Can you please help me to use my array and result which I want?

    – Vipul Jethva
    Nov 27 '18 at 10:55
















0















How can I get this format? Result should be dynamic:



1 
a
b
0


This is my original array:



$arr = array(0 => 1,
array(
0 => 'a',
1 => 'b'
),
'c' => '0'
);


I've tried:



$kArr = array();
for($i=0; $i<count($arr); $i++ ){
$iArr = array();
echo $arr[$i];
foreach($arr[$i] as $key => $value){
if(!is_array($value)) continue;

foreach($value as $subKey => $subValue){
$iArr = $subValue;
}
}

if(count($iArr) > 0 )
$kArr[$i] = $iArr;
}

echo '<pre>'; print_r($kArr);


Where I am wrong? if there is another method then please let me know.










share|improve this question

























  • What exactly is "this format"? Are you looking for a way to recursively print the values?

    – Nico Haase
    Nov 27 '18 at 10:47











  • are you just trying to convert multi-dimensional array into a 2d array?

    – treyBake
    Nov 27 '18 at 10:49











  • Yes, I mentioned the result which format I want. If the array has inner array then the result must be print as per my formatted.

    – Vipul Jethva
    Nov 27 '18 at 10:49






  • 4





    Possible duplicate of How to Flatten a Multidimensional Array?

    – ponury-kostek
    Nov 27 '18 at 10:50











  • @ponury-kostek Can you please help me to use my array and result which I want?

    – Vipul Jethva
    Nov 27 '18 at 10:55














0












0








0








How can I get this format? Result should be dynamic:



1 
a
b
0


This is my original array:



$arr = array(0 => 1,
array(
0 => 'a',
1 => 'b'
),
'c' => '0'
);


I've tried:



$kArr = array();
for($i=0; $i<count($arr); $i++ ){
$iArr = array();
echo $arr[$i];
foreach($arr[$i] as $key => $value){
if(!is_array($value)) continue;

foreach($value as $subKey => $subValue){
$iArr = $subValue;
}
}

if(count($iArr) > 0 )
$kArr[$i] = $iArr;
}

echo '<pre>'; print_r($kArr);


Where I am wrong? if there is another method then please let me know.










share|improve this question
















How can I get this format? Result should be dynamic:



1 
a
b
0


This is my original array:



$arr = array(0 => 1,
array(
0 => 'a',
1 => 'b'
),
'c' => '0'
);


I've tried:



$kArr = array();
for($i=0; $i<count($arr); $i++ ){
$iArr = array();
echo $arr[$i];
foreach($arr[$i] as $key => $value){
if(!is_array($value)) continue;

foreach($value as $subKey => $subValue){
$iArr = $subValue;
}
}

if(count($iArr) > 0 )
$kArr[$i] = $iArr;
}

echo '<pre>'; print_r($kArr);


Where I am wrong? if there is another method then please let me know.







php arrays multidimensional-array






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 27 '18 at 10:57







Vipul Jethva

















asked Nov 27 '18 at 10:47









Vipul JethvaVipul Jethva

432620




432620













  • What exactly is "this format"? Are you looking for a way to recursively print the values?

    – Nico Haase
    Nov 27 '18 at 10:47











  • are you just trying to convert multi-dimensional array into a 2d array?

    – treyBake
    Nov 27 '18 at 10:49











  • Yes, I mentioned the result which format I want. If the array has inner array then the result must be print as per my formatted.

    – Vipul Jethva
    Nov 27 '18 at 10:49






  • 4





    Possible duplicate of How to Flatten a Multidimensional Array?

    – ponury-kostek
    Nov 27 '18 at 10:50











  • @ponury-kostek Can you please help me to use my array and result which I want?

    – Vipul Jethva
    Nov 27 '18 at 10:55



















  • What exactly is "this format"? Are you looking for a way to recursively print the values?

    – Nico Haase
    Nov 27 '18 at 10:47











  • are you just trying to convert multi-dimensional array into a 2d array?

    – treyBake
    Nov 27 '18 at 10:49











  • Yes, I mentioned the result which format I want. If the array has inner array then the result must be print as per my formatted.

    – Vipul Jethva
    Nov 27 '18 at 10:49






  • 4





    Possible duplicate of How to Flatten a Multidimensional Array?

    – ponury-kostek
    Nov 27 '18 at 10:50











  • @ponury-kostek Can you please help me to use my array and result which I want?

    – Vipul Jethva
    Nov 27 '18 at 10:55

















What exactly is "this format"? Are you looking for a way to recursively print the values?

– Nico Haase
Nov 27 '18 at 10:47





What exactly is "this format"? Are you looking for a way to recursively print the values?

– Nico Haase
Nov 27 '18 at 10:47













are you just trying to convert multi-dimensional array into a 2d array?

– treyBake
Nov 27 '18 at 10:49





are you just trying to convert multi-dimensional array into a 2d array?

– treyBake
Nov 27 '18 at 10:49













Yes, I mentioned the result which format I want. If the array has inner array then the result must be print as per my formatted.

– Vipul Jethva
Nov 27 '18 at 10:49





Yes, I mentioned the result which format I want. If the array has inner array then the result must be print as per my formatted.

– Vipul Jethva
Nov 27 '18 at 10:49




4




4





Possible duplicate of How to Flatten a Multidimensional Array?

– ponury-kostek
Nov 27 '18 at 10:50





Possible duplicate of How to Flatten a Multidimensional Array?

– ponury-kostek
Nov 27 '18 at 10:50













@ponury-kostek Can you please help me to use my array and result which I want?

– Vipul Jethva
Nov 27 '18 at 10:55





@ponury-kostek Can you please help me to use my array and result which I want?

– Vipul Jethva
Nov 27 '18 at 10:55












5 Answers
5






active

oldest

votes


















1














foreach ($arr as $k => $v) {
if (is_array($v)) {
foreach ($v as $k2 => $v2) {
echo "$v2n";
}
} else {
echo "$vn";
}
}


if you want to do it from scratch you can try something like this.






share|improve this answer































    1














    To achieve your desired result you have to loop through your $arr variable and check if it's an array using is_array function. If it is an array then loop through that array and out put the values.
    Try the following code.



    $arr = array(0 => 1,
    array(
    0 => 'a',
    1 => 'b'
    ),
    'c' => '0'
    );
    foreach($arr as $key => $value)
    {
    if(is_array($value))
    {
    foreach($value as $inner_key => $inner_value)
    {
    echo "<br>";
    echo $inner_value;
    }
    }
    else
    {
    echo "<br>";
    echo $value;
    }
    }





    share|improve this answer

































      1














      I wrote a function that gets the values of the internal arrays and adds them in the correct order:



      function array_values_recursive(array $array)
      {
      $values = array_values($array);

      foreach ($values as $key => $value) {
      if (is_array($value)) {
      $innerValues = array_values_recursive($value);

      if ($innerValues) {
      array_splice($values, $key, 1, $innerValues);
      }
      }
      }

      return $values;
      }


      Example:



      $array = array(
      0 => 1,
      array(
      0 => 'a',
      1 => 'b'
      ),
      'c' => '0'
      );

      $values = array_values_recursive($array);

      echo(implode(PHP_EOL, $values));


      Will print:



      1
      a
      b
      0





      share|improve this answer































        0














        function flatten($array) {
        // flattened array
        $flat = ;
        // flattening anonymous function
        $flatten = function ($array) use (&$flatten, &$flat) {
        // loop over an array
        foreach ($array as $item) {
        // if item is array, use flattening function
        if (is_array($item)) {
        $flatten($item);
        } else {
        // else push item to flattened array
        $flat = $item;
        }
        }
        };
        // initial call of flattening function
        $flatten($array);

        // return flattened array
        return $flat;
        }

        var_dump(flatten([
        0 => 1,
        [
        0 => 'a',
        1 => 'b'
        ],
        'c' => '0'
        ]));





        share|improve this answer

































          -1














          Just use array_walk_recursive.



          $r = ;
          array_walk_recursive($arr, function ($e) use (&$r) {
          $r = $e;
          });





          share|improve this answer























            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%2f53497935%2fphp-array-formatting-for-array-inner-array-without-using-recursiveiteratoriterat%23new-answer', 'question_page');
            }
            );

            Post as a guest















            Required, but never shown

























            5 Answers
            5






            active

            oldest

            votes








            5 Answers
            5






            active

            oldest

            votes









            active

            oldest

            votes






            active

            oldest

            votes









            1














            foreach ($arr as $k => $v) {
            if (is_array($v)) {
            foreach ($v as $k2 => $v2) {
            echo "$v2n";
            }
            } else {
            echo "$vn";
            }
            }


            if you want to do it from scratch you can try something like this.






            share|improve this answer




























              1














              foreach ($arr as $k => $v) {
              if (is_array($v)) {
              foreach ($v as $k2 => $v2) {
              echo "$v2n";
              }
              } else {
              echo "$vn";
              }
              }


              if you want to do it from scratch you can try something like this.






              share|improve this answer


























                1












                1








                1







                foreach ($arr as $k => $v) {
                if (is_array($v)) {
                foreach ($v as $k2 => $v2) {
                echo "$v2n";
                }
                } else {
                echo "$vn";
                }
                }


                if you want to do it from scratch you can try something like this.






                share|improve this answer













                foreach ($arr as $k => $v) {
                if (is_array($v)) {
                foreach ($v as $k2 => $v2) {
                echo "$v2n";
                }
                } else {
                echo "$vn";
                }
                }


                if you want to do it from scratch you can try something like this.







                share|improve this answer












                share|improve this answer



                share|improve this answer










                answered Nov 27 '18 at 11:02









                RomMerRomMer

                10111




                10111

























                    1














                    To achieve your desired result you have to loop through your $arr variable and check if it's an array using is_array function. If it is an array then loop through that array and out put the values.
                    Try the following code.



                    $arr = array(0 => 1,
                    array(
                    0 => 'a',
                    1 => 'b'
                    ),
                    'c' => '0'
                    );
                    foreach($arr as $key => $value)
                    {
                    if(is_array($value))
                    {
                    foreach($value as $inner_key => $inner_value)
                    {
                    echo "<br>";
                    echo $inner_value;
                    }
                    }
                    else
                    {
                    echo "<br>";
                    echo $value;
                    }
                    }





                    share|improve this answer






























                      1














                      To achieve your desired result you have to loop through your $arr variable and check if it's an array using is_array function. If it is an array then loop through that array and out put the values.
                      Try the following code.



                      $arr = array(0 => 1,
                      array(
                      0 => 'a',
                      1 => 'b'
                      ),
                      'c' => '0'
                      );
                      foreach($arr as $key => $value)
                      {
                      if(is_array($value))
                      {
                      foreach($value as $inner_key => $inner_value)
                      {
                      echo "<br>";
                      echo $inner_value;
                      }
                      }
                      else
                      {
                      echo "<br>";
                      echo $value;
                      }
                      }





                      share|improve this answer




























                        1












                        1








                        1







                        To achieve your desired result you have to loop through your $arr variable and check if it's an array using is_array function. If it is an array then loop through that array and out put the values.
                        Try the following code.



                        $arr = array(0 => 1,
                        array(
                        0 => 'a',
                        1 => 'b'
                        ),
                        'c' => '0'
                        );
                        foreach($arr as $key => $value)
                        {
                        if(is_array($value))
                        {
                        foreach($value as $inner_key => $inner_value)
                        {
                        echo "<br>";
                        echo $inner_value;
                        }
                        }
                        else
                        {
                        echo "<br>";
                        echo $value;
                        }
                        }





                        share|improve this answer















                        To achieve your desired result you have to loop through your $arr variable and check if it's an array using is_array function. If it is an array then loop through that array and out put the values.
                        Try the following code.



                        $arr = array(0 => 1,
                        array(
                        0 => 'a',
                        1 => 'b'
                        ),
                        'c' => '0'
                        );
                        foreach($arr as $key => $value)
                        {
                        if(is_array($value))
                        {
                        foreach($value as $inner_key => $inner_value)
                        {
                        echo "<br>";
                        echo $inner_value;
                        }
                        }
                        else
                        {
                        echo "<br>";
                        echo $value;
                        }
                        }






                        share|improve this answer














                        share|improve this answer



                        share|improve this answer








                        edited Nov 27 '18 at 11:12

























                        answered Nov 27 '18 at 11:05









                        Sajjad AliSajjad Ali

                        22419




                        22419























                            1














                            I wrote a function that gets the values of the internal arrays and adds them in the correct order:



                            function array_values_recursive(array $array)
                            {
                            $values = array_values($array);

                            foreach ($values as $key => $value) {
                            if (is_array($value)) {
                            $innerValues = array_values_recursive($value);

                            if ($innerValues) {
                            array_splice($values, $key, 1, $innerValues);
                            }
                            }
                            }

                            return $values;
                            }


                            Example:



                            $array = array(
                            0 => 1,
                            array(
                            0 => 'a',
                            1 => 'b'
                            ),
                            'c' => '0'
                            );

                            $values = array_values_recursive($array);

                            echo(implode(PHP_EOL, $values));


                            Will print:



                            1
                            a
                            b
                            0





                            share|improve this answer




























                              1














                              I wrote a function that gets the values of the internal arrays and adds them in the correct order:



                              function array_values_recursive(array $array)
                              {
                              $values = array_values($array);

                              foreach ($values as $key => $value) {
                              if (is_array($value)) {
                              $innerValues = array_values_recursive($value);

                              if ($innerValues) {
                              array_splice($values, $key, 1, $innerValues);
                              }
                              }
                              }

                              return $values;
                              }


                              Example:



                              $array = array(
                              0 => 1,
                              array(
                              0 => 'a',
                              1 => 'b'
                              ),
                              'c' => '0'
                              );

                              $values = array_values_recursive($array);

                              echo(implode(PHP_EOL, $values));


                              Will print:



                              1
                              a
                              b
                              0





                              share|improve this answer


























                                1












                                1








                                1







                                I wrote a function that gets the values of the internal arrays and adds them in the correct order:



                                function array_values_recursive(array $array)
                                {
                                $values = array_values($array);

                                foreach ($values as $key => $value) {
                                if (is_array($value)) {
                                $innerValues = array_values_recursive($value);

                                if ($innerValues) {
                                array_splice($values, $key, 1, $innerValues);
                                }
                                }
                                }

                                return $values;
                                }


                                Example:



                                $array = array(
                                0 => 1,
                                array(
                                0 => 'a',
                                1 => 'b'
                                ),
                                'c' => '0'
                                );

                                $values = array_values_recursive($array);

                                echo(implode(PHP_EOL, $values));


                                Will print:



                                1
                                a
                                b
                                0





                                share|improve this answer













                                I wrote a function that gets the values of the internal arrays and adds them in the correct order:



                                function array_values_recursive(array $array)
                                {
                                $values = array_values($array);

                                foreach ($values as $key => $value) {
                                if (is_array($value)) {
                                $innerValues = array_values_recursive($value);

                                if ($innerValues) {
                                array_splice($values, $key, 1, $innerValues);
                                }
                                }
                                }

                                return $values;
                                }


                                Example:



                                $array = array(
                                0 => 1,
                                array(
                                0 => 'a',
                                1 => 'b'
                                ),
                                'c' => '0'
                                );

                                $values = array_values_recursive($array);

                                echo(implode(PHP_EOL, $values));


                                Will print:



                                1
                                a
                                b
                                0






                                share|improve this answer












                                share|improve this answer



                                share|improve this answer










                                answered Nov 27 '18 at 11:26









                                Serge AndriichukSerge Andriichuk

                                491




                                491























                                    0














                                    function flatten($array) {
                                    // flattened array
                                    $flat = ;
                                    // flattening anonymous function
                                    $flatten = function ($array) use (&$flatten, &$flat) {
                                    // loop over an array
                                    foreach ($array as $item) {
                                    // if item is array, use flattening function
                                    if (is_array($item)) {
                                    $flatten($item);
                                    } else {
                                    // else push item to flattened array
                                    $flat = $item;
                                    }
                                    }
                                    };
                                    // initial call of flattening function
                                    $flatten($array);

                                    // return flattened array
                                    return $flat;
                                    }

                                    var_dump(flatten([
                                    0 => 1,
                                    [
                                    0 => 'a',
                                    1 => 'b'
                                    ],
                                    'c' => '0'
                                    ]));





                                    share|improve this answer






























                                      0














                                      function flatten($array) {
                                      // flattened array
                                      $flat = ;
                                      // flattening anonymous function
                                      $flatten = function ($array) use (&$flatten, &$flat) {
                                      // loop over an array
                                      foreach ($array as $item) {
                                      // if item is array, use flattening function
                                      if (is_array($item)) {
                                      $flatten($item);
                                      } else {
                                      // else push item to flattened array
                                      $flat = $item;
                                      }
                                      }
                                      };
                                      // initial call of flattening function
                                      $flatten($array);

                                      // return flattened array
                                      return $flat;
                                      }

                                      var_dump(flatten([
                                      0 => 1,
                                      [
                                      0 => 'a',
                                      1 => 'b'
                                      ],
                                      'c' => '0'
                                      ]));





                                      share|improve this answer




























                                        0












                                        0








                                        0







                                        function flatten($array) {
                                        // flattened array
                                        $flat = ;
                                        // flattening anonymous function
                                        $flatten = function ($array) use (&$flatten, &$flat) {
                                        // loop over an array
                                        foreach ($array as $item) {
                                        // if item is array, use flattening function
                                        if (is_array($item)) {
                                        $flatten($item);
                                        } else {
                                        // else push item to flattened array
                                        $flat = $item;
                                        }
                                        }
                                        };
                                        // initial call of flattening function
                                        $flatten($array);

                                        // return flattened array
                                        return $flat;
                                        }

                                        var_dump(flatten([
                                        0 => 1,
                                        [
                                        0 => 'a',
                                        1 => 'b'
                                        ],
                                        'c' => '0'
                                        ]));





                                        share|improve this answer















                                        function flatten($array) {
                                        // flattened array
                                        $flat = ;
                                        // flattening anonymous function
                                        $flatten = function ($array) use (&$flatten, &$flat) {
                                        // loop over an array
                                        foreach ($array as $item) {
                                        // if item is array, use flattening function
                                        if (is_array($item)) {
                                        $flatten($item);
                                        } else {
                                        // else push item to flattened array
                                        $flat = $item;
                                        }
                                        }
                                        };
                                        // initial call of flattening function
                                        $flatten($array);

                                        // return flattened array
                                        return $flat;
                                        }

                                        var_dump(flatten([
                                        0 => 1,
                                        [
                                        0 => 'a',
                                        1 => 'b'
                                        ],
                                        'c' => '0'
                                        ]));






                                        share|improve this answer














                                        share|improve this answer



                                        share|improve this answer








                                        edited Nov 27 '18 at 11:21

























                                        answered Nov 27 '18 at 11:14









                                        ponury-kostekponury-kostek

                                        4,75441123




                                        4,75441123























                                            -1














                                            Just use array_walk_recursive.



                                            $r = ;
                                            array_walk_recursive($arr, function ($e) use (&$r) {
                                            $r = $e;
                                            });





                                            share|improve this answer




























                                              -1














                                              Just use array_walk_recursive.



                                              $r = ;
                                              array_walk_recursive($arr, function ($e) use (&$r) {
                                              $r = $e;
                                              });





                                              share|improve this answer


























                                                -1












                                                -1








                                                -1







                                                Just use array_walk_recursive.



                                                $r = ;
                                                array_walk_recursive($arr, function ($e) use (&$r) {
                                                $r = $e;
                                                });





                                                share|improve this answer













                                                Just use array_walk_recursive.



                                                $r = ;
                                                array_walk_recursive($arr, function ($e) use (&$r) {
                                                $r = $e;
                                                });






                                                share|improve this answer












                                                share|improve this answer



                                                share|improve this answer










                                                answered Nov 27 '18 at 10:51









                                                kopirokopiro

                                                52429




                                                52429






























                                                    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%2f53497935%2fphp-array-formatting-for-array-inner-array-without-using-recursiveiteratoriterat%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

                                                    A CLEAN and SIMPLE way to add appendices to Table of Contents and bookmarks

                                                    Calculate evaluation metrics using cross_val_predict sklearn

                                                    Insert data from modal to MySQL (multiple modal on website)