dswsl2016 2012-04-30 17:09
浏览 24
已采纳

无法以不同方式创建的array_unique合并数组并以不同方式显示

I have this two arrays.

one is generated using a standard SELECT query, the other one is generated using this statement:

$file = file(DOC_ROOT."robots.txt");
foreach($file as $text) {
$test[] = $text;
}

the first array output like this:

Array
(
    [0] => Disallow: /admin/
    [1] => Disallow: /m/
    [2] => Disallow: /private/
)

the other like this:

Array
(
[4] => Disallow: /admin/

[5] => Disallow: /m/

[6] => Disallow: /private/

[7] => Disallow: /success.php
)

the problem now is that after merging the two arrays, array_unique doesn't work, im guessing maybe its caused by the spaces generated by the second array, i've tried removing them but with no luck. now my question is, is it really the spaces that cause this or is there something wrong with my flow? for your convenience here is the array code, not that it would matter that much. How will i be able to solve this problem? Thank you.

$result = array_merge($lines,$test);
    $result = array_unique($result);
    echo '<pre>';
    echo print_r($result);
    echo '</pre>';

the result:

Array
(
[0] => Disallow: /admin/
[1] => Disallow: /m/
[2] => Disallow: /private/

[32] => 

[33] => Disallow: /admin/

[34] => Disallow: /m/

[35] => Disallow: /private/

[36] => Disallow: /success.php
)
  • 写回答

1条回答 默认 最新

  • doolo00026 2012-04-30 17:10
    关注

    array_unique() works on array's values. In your example there aren't any value in common so it will not delete any values.

    And yes spaces make array_unique see 2 different values, so basicalyl these 2 values are not the same:

    [0] => 'hello1   hello2'
    [1] => 'hello1 hello2'
    

    and array_unique leaves both values

    You have that space between keys within pre because file() keeps and . So do a:

    file(,FILE_IGNORE_NEW_LINES);
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥100 支付宝网页转账系统不识别账号
  • ¥15 基于单片机的靶位控制系统
  • ¥15 AT89C51控制8位八段数码管显示时钟。
  • ¥15 真我手机蓝牙传输进度消息被关闭了,怎么打开?(关键词-消息通知)
  • ¥15 下图接收小电路,谁知道原理
  • ¥15 装 pytorch 的时候出了好多问题,遇到这种情况怎么处理?
  • ¥20 IOS游览器某宝手机网页版自动立即购买JavaScript脚本
  • ¥15 手机接入宽带网线,如何释放宽带全部速度
  • ¥30 关于#r语言#的问题:如何对R语言中mfgarch包中构建的garch-midas模型进行样本内长期波动率预测和样本外长期波动率预测
  • ¥15 ETLCloud 处理json多层级问题