how to remove an element from an array with the data-new key

array("data-new" => 1,"data-name" => "1","data-balanse" => 1) 

    2 answers 2

    to remove the first (zero) element of an array, use array_unshift() . This method will return the first element and change the array passed by reference. (for removal from the end use array_pop() )

     $first = array_unshift($data) 

    another option would be to use array_slice() , here the array will be returned, not changed.

      $new = array_slice($data, 1); 

    To remove a given key, use the unset() construct, and unset can take several arguments at once, and generally apply not only to array keys.

     unset($data['key']) unset($data['key1'], $data['key2']) 

    if the keys for deletion are in a separate array, you can use the functions array_diff_key()

      $remove = ['key1', 'key2']; $new = array_diff_key($data, array_flip($remove)); 
       $array = ["data-new" => 1,"data-name" => "1","data-balanse" => 1]; unset($array['data-new']); 
      • writes unexpected t_unset - Aslero
      • it means you do not have the "data-new" key in the array, check the code that I wrote to you, it works correctly - madfan41k
      • here is my array, though in json {"data-new": "2", "hdate": "1", "duedate": "1", "rub": "3", "eur": "1" , "debit": "2", "credit": "3", "balanse": "3"} - Aslero
      • $ a = json_decode ('{"data-new": "2", "hdate": "1", "duedate": "1", "rub": "3", "eur": "1", " debit ":" 2 "," credit ":" 3 "," balanse ":" 3 "} ', true); unset ($ a ['data-new']); print_r ($ a); - madfan41k
      • So I form an array foreach ($ array as $ value) {$ str = array (); foreach ($ value as $ key => $ val) {// if ($ key == "data-new") $ str [$ key] = $ val; } if ($ str ['data-new'] == 2) {unset ($ str ['data-new']); }} - Aslero