删除php数组中不需要的键/值对? [关闭]

$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");

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

$data = array("0"=>"1","id"=>"1","1"=>"mani","name"=>"mani","2"=>"ssss","lname"=>"ssss");
$new_data = array();
foreach($data as $k => $v)
if(strlen($k)>1) $new_data[$k] = $v;

print_r($new_data);

I'm a little confused as to what exactly you're looking for. You give examples of output, but they look like code. If you want your output to look like code you're going to need to be clearer.

Looking at tutorials and documentation will do you alot of good in the long run. PHP.net is a great resource and the array documentation should help you out alot with this: http://php.net/manual/en/function.array.php

The Code-Snippet below contains Self-Explanatory Comments. It might be of help:

<?php

    // SEEMS LIKE YOU WANT TO REMOVE ITEMS WITH NUMERIC INDEXES...

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

    // SO WE CREATE 2 VARIABLES TO HOLD THE RANGE OF INTEGERS
    // TO BE USED TO GENERATE A RANGE OF ARRAY OF NUMBERS
    $startNum   = 0;        //<==   START-NUMBER FOR OUR RANGE FUNCTION
    $endNum     = 10;       //<==   END-NUMBER FOR OUR RANGE FUNCTION

    // GENERATE THE RANGE AND ASSIGN IT TO A VARIABLE
    $arrNum     = range($startNum, $endNum);

    // CREATE A NEW ARRAY TO HOLD THE WANTED ARRAY ITEMS
    $newData    = array();

    // LOOP THROUGH THE ARRAY... CHECK WITH EACH ITERATION
    // IF THE KEY IS NUMERIC... (COMPARING IT WITH OUR RANGE-GENERATED ARRAY)
    foreach($data as $key=>$value){
        if(!array_key_exists($key, $arrNum)){
            // IF THE KEY IS NOT SOMEHOW PSEUDO-NUMERIC,
            // PUSH IT TO THE ARRAY OF WANTED ITEMS... $newData
            $newData[$key]  = $value;
        }
    }

    // TRY DUMPING THE NEWLY CREATED ARRAY:
    var_dump($newData);
    // YIELDS::
    array (size=3)
      'id' => string '1' (length=1)
      'name' => string 'mani' (length=4)
      'lname' => string 'ssss' (length=4)

Or even concisely, you may walk the Array like so:

<?php

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

    array_walk($data, function($value, $index) use(&$data) {
        if(is_numeric($index)){
            unset($data[$index]);
        }
    });

    var_dump($data);
    // YIELDS::
    array (size=3)
      'id' => string '1' (length=1)
      'name' => string 'mani' (length=4)
      'lname' => string 'ssss' (length=4)