PHP Array to variable

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
TravisT6983
Forum Newbie
Posts: 19
Joined: Wed Jul 02, 2008 2:15 pm

PHP Array to variable

Post by TravisT6983 »

OK i am very new to PHP so please be gentle LOL.

I am posting a form to Liveaddress API and then i get the response back in an array it looks like this

Code: Select all

Array
(
    [0] => Array
        (
            [input_index] => 0
            [candidate_index] => 0
            [delivery_line_1] => 13700 Oakland St
            [last_line] => Highland Park MI 48203-3173
            [delivery_point_barcode] => 482033173009
            [components] => Array
                (
                    [primary_number] => 13700
                    [street_name] => Oakland
                    [street_suffix] => St
                    [city_name] => Highland Park
                    [state_abbreviation] => MI
                    [zipcode] => 48203
                    [plus4_code] => 3173
                    [delivery_point] => 00
                    [delivery_point_check_digit] => 9
                )

            [metadata] => Array
                (
                    [record_type] => S
                    [zip_type] => Standard
                    [county_fips] => 26163
                    [county_name] => Wayne
                    [carrier_route] => C021
                    [congressional_district] => 14
                    [rdi] => Commercial
                    [elot_sequence] => 0024
                    [elot_sort] => A
                    [latitude] => 42.40858
                    [longitude] => -83.08783
                    [precision] => Zip9
                )

            [analysis] => Array
                (
                    [dpv_match_code] => Y
                    [dpv_footnotes] => AABB
                    [dpv_cmra] => N
                    [dpv_vacant] => N
                    [active] => Y
                    [footnotes] => L#
                )

        )

)
How would i take primary_number and turn that into a variable to then insert into a database?

Thanks,
T
User avatar
requinix
Spammer :|
Posts: 6617
Joined: Wed Oct 15, 2008 2:35 am
Location: WA, USA

Re: PHP Array to variable

Post by requinix »

Whatever you outputted there is an array. Either go directly to its [0] (if you know there's always just the one item in the array) or loop over it. The subvalues are all arrays; to get to the primary_number you'd use ...["components"]["primary_number"].

Post your code if you have problems or are still unsure about what to do.
TravisT6983
Forum Newbie
Posts: 19
Joined: Wed Jul 02, 2008 2:15 pm

Re: PHP Array to variable

Post by TravisT6983 »

Yeah i have tried that already here is what i got

Code: Select all


<?php
if($_POST['formSubmit'] == "Submit") 
    {
	// Customize this (get ID/token values in your SmartyStreets account)
	$authId = urlencode("...");
	$authToken = urlencode("...");
	
	// Address input
	$input1 = urlencode($_POST['formAddress']);
	$input2 = urlencode($_POST['formCity']);
	$input3 = urlencode($_POST['formState']);
	
	// Build the URL
	$req = "https://api.smartystreets.com/street-address/?street={$input1}&city={$input2}&state={$input3}&auth-id={$authId}&auth-token={$authToken}";
	
	// GET request and turn into associative array
	$result = json_decode(file_get_contents($req),true);
	$primary_number = $result["components"]["primary_number"];

	echo  $primary_number;
Last edited by requinix on Thu Dec 12, 2013 1:41 pm, edited 1 time in total.
Reason: removing credentials
User avatar
requinix
Spammer :|
Posts: 6617
Joined: Wed Oct 15, 2008 2:35 am
Location: WA, USA

Re: PHP Array to variable

Post by requinix »

Please don't post credentials in a public place. For your own good. I'll remove them for you this time :)

You missed the first step of
requinix wrote:Whatever you outputted there is an array. Either go directly to its [0] (if you know there's always just the one item in the array) or loop over it.

Code: Select all

$results = json_decode(file_get_contents($req),true);
foreach($results as $i => $result) {
    echo "Result {$i}: ", $result["components"]["primary_number"];
}
Post Reply