5

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
)
2
  • 4
    do you get this from database? if so then which? if this is mysql use mysqli_fetch_assoc instead of mysqli_fetch_array Commented Jul 6, 2013 at 4:13
  • 1
    Ok but have you any idea how to remove integer index. Commented Jul 6, 2013 at 4:17

3 Answers 3

14

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..

Sign up to request clarification or add additional context in comments.

1 Comment

I mean if array is not coming from database.What we can do for the same result.
4

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

)

2 Comments

I dont see the need to create a function... You can write the same thing just with no function like 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);
@gabrielem thanks (that call back native function "is_numeric").
3

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 )

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.