简体   繁体   English

PHP:根据条件获取数组值

[英]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. 首先,让我们看一下PHP返回的函数var_dump()。 This is just the important part in adjustment to the data I look for. 这只是调整我要查找的数据的重要部分。 Check the whole json object here. 这里检查整个json对象

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']. 您刚刚看到的是$ 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: 我想获取满足以下条件的每笔交易的价值($ data ['txs'] [交易索引] ['out'] ['value']):

  1. The value of the "out" transaction should be bigger than zero. “退出”事务的值应大于零。
  2. The address of the "out" transaction should be equal to $address. “退出”事务的地址应等于$ address。
  3. $address is not in the "inputs" transaction. $ address不在“输入”事务中。 Eg: $address can not be in the array of all ['inputs'][count(inputs)]['inputs']['prev_out']['addr']. 例如:$ address不能在所有['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. 问题是,即使地址13W7N4NFwpTkYnHuVABcQfdrvDyszDqwkr存在,$ result仍为空,结果应为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. @Wrikken有助于得出结论。

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 ++不应位于任何for循环中,因此我将其放在foreach循环的末尾。
  • I had to unset $addressindex, $counter and $index. 我必须取消设置$ addressindex,$ counter和$ index。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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