0

i have a array like this

Array
(
    [0] => Array
        (
            [to_id] => 3
            [email] => [email protected]
            [user_id] => 3
        )

    [2] => Array
        (
            [to_id] => 2
            [email] => [email protected]
            [user_id] => 2
        )

    [4] => Array
        (
            [to_id] => 5
            [email] => [email protected]
            [user_id] => 5
        )

)

and i would like to get array like below

Array
(
    [0] => Array
        (
            [email] => [email protected]
            [user_id] => 3
        )

    [2] => Array
        (
            [email] => [email protected]
            [user_id] => 2
        )

    [4] => Array
        (
            [email] => [email protected]
            [user_id] => 5
        )

)

i got this working by looping with foreach , but i would like to do it single array function ,is it possible ,please help

2
  • you would most likely be able to achieve this with an array_map or array_walk being used in conjunction with a closure Commented Feb 20, 2014 at 10:34
  • I would just copy the old array into a new array ommiting that key in the copy process Commented Feb 20, 2014 at 10:37

2 Answers 2

4
$array = array_map(
    function (array $ar) { return array_diff_key($ar, array_flip(array('to_id'))); },
    $array
);

There's no one single function which does this. You have to write a bit of code. You will also always have to loop through the array. You can only dress that loop up in different ways and dress the action of unsetting the key up in different ways. I wouldn't necessarily think anything is anymore efficient or clearer than a straight forward loop. But, well, go with the above if you're looking for something more fancy.

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

4 Comments

this will do the work , but i need some things like unset($array['to_id']); will loop through and remove in whole array
Nothing like that exists. You could write a function like unset_key($array, 'to_id') which would do it. But it doesn't exist by default.
thanks ,how to inform the php community to create some predefined function to do like so in next release
bugs.php.net - But... I don't think they're going to. PHP already has enough functions as is, nobody needs the hyper specific function in the core when it's trivially implemented in user land code.
0
//This is the array name in which your data is stored.
$my_array = array($array_data);

// now lets get one level inside the array 
foreach($my_array as $key => $value ){

// ok now unset un-wanted index from the array into a temporary array
unset($value['to_id']);

// overwrite our temporary array to original array keeping the keys same as before.
$my_array[$key] = $value;
//Thats it your Problem Solved :)
}

echo "<pre>";
print_r($my_array);
echo "</pre>";

2 Comments

my questions is not to do with foreach
This may not be the best method however it does work. Try to avoid irrelevant details such as your "Bonus" section.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.