Manikanta Siripella Manikanta Siripella - 22 days ago 7
PHP Question

Removing unwanted key/value pair in php array?

$data = array("0"=>"1","id"=>"1","1"=>"mani","name"=>"mani","2"=>"ssss","lname"=>"ssss");


above is my output but i want an array like below format. Please help me.

Correct Output:

$data = array ("id"=>"1","name"=>"mani","lname"=>"ssss");

Answer

check this, use is is_numeric to check number or string.

$data = array("0"=>"1","id"=>"1","1"=>"mani","name"=>"mani","2"=>"ssss","lname"=>"ssss");

foreach ($data as $key => $val)
{
    if(!is_numeric($key))
    {
        $new_array[$key] = $val;
    }
}  

print_r($new_array);

OUTPUT :

Array
(
    [id] => 1
    [name] => mani
    [lname] => ssss
)

DEMO

Comments