我有一个名为“servers”的数据库字段 此字段在每行中都有一个链接,此字段内容为:
> http://www.rapidshare.com/download1 > http://www.rapidshare.com/download2 > http://www.rapidshare.com/download3 > http://www.megaupload.com/download1 > http://www.megaupload.com/download2 > http://www.megaupload.com/download3 > http://www.fileserve.com/download1 > http://www.fileserve.com/download2 > http://www.fileserve.com/download3
我想创建一个包含所有服务器名称的数组,并创建更多包含内部链接的数组。 应该是这样的:
$servers = array( 'rapidshare' => array( 'link1' => 'http://www.rapidshare.com/download1', 'link2' => 'http://www.rapidshare.com/download2', 'link3' => 'http://www.rapidshare.com/download3'), 'megaupload' => array( 'link1' => 'http://www.megaupload.com/download1', 'link2' => 'http://www.megaupload.com/download2', 'link3' => 'http://www.megaupload.com/download3'), 'fileserve' => array( 'link1' => 'http://www.megaupload.com/download1', 'link2' => 'http://www.megaupload.com/download2', 'link3' => 'http://www.megaupload.com/download3') );
答案 0 :(得分:1)
这样可以解决问题:(确保域名实际显示在$ domain变量中,因为它可能是$ matches [1] ...我不记得了)
$newStructure = array();
foreach($links as $link) {
preg_match("/www\.([^\.])\.com/",$link,$matches);
$domain = $matches[0];
$currentLength = count($newStructure[$domain]);
if($currentLength) {
$newStructure[$domain]['link'.($currentLength+1)] = $link;
} else {
$newStructure[$domain] = array('link1'=>$link);
}
}
答案 1 :(得分:0)
$server = array(
'http://www.rapidshare.com/download1',
'http://www.rapidshare.com/download2',
'http://www.rapidshare.com/download3',
'http://www.megaupload.com/download1',
'http://www.megaupload.com/download2',
'http://www.megaupload.com/download3',
'http://www.fileserve.com/download1',
'http://www.fileserve.com/download2',
'http://www.fileserve.com/download3'
);
$match = array();
$myarray = array();
foreach($server as $v) {
// grab server name
preg_match('/\.(.+)\./', $v, $match);
$serverName = $match[1];
// initialize new array if its the first link of that particular server
if (!isset($myarray[$serverName])) {
$myarray[$serverName] = array();
}
// count server array to check how many links are there, and make next link key
$linkKey = 'link' . (count($myarray[$serverName]) + 1);
// store value
$myarray[$serverName][$linkKey] = $v;
}
print_r($myarray);
嘿,也许这会对你有所帮助。但我没有看到这些键的名称的目的(link1,link2等..)。这不会影响您的分页。