PHP 在数组中搜索值

问题描述 投票:0回答:0

我无法找出如何在特定数组中搜索索引/匹配值。

我可以列出并格式化所有内容,但我不确定到底发生了什么(它看起来不像数组中的数组,或者是吗?)并且不知道如何搜索特定索引/ 在这种特殊情况下的值。

示例:

{
  "AppName": {
    "date": "20230801",
    "version": "6.8.48"
  },
  "Invoices": [
    {
      "Administration": {
        "id": 2,
        "code": "TEST",
        "description": "Test Administration"
      },
      "Invoice": {
        "id": 4350,
        "type": "",
        "invoiceNumber": 2340323,
        "date": "2023-07-31T00:00:00.000Z",
        "dueDate": "2023-08-14T00:00:00.000Z",
        "amount": 196.3
      },
      "Relation": {
        "nr": 102616,
        "name": "John Doe"
      },
      "TermsOfPayment": {
        "id": 11,
        "description": "Prepayment"
      }
    },
    {
      "Administration": {
        "id": 2,
        "code": "TEST",
        "description": "Test Administration"
      },
      "Invoice": {
        "id": 4351,
        "type": "",
        "invoiceNumber": 2340324,
        "date": "2023-07-31T00:00:00.000Z",
        "dueDate": "2023-08-14T00:00:00.000Z",
        "amount": 748.3
      },
      "Relation": {
        "nr": 102158,
        "name": "Jane Doe"
      },
      "TermsOfPayment": {
        "id": 11,
        "description": "Prepayment"
      }
    },
  ]
}

对于列出值,我使用:

$parsed = json_decode($received, true);

if ($parsed['AppName'] == true) {

    $obj = json_decode($received);

    $invoices = $obj->Invoices;

    foreach ($invoices as $key => $value) {
        $invoice_number = $value->Invoice->invoiceNumber;
        $invoice_amount = $value->Invoice->amount;

        print "<br>".$invoice_number;
        print "<br>".$invoice_amount."<br>";
    }

现在下一步是搜索特定发票号码的金额。所以搜索2340324需要返回748.3

我尝试使用 array_search 和 array_column 搜索/匹配键,但我得到的只是空值。我一定做错了什么,我猜这与“数组深度”有关?

任何有关如何处理此问题的指示将不胜感激。

php arrays search
© www.soinside.com 2019 - 2024. All rights reserved.