0

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

0

3 Answers 3

2

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
)
Sign up to request clarification or add additional context in comments.

2 Comments

where the php tag questions there will be @Sahil Gulati's answer :D you are too active (y)
Gonna mark your comment as the answer, as you took the fact that the value might not be an array into consideration, something I didnt account for in the example array but no doubt could happen; thank you for the answer
1
$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

2 Comments

why complicating things with array_shift() instead of a simple $arr[$key] = $val[$key]; ?
@Jack Good question, please check array and check sub array keys, hope that will clear your doubt. We don't know the key of sub array thats why I used array_shift()
0
<?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);

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.