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.

13
  • What exactly is "this format"? Are you looking for a way to recursively print the values? Commented Nov 27, 2018 at 10:47
  • are you just trying to convert multi-dimensional array into a 2d array? Commented Nov 27, 2018 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. Commented Nov 27, 2018 at 10:49
  • 4
    Possible duplicate of How to Flatten a Multidimensional Array? Commented Nov 27, 2018 at 10:50
  • @ponury-kostek Can you please help me to use my array and result which I want? Commented Nov 27, 2018 at 10:55

5 Answers 5

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

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

Sign up to request clarification or add additional context in comments.

Comments

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;        
    }
}

Comments

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

Comments

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'
]));

Comments

-1

Just use array_walk_recursive.

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

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.