Jump to content

array parsing to variable from echo/print response


airborne305

Recommended Posts

Sorry if my wording/terminalogy on the discription is wrong. I'm new to PHP, so i'll do my best to detail what im trying to do.

 

The PHP is doing what i need it to do. But i do not need to print the whole response. I only need the [address] and [pubkey] values as variables. I have read a few tutorials, but they assume i already know the strings to be converted to variables.

 

Please help :)

 

Code...

<?php

require_once 'jsonClient.php';
 
$service = new jsonClient('http://ooples:[email protected]:18332/');
 
$apple =  array ($service->validateaddress("1234567890"));
 
print_r ($apple);

?>

Response...

Array ( [0] => Array ( [isvalid] => 1 [address] => 1234567890 [ismine] => 1 [isscript] => [pubkey] => hqwiuehf9 [iscompressed] => 1 [account] => foo ) ) 

Tp print 'address' and 'pubkey', you will access it like this:

$apple[0]['address']
$apple[0]['pubkey']

Your variable $apple is an array, with 1 element, which is another array. That array then has all of your values, so that's why you need the '[0]' before the index of the value you want (as array's are 0-indexed, meaning the first value is always at position 0, second value at position 1, and so on).

 

Hopefully that helps you out.

Denno

 

Edit:

I just read through all of your code (instead of just looking at the print_r result), to avoid the need of the [0], then change:

 

$apple = array ($service->validateaddress("1234567890"));
//to
$apple = $service->validateaddress("1234567890");

Then you access the values like this:

 

echo $apple['address'];
echo $apple['pubkey'];

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.