Jump to content

array parsing to variable from echo/print response


Go to solution Solved by denno020,

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:booples@127.0.0.1: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 ) ) 
  • Solution

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'];
Edited by denno020
This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.