简体   繁体   English

删除整数索引数组

[英]remove integer index an array

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
)

Database Solution:数据库解决方案:

To get only the associative array from mysql database use: mysqli_fetch_assoc() instead of mysqli_fetch_array() .要仅从 mysql 数据库中获取关联数组,请使用: mysqli_fetch_assoc()而不是mysqli_fetch_array()

mysqli_fetch_array() fetches the entire array - integer indexes as well as column names as keys. mysqli_fetch_array()获取整个数组 - 整数索引以及列名作为键。

mysqli_fetch_assoc() only fetches the column names as keys. mysqli_fetch_assoc()仅获取列名作为键。 - 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..如果你愿意,你也可以使用is_int() ..

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

)

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 ) ( [第一] => 第二 [第二] => 第二 [第三] => 第三 [第四] => v [第五] => 第五)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM