如何从此数组中选择数据集[重复]
Posted
技术标签:
【中文标题】如何从此数组中选择数据集[重复]【英文标题】:how to select a data set from this array [duplicate] 【发布时间】:2018-06-03 20:51:56 【问题描述】:
"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 中做到这一点提前谢谢。
【问题讨论】:
因为这个问题现在离题了,你的代码在哪里? 你有什么尝试吗?请与我们分享,以便我们为您提供帮助。 现在将添加它 第一步....使它成为一个数组而不是一个json字符串 $obj = json_decode($execResult, true); $returned_result = $obj['result']['TxId'];所以我使用 json_decode 并使用 $obj['result']['TxId'] 它最终返回了整个 json 【参考方案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】: <?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 无效。我修复了它以使其正常工作,您在 opened 的第二个数组中有错字,但我想这只是一个错字而已。因此,此代码将为您提供所需的结果。由于您寻求的结果是嵌套的,因此您需要遵循它们的“路径”才能到达它们,因此您必须首先访问父数组等
*更新
我修改了我的代码以搜索您在描述中询问的值,我错过了那部分。现在它可以按照您的意愿工作了:
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] =>
)
【讨论】:
【参考方案3】:在 json_decoded 数组中循环遍历结果数组,然后检查 TxId
$search = "3efd41b3a051433a888eed3ecc174c1d025a5e2b486eb418eaaec5efddda22de";
$obj = json_decode($execResult, true);
foreach ($obj['result'] as $result)
if ($result['TxId'] == $search)
// If statement for PaymentUuid here
【讨论】:
以上是关于如何从此数组中选择数据集[重复]的主要内容,如果未能解决你的问题,请参考以下文章