2
{
"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
    }
]

}

How do I select the data set that contains "TxId":"3efd41b3a051433a888eed3ecc174c1d025a5e2b486eb418eaaec5efddda22de"

So that I can write an if statement using its payment did. Also, these data sets are going to be in different order every time and with more and more dataset added so cant use number indexing.

How could I do this in PHP thanks in advance.

rimonmostafiz
  • 1,341
  • 1
  • 15
  • 33
johnflex
  • 21
  • 1

3 Answers3

2

Loop thru result array inside your json_decoded array, then check for TxId

$search = "3efd41b3a051433a888eed3ecc174c1d025a5e2b486eb418eaaec5efddda22de";

$obj = json_decode($execResult, true);

foreach ($obj['result'] as $result) {
    if ($result['TxId'] == $search) {
        // If statement for PaymentUuid here
    }
}
Goma
  • 2,018
  • 1
  • 10
  • 19
1

Use json_decode to get data and use foreach loop.

<?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
      }


    }

 ?>
Moby M
  • 910
  • 2
  • 7
  • 26
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>';
    }

}

First of all your json was not valid. I fixed it in order to make it work, you had typo in the second array in the opened but i guess it's just a typo nothing more. So this code will get you the results you need. As the results you seek are nested you need to follow their "path" in order to reach them so you have to access first the parent array etc etc

*UPDATE

I modified my code to search for the value you asked in the description, i missed that part. Now it's working as you wish the output is:

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] => 
)
pr1nc3
  • 8,108
  • 3
  • 23
  • 36