[英]PHP creating an array from another array?
Hi I was wondering how to create an array with key value pairs from my other array which is an array consisting of values read in from a DB table. 嗨,我想知道如何从另一个数组(包含从DB表读取的值组成的数组)中创建具有键值对的数组。
Heres the code: 这是代码:
$query1 = "SELECT phone, id FROM table1 GROUP BY id";
$result1 = $mysqli->query($query1);
while($rows = $result1->fetch_assoc()) {
}
In order to see the array I used fwrite and var_export 为了查看数组,我使用了fwrite和var_export
Heres the var_export($row,1): 这是var_export($ row,1):
array('phone' => 123, 'id' => 456)
array('phone' => 246, 'id' => 789)
What am looking for is to create another array using those values to look like this: 寻找的是使用这些值创建另一个数组,如下所示:
array(
123 => 456
246 => 789)
Use this: 用这个:
$newArray = array();
while($rows = $result1->fetch_assoc()) {
$newArray[$rows['phone']] = $rows['id'];
}
The new array will then look like this: 新数组将如下所示:
array(
123 => 456
246 => 789
)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.