Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/263.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/arrays/13.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
在PHP数组中插入唯一值_Php_Arrays - Fatal编程技术网

在PHP数组中插入唯一值

在PHP数组中插入唯一值,php,arrays,Php,Arrays,例如,如果我回显$studentID,结果将是123345567123567,我的数据包含冗余值 我想要的只是123345和567 在将$studentID分配到$arr之前,我应该做什么?我不能使用in_array(),因为我循环遍历一堆字符串,而不是本例中的数组。获取该信息并将其放入数组中,然后对in_数组进行任何其他类型的比较。像你一样独一无二 $dom = $html->find('.someClass',0)->find('.studentID'); $arr = ar

例如,如果我回显
$studentID
,结果将是
123345567123567
,我的数据包含冗余值

我想要的只是123345和567


在将$studentID分配到$arr之前,我应该做什么?我不能使用in_array(),因为我循环遍历一堆字符串,而不是本例中的数组。

获取该信息并将其放入数组中,然后对in_数组进行任何其他类型的比较。像你一样独一无二

 $dom = $html->find('.someClass',0)->find('.studentID');

$arr = array();

foreach ($dom as $data) {
    if(isset($data->find('.studentID',0)->plaintext)){

        $studentID = $data->find('.studentID',0)->plaintext;

            $arr[] = array(
                'studentID'=>$studentID
            );

    }

}
接受@dan08回答

$studentIdArr = explode(", ", $studentId);


将返回一个非冗余值的唯一数组,并键入

您可能(没有足够的代码来确保)将学生id用作$arr数组中的键。分配数据时,只需执行以下操作:

<?php
$result = array_unique($arr);
print_r($result);
?>

这样您就知道学生id在列表中只出现一次。只要确保仔细检查数据结构,以确保它对您的用例有效。

您是否看到了
array\u unique
@dan08我应该把它放在哪里?我把它放在我的循环之外,它的错误是:数组到字符串的转换,这样可以避免冗余吗?因为密钥不能是冗余的?只要你只是避免学生id上的冗余,它就应该这样做。我试过了,你的解决方案成功了!这是最简单的方法,为什么人们会建议array_unique?我的情况行吗?你怎么看?array_unique正如azngunit81所建议的那样可能在你的情况下工作,但前提是子数组中的所有数据对于每个学生id总是相同的。如果你有一个简单的学生id数组,那么array_unique可能是一个好的解决方案。子数组中的所有数据?我不明白。
        $arr[$studentID] = array(
            'studentID'=>$studentID
        );