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.

Vipul Jethva
  • 647
  • 1
  • 7
  • 27
  • 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?](https://stackoverflow.com/questions/1319903/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
  • @VipulJethva just use one of example provided in above link, like `print_r(new RecursiveIteratorIterator(new RecursiveArrayIterator($arr)));` – ponury-kostek Nov 27 '18 at 10:57
  • Why have you added *without using RecursiveIteratorIterator*? What went wrong when you tried it? Are there any other solutions you don't want to use? If you know which ones you don't want, do you know which ones you *do* want? – iainn Nov 27 '18 at 11:00
  • @iainn There is no wrong when I tried to using RecursiveIteratorIterator. but I need to get the result using simple PHP. Hope you understand my requirement. This is very easy and simple method to get the result using RecursiveIteratorIterator function. But I don't it. – Vipul Jethva Nov 27 '18 at 11:04
  • @VipulJethva RecursiveArrayIterator is simple PHP - it's a built-in library ... – treyBake Nov 27 '18 at 12:44
  • @treyBake Yes, This is the simple method to use in the recursive array. But actually, I don't use any inbuilt function. – Vipul Jethva Nov 27 '18 at 13:06
  • @VipulJethva that makes next to no sense ... – treyBake Nov 27 '18 at 13:32
  • @treyBake Why this no sense? I need to build login without using the inbuilt function. As you agree with me that inbuilt function decrease your code length only. That's not what I require. – Vipul Jethva Nov 27 '18 at 13:57
  • @VipulJethva no.. that's not what inbuilt functions do .... it makes no sense why you wouldn't use default in built functions ... – treyBake Nov 27 '18 at 13:58

5 Answers5

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.

RomMer
  • 113
  • 12
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;        
    }
}
Sajjad Ali
  • 304
  • 3
  • 12
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
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'
]));
ponury-kostek
  • 7,824
  • 4
  • 23
  • 31
-1

Just use array_walk_recursive.

$r = [];
array_walk_recursive($arr, function ($e) use (&$r) {
    $r[] = $e;
});
kopiro
  • 689
  • 4
  • 11