跳过阵列关键,如果它已经存在 [英] Skip array key if it already exists

查看:123
本文介绍了跳过阵列关键,如果它已经存在的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我建立一个这样的数组:

  $阵列=阵列(); //使用空单开始$数组[] ='foobar的';
$数组[] ='你好';
$数组[] ='foobar的';
$数组[] =世界;
$数组[] ='foobar的';

正如你所看到的, foobar的重复三次。我怎么让这个数组跳过,如果它已添加previously的关键?因此,在这种情况下,第二和第三 foobar的不应添加


解决方案

 < PHP    $阵列=阵列(); //使用空单开始    $数组[] ='foobar的';
    $数组[] ='你好';
    $数组[] ='foobar的';
    $数组[] =世界;
    $数组[] ='foobar的';    $阵列= array_unique($数组); //删除所有重复    后续代码var_dump($数组);
?>

从PHP手册

I build an array like this:

$array = array(); // start with empty one

$array[] = 'foobar';
$array[] = 'hello';
$array[] = 'foobar';
$array[] = 'world';
$array[] = 'foobar';

As you can see, foobar is repeated three times. How do I make it so that the array skips the key if it was already added previously? So in this case, the second and third foobar should not be added.

解决方案

<?php

    $array = array(); // start with empty one

    $array[] = 'foobar';
    $array[] = 'hello';
    $array[] = 'foobar';
    $array[] = 'world';
    $array[] = 'foobar';

    $array = array_unique($array);  // removes all the duplicates

    var_dump( $array );
?>

From PHP Manual

这篇关于跳过阵列关键,如果它已经存在的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