在php中匹配两个多维数组

时间:2017-05-23 06:33:02

标签: php arrays multidimensional-array

我有两个多维数组invoice_data,另一个是post_data。我的问题是我必须将invoice_number格式invoice_data数组与post_data数组匹配。如果匹配创建post_data数组,如果不匹配则转到else条件show invoice_number in the array。我试过但不能正常工作我认为犯了错误。

invoice_data数组

Array
(
    [0] => stdClass Object
        (
            [invoice_no] => H146/17-18
            [invoice_date] => 2017-05-2
        )

)

post_data数组

Array
(

    [0] => Array
        (
            [0] => H146/17-18
            [1] => 1
            [2] => 71023910
            [3] => tes
            [4] => 1.01
            [5] => CTM
            [6] => 3935.19
            [7] => 1
            [8] => 0
            [9] => 00
            [10] => 0
            [11] => 0
            [12] => 0
        )

    [1] => Array
        (
            [0] => H146/17-18
            [1] => 2
            [2] => 71023910
            [3] => test2
            [4] => 1.02
            [5] => CTM
            [6] => 3567.90
            [7] => 1
            [8] => 0
            [9] => 00
            [10] => 0
            [11] => 0
            [12] => 0
        )

    [2] => Array
        (
            [0] => H146/17-18
            [1] => 3
            [2] => 71023910
            [3] => test3
            [4] => 1.01
            [5] => CTM
            [6] => 3462.96
            [7] => 1
            [8] => 0
            [9] => 00
            [10] => 0
            [11] => 0
            [12] => 0
        )

    [3] => Array
        (
            [0] => H146/17-19
            [1] => 12
            [2] => 71023910
            [3] => test4
            [4] => 1.04
            [5] => CTM
            [6] => 2466.05
            [7] => 1
            [8] => 0
            [9] => 00
            [10] => 0
            [11] => 0
            [12] => 0
        )
   [3] => Array
       (
            [0] => H146/17-20
            [1] => 12
            [2] => 71023910
            [3] => test4
            [4] => 1.04
            [5] => CTM
            [6] => 2466.05
            [7] => 1
            [8] => 0
            [9] => 00
            [10] => 0
            [11] => 0
            [12] => 0
    )

)

我尝试过的代码

$dataSet = [];
$error_dispay = [];
$product_fob = 0;
$invoice_fob = 0;
foreach ($invoice_data as $invoicedata) {
    $invoice_fob += $invoicedata->invoice_value;
       foreach ($post_data as $postdata) {
          if (!empty($postdata)) {
              if ($invoicedata->invoice_no == $postdata[0]) {
                  $product_fob += ($postdata[6] * $postdata[4]);
                   $dataSet[] = [
                       'session_token' => $session_token,
                       'invoice_no' => $postdata[0],
                       'ritc' => $postdata[2],
                       'product_description' => $postdata[3],
                       'unit_price' => $postdata[4],
                       'quantity' => $postdata[6],
                       'amount' => ($postdata[6] * $postdata[4]),
                       'fob' => ($postdata[6] * $postdata[4]),
                       'pmv_value' => $postdata[7],
                    ];
                } else {
                    $error_dispay[] = [
                            'not_match' => $invoicedata->invoice_no
                        ];
                }
             }
           }
        }

如果不匹配则进入else条件,数组显示如下。请参阅发票号H146/17-20有一条记录,但循环显示多个时间错误。我该怎么办呢。

    Array
(
    [0] => Array
        (
            [not_match] => H146/17-18
        )

    [1] => Array
        (
            [not_match] => H146/17-18
        )

    [2] => Array
        (
            [not_match] => H146/17-18
        )

    [3] => Array
        (
            [not_match] => H146/17-19
        )

    [4] => Array
        (
            [not_match] => H146/17-19
        )

    [5] => Array
        (
            [not_match] => H146/17-19
        )

    [6] => Array
        (
            [not_match] => H146/17-19
        )

    [7] => Array
        (
            [not_match] => H146/17-19
        )

    [8] => Array
        (
            [not_match] => H146/17-19
        )

    [9] => Array
        (
            [not_match] => H146/17-19
        )

    [10] => Array
        (
            [not_match] => H146/17-19
        )

    [11] => Array
        (
            [not_match] => H146/17-20
        )

    [12] => Array
        (
            [not_match] => H146/17-20
        )

    [13] => Array
        (
            [not_match] => H146/17-20
        )

    [14] => Array
        (
            [not_match] => H146/17-20
        )

)

请告诉我我的错误是什么。提前谢谢。

3 个答案:

答案 0 :(得分:0)

你的第一个数组是对象,所以它不会在你的第一个foreach中检查第三级值。您必须使用object元素来解决此问题。  用这一行更新它:

foreach ($invoice_data[0] as $invoicedata) {

由于您要将对象与数组进行比较,因此您需要更新编码标准,但这是一个快速解决方法。

答案 1 :(得分:0)

在foreach看来你不需要传递索引。

更改以下行 foreach($ invoice_data as $ invoicedata){with foreach($ invoice_data [0] as $ invoicedata){因为这个数组是对象。

if($ invoicedata-> invoice_no == $ postdata [0]){ 请使用以下代码更改上述行 if($ invoicedata-> invoice_no == $ postdata){

答案 2 :(得分:0)

反转循环数组意味着socialName为外循环,- socialNames = ['facebook', 'twitter', 'instagram'] each socialName in socialNames // - social = this[socialName] social = socialName a(href=social.url) div img.ug-profile-socials--icon(src=chrome.extension.getURL('public/socials/'+ social + '.png')) div span.ug-profile-socials-followers !{social.followers} 为内循环,

post_data

如果仍然无效,请执行

激活错误报告
invoice_data

然后检查你得到的错误并根据它进行调试。