PHP 数组 : How to get this hexadecimal value from array

标签 php arrays laravel associative-array laravel-8

我已经检索了一个这样的数组: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 访问所有 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>';
}

关于PHP 数组 : How to get this hexadecimal value from array,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/67605446/

相关文章:

javascript - 使用一个键的值数组过滤多个 javascript 对象

php - 在 Laravel 中找到交点的最佳方法

javascript - 上传图片时的 formData 与 JSON(最佳实践)

php - 仅从连接表中获取指定列

Java访问数组等对象

php - 在jquery/javascript中创建数组的数组

php - 如何在Linux PHP服务器上获取网站的屏幕截图?

php - mysql,从多个表中选择数据,表中没有公共(public)字段

arrays - 在 Dymola 中编译大型数组

c++ - 如何在C++中将字符串对象数组的数据转换为int