Delete integer index array

I am working on a website where we need to remove the index of an array, where type is an integer. Do you have any ideas or suggestions. My array looks like this:

Array ( [0] => first [first] => second [1] => second [second] => second [2] => third [third] => third [3] => forth [forth] => v [4] => fifth [fifth] => fifth ) 

How can we remove an integer index from an array. One more remark that we do not have a static array, we do not know how many indexes are there.

You need:

 Array ( [first] => second [second] => second [third] => third [forth] => v [fifth] => fifth ) 
+4
source share
3 answers

Database Solution:

To get only an associative array from the mysql database, use mysqli_fetch_assoc() instead of mysqli_fetch_array() .

mysqli_fetch_array() retrieves integer integer indices as well as column names as keys.

mysqli_fetch_assoc() only selects column names as keys. - thereby getting rid of whole keys.


Common decision:

To do what you requested in general, I would use:

 foreach($array as $key => $value){ if(is_numeric($key)) unset($array[$key]); } 

You can also use is_int() if you want ..

+7
source

Another way to do this: -

 $array1 = array("6566"=>"zd xf", "2"=>2, "c"=>3, "d"=>4, "e"=>5); $keys=array_filter(array_keys($array1), "is_numeric"); $out =array_diff_key($array1,array_flip($keys)); print_r($out); 

output:

 Array ( [c] => 3 [d] => 4 [e] => 5 ) 
+3
source

remove the integer index value of the array.

 $array1=array(); $array = array(0 => first,first => second,1 => second,second => second,2 => third,third => third,3 => forth,forth => v,4 => fifth,fifth => fifth); foreach ($array as $key=>$value){ if(gettype($key)=='integer'){ unset($key); unset($value); }else{ $array1[$key]=$value; } } print_r($array1); 

out is as follows.

 Array 

([first] => second [second] => second [third] => third [forward] => v [fifth] => fifth)

+3
source

Source: https://habr.com/ru/post/1490010/


All Articles