tags:

views:

298

answers:

3

I am looking for an efficient way to pull the data I want out of an array called $submission_info so I can easily auto-fill my form fields. The array size is about 120.

I want to find the field name and extract the content. In this case, the field name is loanOfficer and the content is John Doe.

Output of Print_r($submission_info[1]):

Array ( [field_id] => 2399 [form_id] => 4 [field_name] => loanOfficer [field_test_value] => ABCDEFGHIJKLMNOPQRSTUVWXYZ [field_size] => medium [field_type] => other [data_type] => string [field_title] => LoanOfficer [col_name] => loanOfficer [list_order] => 2 [admin_display] => yes [is_sortable] => yes [include_on_redirect] => yes [option_orientation] => vertical [file_upload_dir] => [file_upload_url] => [file_upload_max_size] => 1000000 [file_upload_types] => [content] => John Doe )

I want to find the field name and extract the content. In this case, the field name is loanOfficer and the content is John Doe.

A: 

I'm assuming that php has an associative array (commonly called dictionary or hashtable). The most efficient routine would be to run over the array once and put the fields into a dictionary keyed on the field name.

Then instead of having to search through the original array when you want to find a specific field (an O(n)) operation. You just used the dictionary to retrieve it by the name of the field in an O(1) (or constant) operation. Of course the first pass over the array to populate the dictionary would be O(n) but that's a one time cost rather than paying that same penalty for every lookup.

Mike Brown
+2  A: 

You're probably best off going through each entry and creating a new associative array out of it.

foreach($submission_info as $elem) {
    $newarray[$elem["field_name"]] = $elem["content"];
}

Then you can just find the form fields by getting the value from $newarray[<field you're filling in>]. Otherwise, you're going to have to search $submission_info each time for the correct field.

Randy
Thank you rcar, I will try it right now and let you know how it turns out.
Haabda
That did it! Thank you very much for your help.
Haabda
+1  A: 

Not sure if this is the optimal solution:

foreach($submission_info as $info){
  if($info['field_name'] == 'loanOfficer'){ //check the field name
    $content = $info['content']; //store the desired value
    continue; //this will stop the loop after the desired item is found
  }
}

Next time: Questions are more helpful to you and others if you generalize them such that they cover some overarching topic that you and maybe others don't understand. Seems like you could use an array refresher course...

Matthew Encinas
Your answer is what I thought of at first. But, I didn't want to loop through the entire array for every single lookup. The associative array answer from rcar seems to me to be a more efficient solution.
Haabda