简体   繁体   中英

PHP: Get array values based on conditions

I've been puzzling a lot with getting the correct values based on some conditions. First, let's look at what PHP's function var_dump() returned. This is just the important part in adjustment to the data I look for. Check the whole json object here.

array (size=3)
  0 => 
    array (size=12)
      'inputs' => 
        array (size=2)
          0 => 
            array (size=3)
              'prev_out' => 
                array (size=7)
                  'addr' => string '1AvY95SQqtWy6MKWZbE3Xnqant9F3whfPH' (length=34)
                  'value' => int 1583375
          1 => 
            array (size=3)
              'prev_out' => 
                array (size=7)
                  'addr' => string '1KEUVc2TxDh1VAcvrS5p3PtJHcRwzrvin1' (length=34)
                  'value' => int 150000
      'out' => 
        array (size=2)
          0 => 
            array (size=7)
              'addr' => string '1Djp9h8mR3qqRz6v54nx265b8jZuqYNQ8j' (length=34)
              'value' => int 1046166
          1 => 
            array (size=7)
              'addr' => string '13W7N4NFwpTkYnHuVABcQfdrvDyszDqwkr' (length=34)
              'value' => int 677209

What you just saw was the output of $data['txs']. The outer array is a list of transactions. Within each, the details are specified. The length of the "inputs" and "out" can change! I want to get the value of each transaction ($data['txs'][index of transaction]['out']['value']) that meets the following criteria:

  1. The value of the "out" transaction should be bigger than zero.
  2. The address of the "out" transaction should be equal to $address.
  3. $address is not in the "inputs" transaction. Eg: $address can not be in the array of all ['inputs'][count(inputs)]['inputs']['prev_out']['addr'].

Here is my last attempt at solving this. The problem is that $result stays empty, even though the address 13W7N4NFwpTkYnHuVABcQfdrvDyszDqwkr is there and the result should be 677209.

$i = 0;
$txresult = array();    
foreach($data['txs'] as $ct => $cv) {

    $index = count($data['txs'][$i]['inputs']) - 1;
    $addressindex = array();
    for($testindex = 0; $testindex < $index; $testindex++) {
        $addressindex[] = $data['txs'][$i]['inputs'][$index]['prev_out']['addr'];
    }

    $counter = count($data['txs'][$i]['out']) - 1;      
    for($count = 0; $count < $counter; $count++) {
        if ($data['txs'][$i]['out'][$count]['value'] > 0 && $data['txs'][$i]['out'][$count]['addr'] == $address && !in_array($address, $addressindex)) {
            $result = $data['txs'][$i]['out'][$count]['value'];
        }
    }
    $i++;
}

Remember I cut the scope of the code, so it's possible some suggestions that require too much change may not work. I will do my best to respond to your questions asap.

Figured it out on my own terms. @Wrikken was instrumental to come to a conclusion.

The solution was pretty easy. Only two things:

  • $i++ shouldn't be in any of the for-loops, so I put it at the end of the foreach loop.
  • I had to unset $addressindex, $counter and $index.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM