开发者

How to pull data from a MySQL column and put it into a single array

开发者 https://www.devze.com 2022-12-27 19:27 出处:网络
Basically, this is what I currently use in an included file: $sites[0][\'url\'] = \"http://example0.com\";

Basically, this is what I currently use in an 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'] =开发者_如何学C "http://example4.com";
    $sites[5]['url'] = "http://example5.com";

And so I output it like so:

foreach($sites as $s)

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


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

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号