Jump to content

How to access multiple json arrays with single variable?


Go to solution Solved by Jacques1,

Recommended Posts

I asked this question before and I did receive the answer I was looking for. But now I realize something. It only works for the first result.  Let me explain.

 

I am using this API. https://block.io/api/simple/php

$get_address            = $block_io->get_transactions(array('type' => 'received', 'addresses' => $my_wallet));
$api_amount             = $get_address->data->txs[0]->amounts_received[0]->amount;
$api_recp     		= $get_address->data->txs[0]->amounts_received[0]->recipient;
$api_trans_id 		= $get_address->data->txs[0]->txid;
$api_sender_id 		= $get_address->data->txs[0]->senders;

These are variables above i am using to check if something is true or not. For eg. The form below. I want to check if the $api_trans_id matches the the post id I submit below.

if($api_trans_id == $_POST['trans-id']) {
  echo 'Success';
} else {
  echo 'Fail.';
}
<form action="" method="post">					
   <input type="text" name="trans-id" value="" maxlength="1084" />
   <input type="submit" name="submit" value="Submit" />
</form>

The above works if the first result in the json array below has the same post id or else it fails.  I want to loop through ALL the json arrays and try to match the post id($_POST['trans-id']).

Below are the 3 array results to give you an example. The first txid is "41d9afcc73da63c45674088411460116cfefe324cc7a6f5fcb267d9f584adf73". If I submit this same id as the post id in the form, it'll be success full. But if I use the 2nd or 3rd txid below, it will fail.

 

So how can I change this "$api_trans_id = $get_address->data->txs[0]->txid;" so that it looks for all three txid instead of only the first one?

{
  "status" : "success",
  "data" : {
    "network" : "BTC",
    "txs" : [
      {
        "txid" : "41d9afcc73da63c45674088411460116cfefe324cc7a6f5fcb267d9f584adf73",
        "from_green_address" : false,
        "time" : 1496877627,
        "confirmations" : 3080,
        "amounts_received" : [
          {
            "recipient" : "1QFaAYB4r8sk7MG3beb7yY4R21t9JrnMwK",
            "amount" : "0.00200000"
          }
        ],
        "senders" : [
          "2EqvPUTwjUuon6c2a6YxBreJtBkkZD5fsp"
        ],
        "confidence" : 1.0,
        "propagated_by_nodes" : null
      },
      {
        "txid" : "2f72d4dd97234937ddf6e8174b17a9a421ba35bbfb6fa40cfeb6ac92db6ce032",
        "from_green_address" : false,
        "time" : 1491675979,
        "confirmations" : 12338,
        "amounts_received" : [
          {
            "recipient" : "1QFaAYB4r8sk7MG3beb7yY4R21t9JrnMwK",
            "amount" : "0.00010000"
          }
        ],
        "senders" : [
          "3L98MQZKvwNo21fFpY7RJ9HYfYdv62XpYz"
        ],
        "confidence" : 1.0,
        "propagated_by_nodes" : null
      },
      {
        "txid" : "fce58f0cb1d32c1ed7c27825c41753ce98323771bd931f0a0f99b6294d9ee642",
        "from_green_address" : false,
        "time" : 1491672301,
        "confirmations" : 12341,
        "amounts_received" : [
          {
            "recipient" : "1QFaAYB4r8sk7MG3beb7yY4R21t9JrnMwK",
            "amount" : "0.00010000"
          }
        ],
        "senders" : [
          "4L98MQZKvwNo21fFpY7RJ9HYfYdv62XpYz"
        ],
        "confidence" : 1.0,
        "propagated_by_nodes" : null
      },
    ]
  }
}
Edited by imgrooot

Just loop through the txs array and check each transaction.

 

foreach ($get_address->data->txs as $tx){
    $api_amount         = $tx->amounts_received[0]->amount;
    $api_recp           = $tx->amounts_received[0]->recipient;
    $api_trans_id       = $tx->txid;
    $api_sender_id      = $tx->senders;

    //...
}
  • Like 1

Just loop through the txs array and check each transaction.

 

foreach ($get_address->data->txs as $tx){
    $api_amount         = $tx->amounts_received[0]->amount;
    $api_recp           = $tx->amounts_received[0]->recipient;
    $api_trans_id       = $tx->txid;
    $api_sender_id      = $tx->senders;

    //...
}

 

