douan2907 2014-12-11 09:50
浏览 101
已采纳

php从两个数组中获取匹配值

I Have two arrays like this

$array1 = Array ( [0] => Dutch [1] => Kannada [2] => Vietnamese [3] => Belarusian );
$array2 = Array ( [0] => English [1] => Germany [2] => Vietnamese [3] => Spanish [4] => Hindi );

I want to get the same values present in both arrays. So for that I used array_intersect like this

<?php
$array1 = Array ( [0] => Dutch [1] => Kannada [2] => Vietnamese [3] => Belarusian );
$array2 = Array ( [0] => English [1] => Germany [2] => Vietnamese [3] => Spanish )
$result = array_intersect($array1, $array2);
print_r($result);
?>

But its not showing the matching values. Even it is showing the error like

Parse error: syntax error, unexpected '[', expecting ')'

I think the array code structure is fine then why I am getting this error and not getting the matching values from two arrays? Any help and suggestions will be really appreciable. Thanks

  • 写回答

2条回答 默认 最新

  • dqhgjay5753 2014-12-11 09:52
    关注

    This should work for you:

    (First you declared your array wrong and forgot a ;)

    $array1 = array ("Dutch", "Kannada", "Vietnamese", "Belarusian");
    $array2 = array ("English", "Germany", "Vietnamese", "Spanish");
    $result = array_intersect($array1, $array2);
    print_r($result);
    

    Output:

    Array ( [2] => Vietnamese )
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 PointNet++的onnx模型只能使用一次
  • ¥20 西南科技大学数字信号处理
  • ¥15 有两个非常“自以为是”烦人的问题急期待大家解决!
  • ¥30 STM32 INMP441无法读取数据
  • ¥15 R语言绘制密度图,一个密度曲线内fill不同颜色如何实现
  • ¥100 求汇川机器人IRCB300控制器和示教器同版本升级固件文件升级包
  • ¥15 用visualstudio2022创建vue项目后无法启动
  • ¥15 x趋于0时tanx-sinx极限可以拆开算吗
  • ¥500 把面具戴到人脸上,请大家贡献智慧,别用大模型回答,大模型的答案没啥用
  • ¥15 任意一个散点图自己下载其js脚本文件并做成独立的案例页面,不要作在线的,要离线状态。