如何从这个数组中选择一个数据集

时间:2017-12-21 08:06:49

标签: php json api

{
"success" : true,
"message" : "",
"result" : [{
        "PaymentUuid" : "554ec664-8842-4fe9-b491-06225becbd59",
        "Currency" : "BTC",
        "Amount" : 0.00156121,
        "Address" : "1K37yQZaGrPKNTZ5KNP792xw8f7XbXxetE",
        "Opened" : "2014-07-11T03:41:25.323",
        "Authorized" : true,
        "PendingPayment" : false,
        "TxCost" : 0.00020000,
        "TxId" : "70cf6fdccb9bd38e1a930e13e4ae6299d678ed6902da710fa3cc8d164f9be126",
        "Canceled" : false,
        "InvalidAddress" : false
    }, {
        "PaymentUuid" : "d3fdf168-3d8e-40b6-8fe4-f46e2a7035ea",
        "Currency" : "BTC",
        "Amount" : 0.11800000,
        "Address" : "1Mrcar6715hjds34pdXuLqXcju6QgwHA31",
        "O
        pened" : "2014-07-03T20:27:07.163",
        "Authorized" : true,
        "PendingPayment" : false,
        "TxCost" : 0.00020000,
        "TxId" : "3efd41b3a051433a888eed3ecc174c1d025a5e2b486eb418eaaec5efddda22de",
        "Canceled" : false,
        "InvalidAddress" : false
    }
]

}

如何选择包含的数据集 "TxId":"3efd41b3a051433a888eed3ecc174c1d025a5e2b486eb418eaaec5efddda22de"

这样我就可以用付款方式写一个if语句了。此外,这些数据集每次都会以不同的顺序排列,并且添加的数据集越来越多,因此无法使用数字索引。

我怎么能在PHP中提前做到这一点。

3 个答案:

答案 0 :(得分:2)

在json_decoded数组中循环结果数组,然后检查TxId

$search = "3efd41b3a051433a888eed3ecc174c1d025a5e2b486eb418eaaec5efddda22de";

$obj = json_decode($execResult, true);

foreach ($obj['result'] as $result) {
    if ($result['TxId'] == $search) {
        // If statement for PaymentUuid here
    }
}

答案 1 :(得分:1)

使用json_decode获取数据并使用foreach循环。

<?php 
  $string='{
"success" : true,
"message" : "",
"result" : [{
        "PaymentUuid" : "554ec664-8842-4fe9-b491-06225becbd59",
        "Currency" : "BTC",
        "Amount" : 0.00156121,
        "Address" : "1K37yQZaGrPKNTZ5KNP792xw8f7XbXxetE",
        "Opened" : "2014-07-11T03:41:25.323",
        "Authorized" : true,
        "PendingPayment" : false,
        "TxCost" : 0.00020000,
        "TxId" : "70cf6fdccb9bd38e1a930e13e4ae6299d678ed6902da710fa3cc8d164f9be126",
        "Canceled" : false,
        "InvalidAddress" : false
    }, {
        "PaymentUuid" : "d3fdf168-3d8e-40b6-8fe4-f46e2a7035ea",
        "Currency" : "BTC",
        "Amount" : 0.11800000,
        "Address" : "1Mrcar6715hjds34pdXuLqXcju6QgwHA31",
        "O
        pened" : "2014-07-03T20:27:07.163",
        "Authorized" : true,
        "PendingPayment" : false,
        "TxCost" : 0.00020000,
        "TxId" : "3efd41b3a051433a888eed3ecc174c1d025a5e2b486eb418eaaec5efddda22de",
        "Canceled" : false,
        "InvalidAddress" : false
    }
]
}';
$json = json_decode($string, true);
$TxId = "3efd41b3a051433a888eed3ecc174c1d025a5e2b486eb418eaaec5efddda22de";
foreach ($json as $key => $value){
      $txid=$value[1]['TxId'];
      if ($txid == $TxId) {
        // code
      }


    }

 ?>

答案 2 :(得分:0)

   <?php

$data='{
"success" : true,
"message" : "",
"result" : [{
        "PaymentUuid" : "554ec664-8842-4fe9-b491-06225becbd59",
        "Currency" : "BTC",
        "Amount" : 0.00156121,
        "Address" : "1K37yQZaGrPKNTZ5KNP792xw8f7XbXxetE",
        "Opened" : "2014-07-11T03:41:25.323",
        "Authorized" : true,
        "PendingPayment" : false,
        "TxCost" : 0.00020000,
        "TxId" : "70cf6fdccb9bd38e1a930e13e4ae6299d678ed6902da710fa3cc8d164f9be126",
        "Canceled" : false,
        "InvalidAddress" : false
    }, {
        "PaymentUuid" : "d3fdf168-3d8e-40b6-8fe4-f46e2a7035ea",
        "Currency" : "BTC",
        "Amount" : 0.11800000,
        "Address" : "1Mrcar6715hjds34pdXuLqXcju6QgwHA31",
        "opened" : "2014-07-03T20:27:07.163",
        "Authorized" : true,
        "PendingPayment" : false,
        "TxCost" : 0.00020000,
        "TxId" : "3efd41b3a051433a888eed3ecc174c1d025a5e2b486eb418eaaec5efddda22de",
        "Canceled" : false,
        "InvalidAddress" : false
    }
]
}';


$data=json_decode($data,true);

foreach($data['result'] as $row){

    if(array_search("3efd41b3a051433a888eed3ecc174c1d025a5e2b486eb418eaaec5efddda22de",$row,true)){
        echo '<pre>';
         print_r($row);
         echo '</br>';
    }

}

首先你的json无效。我修复它以使其工作,你在打开的第二个数组中输入错误但我猜它只是一个错字而已。因此,此代码将为您提供所需的结果。由于你寻找的结果是嵌套的,你需要按照他们的“路径”来达到它们,所以你必须首先访问父数组等等。

<强> * UPDATE

我修改了我的代码以搜索您在说明中提到的值,我错过了那部分。现在它的工作正如您希望的输出:

Array
(
    [PaymentUuid] => d3fdf168-3d8e-40b6-8fe4-f46e2a7035ea
    [Currency] => BTC
    [Amount] => 0.118
    [Address] => 1Mrcar6715hjds34pdXuLqXcju6QgwHA31
    [opened] => 2014-07-03T20:27:07.163
    [Authorized] => 1
    [PendingPayment] => 
    [TxCost] => 0.0002
    [TxId] => 3efd41b3a051433a888eed3ecc174c1d025a5e2b486eb418eaaec5efddda22de
    [Canceled] => 
    [InvalidAddress] => 
)