I am fairly new to PHP-MYsql and HTML programming. So Please bear with me,

I'm trying to create a function which will produce an array of 'Fields in a Table' and combine that array with an array of 'Column Headings' to produce an associative array like this;

$labels= array('IDno' => 'Id No', 
                  'VendorID' => 'Vendor Id No',
                    etc, 
                    ect.
             );

so $labels('VendorID') would show 'Vendor Id No', etc.
the code I will use to produce function getTableFields() is as follows;

 // ============ getTableFields =========== 
  $query= sprintf("SHOW COLUMNS FROM %s", $table); 
   $result = mysqli_query($cxn,$query)
            or die ("Couldn't execute query.");
  $row = mysqli_fetch_assoc($result);
            $fields[]=$row['Field'];         
  for($i=1; $i<mysqli_num_rows($result); $i++)
  { 
                  $row = mysqli_fetch_assoc($result);
                  $fields[]=$row['Field'];

  } 

This works and produces the array of fields.
Hand written array of column headings for above fields.

 $colhead = array(
        "Id No:",
        "Vendor Id No:",
        "Account No:",
        "Company:",
        "Category:",
        "Email:",
        "Phone:",
        "Internet Id:",
        "Password:", 
        "License:",
        "Email of Record:",
        "Notes:"        
   );

I have tried this and variations of this;

$labels=array('$fields[0]' =>'$colhead[0]',    
          '$fields[1]' => $colhead[1],
           etc,
            etc,
 ); 

which does not work.
I have tried all kinds of combinations of { } ( ) [ ] ' " \" to enclose the variables to make it work. I've tried echo, print, printf, sprintf etc.

Can you help?
It just seems that this should be trivial.

Thanks in advance.

Frustrated, Rick_P

Try looking at the method array_combine.

If you're sure that your two arrays, field names and column heading will always match up, then this function accepts two arrays. The first, as the keys and the second as the values.

Check out http://uk3.php.net/manual/en/function.array-combine.php for more info.

R.

Thanks robothy, I knew it was simple and!!! array_combine() works like a champ. I think this will help me solve some other similar problems. If not I'll post again.

Again a big THANKS!!!!

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.