简体   繁体   中英

PHP Arrays: How to get this hexadecimal value from array

I have retrieved an array like this:

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

And the result is:

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"
    }
  }
}

Now I need to get the hexadecimal values (example: 0x02 , 0x03 , etc).

I have already tried var_dump($trigger[1]->value); and I got this:

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"
}

But it does not seem to be the hexadecimal value !

So how can I properly get the hexadecimal value from this $trigger array?

I would really appreciate any idea or suggestion from you guys...

Thanks in advance.

UPDATE #1:

If I say var_dump($trigger[0][1]->value) , I get this error:

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

UPDATE #2:

I'm using Tron API to get access to all the contract information. I can properly get all the tickets information and by using a for loop , I can get this array as result of each ticket.

The code behind for doing that goes here:

         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());
        }

The BigInteger class has a toHex() method. So you can get the hex value like this:

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

You can iterate over the $trigger array like this:

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

First, I would suggest to your loop (ie for, foreach) to iterate through the array, instead of accessing item using the numeric index like:

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

Now Look at the class property you are trying to access: https://github.com/phpseclib/phpseclib/blob/master/phpseclib/Math/BigInteger.php Here Value property is private. look line: 70 Now try to access value through available public method returning value property.

foreach ($trigger as $item) {
  $item->toString(); 
 //OR
  $item->toHex();
}

If you want fix no. of iteration then you can for loop like:

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

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