Lassie Posted November 14, 2013 Share Posted November 14, 2013 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'] ], ); } ?> Quote Link to comment Share on other sites More sharing options...
Ch0cu3r Posted November 14, 2013 Share Posted November 14, 2013 The foreach loop is creating a multidimensional $values array containing your form fields. It is using the form fields id as the key in $values. What is the issue? Quote Link to comment Share on other sites More sharing options...
Lassie Posted November 14, 2013 Author Share Posted November 14, 2013 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)) Quote Link to comment Share on other sites More sharing options...
Ch0cu3r Posted November 14, 2013 Share Posted November 14, 2013 (edited) 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 Edited November 14, 2013 by Ch0cu3r Quote Link to comment Share on other sites More sharing options...
Lassie Posted November 14, 2013 Author Share Posted November 14, 2013 Thanks. I will pursue that line Quote Link to comment Share on other sites More sharing options...
AbraCadaver Posted November 14, 2013 Share Posted November 14, 2013 If you don't need the id I would just do: foreach($form['fields'] as $field) { $result[$field['label']] = $field['value']; } print_r($result); Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.