Tell me more ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

I am working on a website where we need to remove the index of an array where type is integer. Have you any idea or suggestion regarding. 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 we can remove integer index from array.One more thing to note that we have not static array we do not know how many index is there.

Need like this :

Array
(  
    [first] => second  
    [second] => second  
    [third] => third  
    [forth] => v
    [fifth] => fifth
)
share|improve this question
3  
do you get this from database? if so then which? if this is mysql use mysqli_fetch_assoc instead of mysqli_fetch_array – Yotam Omer Jul 6 at 4:13
Yeh @YotamOmer Thanks for this – sonusindhu Jul 6 at 4:16
adding as answer.. – Yotam Omer Jul 6 at 4:16
Ok but have you any idea how to remove integer index. – sonusindhu Jul 6 at 4:17

3 Answers

up vote 3 down vote accepted

Database Solution:

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

mysqli_fetch_array() fetches the entire array - integer indexes as well as column names as keys.

mysqli_fetch_assoc() only fetches the column names as keys. - thus getting rid of integer keys.


General Solution:

To do what you asked 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 like..

share|improve this answer
I mean if array is not coming from database.What we can do for the same result. – sonusindhu Jul 6 at 4:19
updated - see new code – Yotam Omer Jul 6 at 4:20
Thanks Looks Great!! – sonusindhu Jul 6 at 4:25

remove the integer index value of 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 put like this.

Array

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

share|improve this answer

Another way to do this :-

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

output :

Array
(
[c] => 3

[d] => 4

[e] => 5

)
share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.