dspld86684 2012-05-24 18:38
浏览 13
已采纳

如果数组等于数组PHP

Is possible to make if($array1[0]=>somevalue == $array2[0]=>somevalue){echo true;}else{echo false;}? because I cant make it work, can you help me please,

Example: $array1([0] => 'Joe', [2] => 'Pairs'); $array2([0] => 'info' => array([0] => 'joe'));

foreach($array2->info as $info){
    foreach($array1 as $name){
        if($name == $info[0]){
             echo 'true';
           }
           else{
             echo 'false';
           }
      }
  }

And this is the real code,

The arrays:

 Array
(
[0] => SimpleXMLElement Object
    (
        [companyLocationInfo] => Array
            (
                [0] => SimpleXMLElement Object
                    (
                        [companyName] => AVIS
                        [name] => NYCC07
                        [line1] => 420 EAST 90TH STREET
                    )
                [2] => SimpleXMLElement Object
                    (
                        [companyName] => AVIS
                        [name] => NYCC06
                        [line1] => 310 EAST 64TH STREET
                    )
                [3] => SimpleXMLElement Object
                    (
                        [companyName] => AVIS
                        [name] => NYCC01
                        [line1] => 68 EAST 11TH STREET
                    )

            )

        [rates] => Array
            (
                [0] => SimpleXMLElement Object
                    (
                        [pickupDropoffLocations] => Array
                            (
                                [0] => SimpleXMLElement Object
                                    (
                                        [companyName] => AVIS
                                        [name] => NYCC07
                                    )
                            )
                        [vehicleRentalPrefType] => CCAR
                        [rateAmount] => 83.99
                        [rateCurrency] => USD
                    )
                [2] => SimpleXMLElement Object
                    (
                        [pickupDropoffLocations] => Array
                            (
                                [0] => SimpleXMLElement Object
                                    (
                                        [companyName] => AVIS
                                        [name] => NYCC06
                                    )
                            )
                        [vehicleRentalPrefType] => CCAR
                        [rateAmount] => 110.54
                        [rateCurrency] => USD
                    )
                [3] => SimpleXMLElement Object
                    (
                        [pickupDropoffLocations] => Array
                            (
                                [0] => SimpleXMLElement Object
                                    (
                                        [companyName] => AVIS
                                        [name] => NYCC01
                                    )
                            )
                        [vehicleRentalPrefType] => CCAR
                        [rateAmount] => 210.65
                        [rateCurrency] => USD
                    )

            )

    )

)

and this is the code:

$results_array = array();

foreach($result[0]->rates as $rate) {
    foreach($result[0]->companyLocationInfo as $info) {
        if($info->name == $rate->pickupDropoffLocations[0]->name) {
            $results_array[] = array(
                'line1' => $info->line1,
                'name' => $info->locationDetails->name,
                'companyName' => $info->companyName,
                'vehicleRentalPrefType' => $rate->vehicleRentalPrefType
            );
        }
    }
}
print_r($results_array);

Thank you.

  • 写回答

1条回答 默认 最新

  • douhuan3448 2012-05-24 18:46
    关注

    What you're trying to do should be possible. If the code above is what you're actually trying to run, then your first problem is that you're comparing the values "Joe" and "joe" and those are not the same.

    If you want a case-insensitive comparison, use if (strcasecmp($var1, $var2) == 0) (it returns zero if the two strings are the same, without considering case).

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

报告相同问题?

悬赏问题

  • ¥15 R语言Rstudio突然无法启动
  • ¥15 关于#matlab#的问题:提取2个图像的变量作为另外一个图像像元的移动量,计算新的位置创建新的图像并提取第二个图像的变量到新的图像
  • ¥15 改算法,照着压缩包里边,参考其他代码封装的格式 写到main函数里
  • ¥15 用windows做服务的同志有吗
  • ¥60 求一个简单的网页(标签-安全|关键词-上传)
  • ¥35 lstm时间序列共享单车预测,loss值优化,参数优化算法
  • ¥15 Python中的request,如何使用ssr节点,通过代理requests网页。本人在泰国,需要用大陆ip才能玩网页游戏,合法合规。
  • ¥100 为什么这个恒流源电路不能恒流?
  • ¥15 有偿求跨组件数据流路径图
  • ¥15 写一个方法checkPerson,入参实体类Person,出参布尔值