PHP:如果子数组只有一个值,请用该值替换该数组

My question is rather straight forward, yet I am very unsure on how to do it in an effective way.

Say we have an array like this:

array (
    [0] => array(
        [0] => "a"
        )
    [1] => array(
        [0] => "b"
        [1] => "c"
        )
    ["a"] => array(
       ["b"] => "d"
       )
    )

It does not matter to me wether or not keys are preserved, what I ultimately want to end up with is this (with or without key preservation, doesnt matter too much):

Expected output:

array (
    [0] => "a"
    [1] => array(
        [0] => "b"
        [1] => "c"
        )
    ["a"] => "d"
    )

I hope you can help!

~ Troels

Here we are inserting the first value of an array. when count of $value is equal to 1.

Try this code snippet here

<?php
$array=array (
    0 => array(
        0 => "a"
        ),
    1 => array(
        0 => "b",
        1 => "c"
        ),
    "a" => array(
       "b" => "d"
       )
    );
foreach($array as $key => $value)
{
    if(is_array($value) && count($value)==1)
    {
        $array[$key]=current($value); //replace array with lone value
    }
}
print_r($array);

Output:

Array
(
    [0] => a
    [1] => Array
        (
            [0] => b
            [1] => c
        )

    [a] => d
)
$arr = array (
    0 => array(
        0 => "a"
        ),
    1 => array(
        0 => "b",
        1 => "c"
        ),
    "a" => array(
       "b" => "d"
       )
    );
    echo "<pre>"; print_r($arr);
    foreach($arr as $key=>$val){
        if(is_array($val) && count($val) == 1){
           $arr[$key] = array_shift($val); //get first element of the array which have only one element
        }
    }
      echo "<pre>"; print_r($arr);

PHP Demo

<?php

$temp = array (
    0 => array(
        0 => "a"
        ),
    1 => array(
        0 => "b",
        1 => "c"
        ),
    "a" => array(
       "b" => "d"
       )
    );
    echo "<pre>"; print_r($temp);
     $new_arr = array();

 foreach ($temp as $key => $value) {
    if(count($value)>1)
        $new_arr[$key] = $value;
    else
    {
        $new_arr[$key] = array_shift($value);
    }
 }
 echo '<pre>'; print_r($new_arr);