0

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)  

1 Answer 1

3

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
)
Sign up to request clarification or add additional context in comments.

1 Comment

Note the fetch mode: fetch_assoc, best use $newArray[$rows['phone']] = $rows['id']

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.