I'm assuming you mean check if it's true or not inside the foreach loop like this?

foreach ($get_address->data->txs as $tx){
    $api_amount         = $tx->amounts_received[0]->amount;
    $api_recp           = $tx->amounts_received[0]->recipient;
    $api_trans_id       = $tx->txid;
    $api_sender_id      = $tx->senders;

    if($api_trans_id == $_POST['trans-id']) {
      echo 'Success';
    } else {
     echo 'Fail.';
    }
}

If so then I get the "Fail" error. It's shown repeated multiple times.

Edited by imgrooot

Alright so I think I figured it out. Using "kicken"'s foreach loop, I need to turn the loop variables into an array and access them outside the loop like this.


$api_amount = array();
$api_recp = array();
$api_trans_id = array();
$api_sender_id = array();
foreach($get_address->data->txs as $tx){
   $api_amount[]       = $tx->amounts_received[0]->amount;
   $api_recp[]         = $tx->amounts_received[0]->recipient;
   $api_trans_id[]     = $tx->txid;
   $api_sender_id[]    = $tx->senders;
}
 

$post_transid = $_POST['trans-id'];

if(in_array($post_transid, $api_trans_id)) { echo 'yes'; } else { echo 'no'; }

if(in_array('0.00400000', $api_amount)) {
echo 'yes';
} else {
echo 'no';
}

 

The above works.  

 

One thing that doesn't work is the "$api_sender_id". I realized why. This is how it looks in json. If you look closley you will notice the value of "senders" is directly after the bracket ([). The others don't have that. So how do I access this "senders" value? 


status" : "success",
  "data" : {
    "network" : "BTC",
    "txs" : [
      {
        "txid" : "71d9afcc73da63c45674088411460116cfefe324cc7a6f5fcb267d9f584adf73",
        "from_green_address" : false,
        "time" : 1496877627,
        "confirmations" : 3080,
        "amounts_received" : [
          {
            "recipient" : "4QFaAYB4r8sk7MG3beb7yY4R21t9JrnMwK",
            "amount" : "0.00200000"
          }
        ],
        "senders" : [
          "6EqvPUTwjUuon6c2a6YxBreJtBkkZD5fsp"
        ],
        "confidence" : 1.0,
        "propagated_by_nodes" : null
      }
    ]
   }
 
Edit: Sorry for the shit code paste. It shows in fine in the code editor but it for some reason it shows outside of it after I save it.
Edited by imgrooot

Well since no one else has anything to add.  Here's my full answer to the original problem.

$api_amount = array();
$api_recp = array();
$api_trans_id = array();
$api_sender_id = array();
foreach($get_address->data->txs as $tx){
   $api_amount[]       = $tx->amounts_received[0]->amount;
   $api_recp[]         = $tx->amounts_received[0]->recipient;
   $api_trans_id[]     = $tx->txid;
   $api_sender_id[]    = $tx->senders[0];
}
 
$post_transid = $_POST['trans-id'];

// example 1
if(in_array($post_transid, $api_trans_id)) { 
  echo 'yes'; 
} else { 
  echo 'no'; 
}

// example 2
if(in_array('0.00400000', $api_amount)) {
  echo 'yes';
} else {
  echo 'no';
}
  • Solution

How are we supposed to tell you what you want? There are multiple senders and received amounts, and you've picked the first one while ignoring the rest. This could be horribly wrong or exactly right, depending on what you actually want to do.

 

In any case, your code is rather weird. What's the point of iterating over the transactions, storing the various items in new arrays and then iterating over those arrays to finally do the checks? Why not just check the transactions directly? This is basic programming:

<?php

// let's use a more sensible name than $get_address to hold the transactions
$last_received_transactions = $block_io->get_transactions(['type' => 'received', 'addresses' => $my_wallet]);

$transaction_id_matches = false;
foreach ($last_received_transactions->data->txs as $transaction)
{
    if ($transaction->txid == $_POST['trans-id'])
    {
        $tx_id_matches = true;
    }

    // other checks go here
}

if ($transaction_id_matches)
{
    echo 'transaction ID found';
}
else
{
    echo 'transaction ID not found';
}
This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.