PHP 数组:如何从数组中获取此十六进制值

PHP Arrays: How to get this hexadecimal value from array

我已经检索到这样的数组:

echo "<pre>"; var_dump($trigger);

结果是:

array(5) {
  [0]=>
  object(phpseclib\Math\BigInteger)#372 (2) {
    ["value"]=>
    string(4) "0x02"
    ["engine"]=>
    string(3) "gmp"
  }
  [1]=>
  object(phpseclib\Math\BigInteger)#376 (2) {
    ["value"]=>
    string(4) "0x03"
    ["engine"]=>
    string(3) "gmp"
  }
  [2]=>
  object(phpseclib\Math\BigInteger)#371 (2) {
    ["value"]=>
    string(4) "0x01"
    ["engine"]=>
    string(3) "gmp"
  }
  [3]=>
  object(phpseclib\Math\BigInteger)#373 (2) {
    ["value"]=>
    string(6) "0x00a8"
    ["engine"]=>
    string(3) "gmp"
  }
  [4]=>
  array(6) {
    [0]=>
    object(phpseclib\Math\BigInteger)#383 (2) {
      ["value"]=>
      string(4) "0x0f"
      ["engine"]=>
      string(3) "gmp"
    }
    [1]=>
    object(phpseclib\Math\BigInteger)#377 (2) {
      ["value"]=>
      string(4) "0x0c"
      ["engine"]=>
      string(3) "gmp"
    }
    [2]=>
    object(phpseclib\Math\BigInteger)#381 (2) {
      ["value"]=>
      string(4) "0x1b"
      ["engine"]=>
      string(3) "gmp"
    }
    [3]=>
    object(phpseclib\Math\BigInteger)#380 (2) {
      ["value"]=>
      string(4) "0x05"
      ["engine"]=>
      string(3) "gmp"
    }
    [4]=>
    object(phpseclib\Math\BigInteger)#367 (2) {
      ["value"]=>
      string(4) "0x16"
      ["engine"]=>
      string(3) "gmp"
    }
    [5]=>
    object(phpseclib\Math\BigInteger)#418 (2) {
      ["value"]=>
      string(4) "0x17"
      ["engine"]=>
      string(3) "gmp"
    }
  }
}
array(5) {
  [0]=>
  object(phpseclib\Math\BigInteger)#407 (2) {
    ["value"]=>
    string(4) "0x03"
    ["engine"]=>
    string(3) "gmp"
  }
  [1]=>
  object(phpseclib\Math\BigInteger)#384 (2) {
    ["value"]=>
    string(4) "0x01"
    ["engine"]=>
    string(3) "gmp"
  }
  [2]=>
  object(phpseclib\Math\BigInteger)#394 (2) {
    ["value"]=>
    string(4) "0x01"
    ["engine"]=>
    string(3) "gmp"
  }
  [3]=>
  object(phpseclib\Math\BigInteger)#300 (2) {
    ["value"]=>
    string(4) "0x06"
    ["engine"]=>
    string(3) "gmp"
  }
  [4]=>
  array(6) {
    [0]=>
    object(phpseclib\Math\BigInteger)#401 (2) {
      ["value"]=>
      string(4) "0x14"
      ["engine"]=>
      string(3) "gmp"
    }
    [1]=>
    object(phpseclib\Math\BigInteger)#392 (2) {
      ["value"]=>
      string(4) "0x19"
      ["engine"]=>
      string(3) "gmp"
    }
    [2]=>
    object(phpseclib\Math\BigInteger)#404 (2) {
      ["value"]=>
      string(4) "0x02"
      ["engine"]=>
      string(3) "gmp"
    }
    [3]=>
    object(phpseclib\Math\BigInteger)#385 (2) {
      ["value"]=>
      string(4) "0x08"
      ["engine"]=>
      string(3) "gmp"
    }
    [4]=>
    object(phpseclib\Math\BigInteger)#399 (2) {
      ["value"]=>
      string(4) "0x0f"
      ["engine"]=>
      string(3) "gmp"
    }
    [5]=>
    object(phpseclib\Math\BigInteger)#299 (2) {
      ["value"]=>
      string(4) "0x0a"
      ["engine"]=>
      string(3) "gmp"
    }
  }
}
array(5) {
  [0]=>
  object(phpseclib\Math\BigInteger)#370 (2) {
    ["value"]=>
    string(4) "0x04"
    ["engine"]=>
    string(3) "gmp"
  }
  [1]=>
  object(phpseclib\Math\BigInteger)#376 (2) {
    ["value"]=>
    string(4) "0x03"
    ["engine"]=>
    string(3) "gmp"
  }
  [2]=>
  object(phpseclib\Math\BigInteger)#375 (2) {
    ["value"]=>
    string(4) "0x01"
    ["engine"]=>
    string(3) "gmp"
  }
  [3]=>
  object(phpseclib\Math\BigInteger)#398 (2) {
    ["value"]=>
    string(6) "0x00a8"
    ["engine"]=>
    string(3) "gmp"
  }
  [4]=>
  array(6) {
    [0]=>
    object(phpseclib\Math\BigInteger)#396 (2) {
      ["value"]=>
      string(4) "0x05"
      ["engine"]=>
      string(3) "gmp"
    }
    [1]=>
    object(phpseclib\Math\BigInteger)#378 (2) {
      ["value"]=>
      string(4) "0x0a"
      ["engine"]=>
      string(3) "gmp"
    }
    [2]=>
    object(phpseclib\Math\BigInteger)#379 (2) {
      ["value"]=>
      string(4) "0x0c"
      ["engine"]=>
      string(3) "gmp"
    }
    [3]=>
    object(phpseclib\Math\BigInteger)#373 (2) {
      ["value"]=>
      string(4) "0x13"
      ["engine"]=>
      string(3) "gmp"
    }
    [4]=>
    object(phpseclib\Math\BigInteger)#390 (2) {
      ["value"]=>
      string(4) "0x19"
      ["engine"]=>
      string(3) "gmp"
    }
    [5]=>
    object(phpseclib\Math\BigInteger)#388 (2) {
      ["value"]=>
      string(4) "0x1c"
      ["engine"]=>
      string(3) "gmp"
    }
  }
}

