从MYSQL过滤器创建动态多维数组

时间:2011-06-13 13:23:36

标签: php mysql multidimensional-array

我一直在搜索从数据库过滤器创建多维数组的正确格式。我正在为一个项目使用一个常量联系人php包装器,它需要一个确切的格式来加载一组电子邮件地址,名字和姓氏。请参阅下面,了解该类将接受的数组的正确格式:

<php?
$contacts = array(
    array(
        'Email Address' => 'test@test.com',
        'First Name' => 'Mike - TEST API',
        'Last Name' => 'NULL',
    )

);

我现在拥有的代码如下:

<form action="<?php echo $PHP_SELF;?>" method="get" name="form" target="_self" id="form">
<p>
<select name="networks[]" size="50" multiple>
<option value="West Bronx">West Bronx</option>
<option value="Williamsburg">Williamsburg</option>
<option value="Turtle Bay">Turtle Bay</option>
<option value="Times Square">Times Square</option>
<option value="Southeast Bronx">Southeast Bronx</option>
<option value="Sutton">Sutton</option>
<option value="Sunnyside">Sunnyside</option>
<option value="Sheridan Square">Sheridan Square</option>
<option value="Sheepshead">Sheepshead</option>
<option value="Roosevelt">Roosevelt</option>
<option value="Riverdale">Riverdale</option>
<option value="Ridgewood">Ridgewood</option>
<option value="Richmond Hill">Richmond Hill</option>
<option value="Battery Park City Network">Battery Park City Network</option>
<option value="Yorkville">Yorkville</option>
</select>
<br>
<input type="submit" name="Submit" value="Submit">
</form> 

<?



// Get the value of the filters and see if there are more then one selection
$foo = $_GET['networks'];
$sQuery = "";
if (sizeof($foo) > 0) {
  for ($i=0;$i<sizeof($foo);$i++) {
    $sQuery .= "Network LIKE '%$foo[$i]%' OR ";
  }
}
else{
  $sQuery = "Network LIKE '%$foo[0]%";
}
mysql_select_db($database_conn, $conn); 


$query_Recordset0 = "SELECT * FROM contacts WHERE $sQuery "; 
// Knock the last "OR" off the Query
$query_Recordset1 = substr($query_Recordset0, 0, -4); 


echo $query_Recordset1;
$Recordset1 = mysql_query($query_Recordset1, $conn) or die(mysql_error());







?>



<table width="200" border="0" cellpadding="0" cellspacing="0">
<tr>
<td>Network</td>
<td>Email</td>
</tr>
<?php do { ?>
<tr>
<td><?php echo $row_Recordset1['Network']; ?></td>
<td><?php echo $row_Recordset1['Email']; ?></td>

<td><?php echo $row_Recordset1['First Name']; ?></td>
<td><?php echo $row_Recordset1['Last Name']; ?></td>
</tr>
<?php

//BUILD The Multidimensional Array from the database Query and store it appropriately

$newarray = array();
$newarray[]['Email Address']=$row_Recordset1['Email'];
$newarray[]['First Name']='NULL';
$newarray[]['Last Name']='Last Name';
print_r($newarray);

这会生成如下数组:

Array (
[0] => Array ( [Email Address] => )
[1] => Array ( [First Name] => NULL )
[2] => Array ( [Last Name] => Last Name ) )

Array (
[0] => Array ( [Email Address] => silverslammer@aol.com )
[1] => Array ( [First Name] => NULL )
[2] => Array ( [Last Name] => Last Name ) )

Array ( 
[0] => Array ( [Email Address] => shlomi@lmopticaldisc.com )
[1] => Array ( [First Name] => NULL )
[2] => Array

非常感谢任何帮助。

以下是我需要它的样子:

$contacts = array(
    array(
        'Email Address' => 'test@test.com',
        'First Name' => 'Mike - TEST API',
        'Last Name' => 'NULL',
    )

);

1 个答案:

答案 0 :(得分:1)

$newarray[] = array(
  'Email Address'=>$row_Recordset1['Email'];
  'First Name'=>'NULL';
  'Last Name'=>'Last Name';
);

print_r($newarray);