-1

I am trying to finish the last function of a script that sync data added in Facebook lead form to MySQL database. I am trying to map MySQL columns to Facebook lead form fields then when new entry added in Facebook lead each content to be added to MySQL column as rows depending on mapping chosen in script setting page.

I have now 2 arrays

1. The first one $mapfieldarray contain key ==> value where
- key is MySQL columns and
- value is the Facebook fields I map to the columns

  Array
         (
            [key] => phone_number_col
             [values] => phone_number

         )
     (
             [key] => full_name_col
             [values] => full_name


         )

2. and the second array is $leadField contain name ==> value - where name is Facebook field that has value entered and - value which return field entry value.

           Array

             ( [name] => phone_number 
             [values] => Array 
                ( 

                 [0] => <test lead: dummy data for phone_number>

             )

            ) (
            [name] => full_name
             [values] => Array
                 (
                     [0] => <test lead: dummy data for full_name>
                 )

         )

how to get this done ?

Vinoth Vino
  • 9,166
  • 3
  • 66
  • 70

1 Answers1

0

You need to use array_search()

E.g. something like this:

$mapfieldarray = [
  'mysql_col_1' => 'fb_field_1',
  'mysql_col_2' => 'fb_field_2',
  'mysql_col_3' => 'fb_field_3',
];

$leadField = [
  'fb_field_1' => 'value to save',
];

$query = 'INSERT INTO table SET ';

foreach($leadField as $fb_field => $value) {
  $mysql_field_name = array_search($fb_field, $mapfieldarray);

  if(!empty($mysql_field_name)) {
    // MAKE SURE TO ESCAPE YOUR VALUES!
    // This is just an example to show how to use array_search
    $query .= "INSERT INTO table SET $mysql_field_name = $value";
  }
}

What array search is doing is searching the values of the first array for a match and then returning the corresponding key, which in this case is your mysql field name.

Felix Eve
  • 3,811
  • 3
  • 40
  • 50