helloWORLD
helloWORLD

Reputation: 21

PHP While Loop Question

How can I turn the code below into a single array when its in the while loop? An example would help.

Here is my PHP code.

while($row = mysqli_fetch_array($dbc)){ 
    $category = $row['category'];
    $url = $row['url'];
}

Upvotes: 1

Views: 208

Answers (3)

user113292
user113292

Reputation:

Building off mawg's solution and your new requirement:

$data = array();

while ($row = mysqli_fetch_array($dbc)) {
  $data[$row['category']] = $row['url'];
}

This would create an associative array with the category name as the key.

Or you could do:

$data = array();

while ($row = mysqli_fetch_array($dbc)) {
  $data[] = array(
    'row' => $row['url'],
    'category' => $row['category'],
  );
}

Which would create an array of associative arrays that would contain the URL and category for each row.

Upvotes: 4

Zsolti
Zsolti

Reputation: 1607

Try this:

$allRows = array(); 
while($row = mysqli_fetch_array($dbc))
{  
    $allRows[] = $row;    
} 

Upvotes: 1

Mawg
Mawg

Reputation: 40140

Do you mean something like

$urls = array();
$Categories = Array();
while($row = mysqli_fetch_array($dbc)){ 
    $Categories[] = $row['category'];
    $urls[] = $row['url'];
}

Upvotes: 1

Related Questions