How to extract data from MySQL column and put it in one array

Basically, this is what I am currently using in the included file:

    $sites[0]['url'] = "http://example0.com";
    $sites[1]['url'] = "http://example1.com";
    $sites[2]['url'] = "http://example2.com";
    $sites[3]['url'] = "http://example3.com";
    $sites[4]['url'] = "http://example4.com";
    $sites[5]['url'] = "http://example5.com";

      

And so I output it like this:

foreach($sites as $s)

      

But I want to simplify management through MySQL database. So my question is, how can I automatically add additional "$ sites [x] ['url'] =" http://examplex.com ";" and output it appropriately from my MySQL table?

+2


a source to share


1 answer


You can use array_map()

for this:

$newSites = array_map('pick_attribute', $sites, 'url');

function pick_attributes($val, $prop) {
  return $val[$prop];
}

      



or a simple loop:

$newSites = array();
foreach ($sites as $v) {
  $newSites[] = $v['url'];
}

      

+1


a source







All Articles