5

using php how to get array result into below way,

 Array
    (
        [3] => Array
            (
                [15] => 15
                [16] => 16
                [17] => 17
                [18] => 18
                [19] => 19
            )

    )

how to convert above array into below format,

 Array
        (
            [0] => 15
            [1] => 16
            [2] => 17
            [3] => 18
            [4] => 19
        )
3
  • Append all child elements into a separate array and then go from there. I'm thinking foreach would work. Not sure if that's the most convenient, but it will work. Commented May 3, 2017 at 13:45
  • Possible duplicate of Removing an outer array: Commented May 3, 2017 at 13:46
  • If you dont know index of outter array and use php > 5.6 array_merge(...$array) Commented May 3, 2017 at 13:51

5 Answers 5

6

array_values() is your friend;

Presuming your array exists in a variable called $array;

$newArray = array_values($array[3]);
Sign up to request clarification or add additional context in comments.

Comments

3

you should use RecursiveArrayIterator to remove parent array

$arr = new RecursiveIteratorIterator(new RecursiveArrayIterator($multidimensional_array));
$new_arr = iterator_to_array($arr, false);

1 Comment

Works great, even in PHP 5.3. However, for some reason I can't get it to work as a function. Any idea on that? Seems simple enough, yet doesn't work.
2

try this, if you have more than one sub-array, it will work.

$arr = array(3 =>
        array
          ( 
           15  => 15,
           16 => 16,    
           17    => 17,  
           18    => 18,  
           19    => 19
          )
        );
    $new = array();
        foreach ($arr as $v){
            $new = array_merge($new , array_values($v)) ;
        }
echo "<pre>"; print_r($new);

Working Demo

Comments

1

Have not tested it but should work as per your requirement.

<?php
$parent array = array(); // The array which you want to change
$result_array = array(); // The array that will hold the results
foreach( $parent_array as $child_array )
{
    if( is_array( $child_array ) )
    {
        foreach( $child_array as $element )
        {
            $result_array[] = $element
        }
    }
}
echo '<pre>';
print_r($result_array);
echo '</pre>';
?>

Comments

0

it's pretty simple, as you can just assign the array holding variable to the values of one index …

<?php

/* build list */
for($i=15;$i<=19;$i++)
    $b[$i] = $i;
$a[3] = $b;

var_export($a);

/* make array smaller again */
$a = $a[3];

var_export($a);

/* reindexing, just values */
$a = array_values( $a );

var_export($a);

?>

the reindexing part is done by a build-in function, also have a look on php.net for the linked functions for arrays, you can do massive stuff simple with them.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.