简体   繁体   中英

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.

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

Heres the 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
)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM