0

I'm little bit confuse in this concept

Actually I'm having an array like this

    $arr = array("0"=>array("username"=>"username1"),"1"=>array("username"=>"username2"),"2"=>array("username"=>"username3"),"3"=>array("username"=>"username4"));
    echo "<pre>";print_r($arr);
    $finalArray = call_user_func_array('array_merge', $arr);
    echo "<pre>";print_r($finalArray);

Finally from this I'm getting an array like this

Array
(
    [username] => username4
)

But I need all the values like as follows

Array
(
    [0] => username1
    [1] => username2
    [2] => username3
    [3] => username4
)

How should I do this?..Could someone please help me..

Thank you,

0

2 Answers 2

2

Simple solution using array_column function(available since PHP 5.5):

$result = array_column($arr, 'username');
Sign up to request clarification or add additional context in comments.

1 Comment

So simple.. Thank you :)
1

You can write your own function to get usernames

Try this

<?php
function getUserNames($arr){
    $userNames = array();
    foreach ($arr as $key => $value){
        $userNames[$key] = $value["username"];
    }   
    return $userNames;
}
$arr = array("0"=>array("username"=>"username1"),"1"=>array("username"=>"username2"),"2"=>array("username"=>"username3"),"3"=>array("username"=>"username4"));
echo "<pre>";print_r($arr);
$finalArray = getUserNames($arr);
echo "<pre>";print_r($finalArray);
?>

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.