现在我需要获取十六进制值(例如:0x020x03 等)。

我已经尝试过 var_dump($trigger[1]->value); 我得到了这个:

object(GMP)#411 (1) {
  ["num"]=>
  string(1) "0"
}
object(GMP)#382 (1) {
  ["num"]=>
  string(1) "3"
}
object(GMP)#409 (1) {
  ["num"]=>
  string(1) "1"
}
object(GMP)#383 (1) {
  ["num"]=>
  string(1) "3"
}
object(GMP)#401 (1) {
  ["num"]=>
  string(1) "3"
}
object(GMP)#396 (1) {
  ["num"]=>
  string(1) "3"
}
object(GMP)#416 (1) {
  ["num"]=>
  string(1) "3"
}
object(GMP)#400 (1) {
  ["num"]=>
  string(1) "3"
}
object(GMP)#395 (1) {
  ["num"]=>
  string(1) "1"
}
object(GMP)#381 (1) {
  ["num"]=>
  string(1) "3"
}
object(GMP)#404 (1) {
  ["num"]=>
  string(1) "3"
}

不过好像不是十六进制的值!

那么我怎样才能正确地从这个 $trigger 数组中获取十六进制值呢?

非常感谢你们的任何想法或建议...

提前致谢。

更新#1:

如果我说 var_dump($trigger[0][1]->value),我得到这个错误:

Error Cannot use object of type phpseclib\Math\BigInteger as array

更新#2:

我正在使用 Tron API to get access to all the contract 信息。我可以正确获取所有门票信息,并且通过使用 for loop,我可以获得每张门票的结果数组。

执行此操作的代码在这里:

         try {
            $totalTck = $TransactionBuilder->triggerSmartContract(
                (array)$abi,
                $contractH,
                $function,
                $params,
                $feeLimit,
                $addressH,
                $callValue = 0,
                $bandwidthLimit = 0);

            // $totalMinus = ($totalTck[0]->value) - 1; 
            $totalMinus = 10;
            for($i=0;$i<=$totalMinus;$i++){
                $address = ADDRESS;
                $addressH = $tron->toHex($address);
                $contract = CONTRACT;
                $contractH = $tron->toHex($contract);
                $function="getTicketInfo";
                $params=array("0"=>$i);
                $feeLimit=30000000;
                $callValue = 0;
                $bandwidthLimit = 0;
                try {
                    $trigger = $TransactionBuilder->triggerSmartContract(
                        (array)$abi,
                        $contractH,
                        $function,
                        $params,
                        $feeLimit,
                        $addressH,
                        $callValue = 0,
                        $bandwidthLimit = 0);

                    var_dump($trigger[0][1]->value);
                        //$query = DB::table('tickets')->insert([
                        //    'userId' => '',
                        //    'types' => '',
                        //    'startRound' => '',
                        //    'endRound' => '',
                        //   'numbers' => $trigger[0]->value
                        //]);
                } catch (\IEXBase\TronAPI\Exception\TronException $e) {
                    die($e->getMessage());
                }
            }
        } catch (\IEXBase\TronAPI\Exception\TronException $e) {
            die($e->getMessage());
        }

BigInteger class 有一个 toHex() 方法。所以你可以得到这样的十六进制值:

echo '0x'.$trigger[0]->toHex();

您可以像这样遍历 $trigger 数组:

foreach($trigger as $e)
{
    if(is_array($e))
    {
        foreach($e as $t)
            echo '0x'.$t->toHex().'<br>';
    }
    else
        echo '0x'.$e->toHex().'<br>';
}

首先,我建议您的循环(即 for、foreach)遍历数组,而不是使用数字索引访问项目,例如:

foreach ($trigger as $item) {
  $item->value;
} 

现在查看您正在尝试访问的 class 属性:https://github.com/phpseclib/phpseclib/blob/master/phpseclib/Math/BigInteger.php 这里的值 属性 是私有的。看线:70 现在尝试通过返回 value 属性.

的可用 public 方法访问 value
foreach ($trigger as $item) {
  $item->toString(); 
 //OR
  $item->toHex();
}

如果你想修复没有。迭代然后你可以for循环像:

$totalMinus = 10;
for($i=0;$i<=$totalMinus;$i++){
    $trigger[$i]->toHex();
}