1

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'];
}

3 Answers 3

4

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.

Sign up to request clarification or add additional context in comments.

Comments

1

Do you mean something like

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

1 Comment

I want to put them all together if possible array()
1

Try this:

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

Comments

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.