【问题标题】:Display data from two arrays having one common point显示来自具有一个公共点的两个数组的数据
【发布时间】:2021-10-10 13:24:29
【问题描述】:

我有两个数组。 第一个是关于汇率的,我的控制台显示是这样的:

{
  "exchange_rate": [
    {
      "id": "978",
      "start_dateTime": "2021-08-01 07:35:02",
      "target_value": "1.00000",
      "currency_value_euro": "0.84097",
      "currency_value_dollar_us": "1.00000",
      "id_currency": "1",
      "currency": "Dollar am\u00e9ricain",
      "currency_symbol": "$US"
    },
    {
      "id": "980",
      "start_dateTime": "2021-08-01 07:35:02",
      "target_value": "1.00000",
      "currency_value_euro": "1.17454",
      "currency_value_dollar_us": "0.71600",
      "id_currency": "2",
      "currency": "Livre sterling",
      "currency_symbol": "\u00a3"
    }
  ]
}

这些数据来自数据库,我可以通过使用 jQuery 选择特定日期来显示它。

第二个数组只包含 id_currency 在我的控制台中是这样的:Array ( [0] => 1 [1] => 2 [2] => 3 [3] => 4 [4] => 5 在我的网站上,我希望能够按特定货币和日期显示特定汇率。 在这里我的问题出现了,我找不到在第一个数组上循环的方法,然后在第二个数组上再次在里面循环并比较两者,比如第一个数组有 id_currency 1,第二个数组有 id_currency 1,然后显示完整的行从第一个数组。

我尝试了几件事,但没有任何效果,最后我尝试了这个:

foreach ($res as $row){
            $idBDD = $row['id_currency'];
            $symbolBDD = $row['currency_symbol'];
            echo $idBDD;
            echo $symbolBDD;
            //var_dump($row);
            /*foreach ($arr as $line){
                $idCheckbox = $line;
                echo $idCheckbox;
            }
            if ($idBDD == $idCheckbox){
                echo 'fine';
            }
            */
        }

感谢您的帮助

【问题讨论】:

  • 这句话compare both like if first array has id_currency 1 and second array has id_currency 1 then display the complete line from first array.是什么意思?您如何比较 id_currency 以及显示完整行是什么意思?
  • 第一个数组 ['exchange_rate'] 包含每个货币的数组。每种货币都有一个 id、start_datetime、id_currency 等。
  • 那么,你需要比较什么,比较之后,你想用它做什么?
  • 我可以通过选择特定日期来显示第一个。第二个数组包含特定货币的 id_currency(用复选框选择),如果在第二个数组中我选择了 id_currency 为 1 的欧元(例如),我想“比较”,我查看 [exchange_rate] 数组并显示数组(包含我脑海中完整的信息(日期、符号等)),其中 id_currency 为 1。如果两个数组具有相同的 id_currency,我想显示第一个数组中的相应信息。
  • 你的意思是如果数组之间有公共字段,显示所有公共字段?

标签: php arrays loops foreach


【解决方案1】:

您需要使用$res["exchange_rate"] 访问该数组,然后循环遍历它。

<?php
$res = [
    "exchange_rate" => [
        [
            "id" => "978",
            "start_dateTime" => "2021-08-01 07:35:02",
            "target_value" => "1.00000",
            "currency_value_euro" => "0.84097",
            "currency_value_dollar_us" => "1.00000",
            "id_currency" => "1",
            "currency" => "Dollar américain",
            "currency_symbol" => "\$US"
        ],
        [
            "id" => "980",
            "start_dateTime" => "2021-08-01 07:35:02",
            "target_value" => "1.00000",
            "currency_value_euro" => "1.17454",
            "currency_value_dollar_us" => "0.71600",
            "id_currency" => "2",
            "currency" => "Livre sterling",
            "currency_symbol" => "£"
        ]
    ]
];

$output = null;

foreach ($res["exchange_rate"] as $row) {
    if (!isset($output)) {
        $output = $row;
    }
    $output = array_intersect_assoc($output, $row);
}

var_dump($output);

【讨论】:

  • 感谢您的回答我已经尝试过您的演示并且它有效,但是您如何比较 ['exchange_rate'] 数组的 id_currency 和第二个数组的 id_currency ?这是第二个数组,必须确定 exchange_rate 数组的哪一行应该显示..(对不起,如果我的解释不清楚)
  • @grollote,我更新了我的答案。这是你想要的吗?我只得到所有数组中的所有公共字段。
  • @grollote,如果这个答案有帮助或者你认为它有用。请接受它,这样它可以帮助将来遇到同样问题的人。
【解决方案2】:

我认为您可以这样做,当然,如果您在循环中,则不需要放置索引。

阅读更多 array_diff() https://www.php.net/manual/pt_BR/function.array-diff.php

$array = [
    "exchange_rate" => [
        [
            "id" => "978",
            "start_dateTime" =>  "2021-08-01 07:35:02",
            "target_value"=>  "1.00000",
            "currency_value_euro"=>  "0.84097",
            "currency_value_dollar_us"=>  "1.00000",
            "id_currency"=>  "1",
            "currency"=>  "Dollar am\u00e9ricain",
            "currency_symbol" =>  "US"
        ],[
            "id"=>  "980",
            "start_dateTime"=>  "2021-08-01 07=> 35=> 02",
            "target_value"=>  "1.00000",
            "currency_value_euro"=>  "1.17454",
            "currency_value_dollar_us"=>  "0.71600",
            "id_currency"=>  "2",
            "currency"=>  "Livre sterling",
            "currency_symbol"=>  "\u00a3"
        ]
    ]
];

$array1 = $array['exchange_rate'][0];
$array2 = $array['exchange_rate'][1];

$result = array_diff( $array1,  $array2);

var_dump($result);

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2012-05-20
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多