Jump to content

Help with array


Lassie

Recommended Posts

I am trying to retrieve data from a gravity form and wish to extract the data into variables that I can then use.

I have got the values in an array but can't figure out how to put them into variables.

 

This is my code

<?php 
    $lead_id = 3;
    $lead = RGFormsModel::get_lead( $lead_id ); 
    $form = GFFormsModel::get_form_meta( $lead['form_id'] ); 
 
    $values= array();
 
    foreach( $form['fields'] as $field ) {
         var_dump($fields);
        $values[$field['id']] = array(
            'id'    => $field['id'],
            'label' => $field['label'],
            'value' => $lead[ $field['id'] ],
            
        );
    }
    
?>
Link to comment
https://forums.phpfreaks.com/topic/283893-help-with-array/
Share on other sites

Hi,

I want to put the field values into variables e.g.$property_title=?

I used print r to view the values but I don't know how to get the value from the array.

The array is

Array
(
[1] => Array
(
[id] => 1
[label] => Property Title
[value] => A New Property 2
)

[2] => Array
(
[id] => 2
[label] => Estate Agent
[value] => Me
)

[3] => Array
(
[id] => 3
[label] => Property Reference
[value] => 1123
)

[4] => Array
(
[id] => 4
[label] => Brief Description
[value] => Brief description
)

[6] => Array
(
[id] => 6
[label] => Add Brochure
[value] => http://localhost/wordpress/site2/files/gravity_forms/2-bb2623c01495bb311c734b2c44390d23/2013/10/Drovers-Way-Bude-Cornwall.pdf
)

[7] => Array
(
[id] => 7
[label] => Post Code
[value] => EX23 9DZ
)

)

Link to comment
https://forums.phpfreaks.com/topic/283893-help-with-array/#findComment-1458243
Share on other sites

I wouldn't assign each field to a seperate variable. Instead I make the field label the key.

$fields = array();
 
foreach( $form['fields'] as $field ) {
    
    // make field label the key
    $key = str_replace(' ', '_', strtolower($field['label']));
    $fields[ $key ] = array(
        'id'    => $field['id'],
        'label' => $field['label'],
        'value' => $lead[ $field['id'] ],
    );
}

So to access the Estate Agent field value you'd can do $fields['estate_agent']['value'].

 

Note: I renamed the $values array to $fields

Link to comment
https://forums.phpfreaks.com/topic/283893-help-with-array/#findComment-1458244
Share on other sites

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.