【问题标题】:Make an array of two制作两个数组
【发布时间】:2010-04-28 09:17:40
【问题描述】:

我想创建一个数组,告诉我的网站页面在 PHP 中的显示位置。
$sor["site_id"] 我有两个或四个字符长度的字符串。例如:23, 42, 13, 1
在我的另一个数组(调用$pages_show)中,我想将所有site_ids 提供给另一个ID。

$parancs="SELECT * FROM pages ORDER BY id";
$eredmeny=mysql_query($parancs) or die("Hibás SQL:".$parancs);
while($sor=mysql_fetch_array($eredmeny)) 
{
$pages[]=array(
"id"=>$sor["id"],
"name"=>$sor["name"],
"title"=>$sor["title"],
"description"=>$sor["description"],
"keywords"=>$sor["keywords"]
);
// this makes my pages array with the information about that page.

$shower = explode(", ",$sor["site_id"]);
// this is explode my site_id
$pages_show[]=array(
"id"=>$sor["id"],
"where"=>$shower
//to 'where' i want to put all the explode's elements one-by-one, to get the result like down
);

这个脚本给了我以下结果:

Array (3)
0 => Array (2)
  id => "29"
  where => Array (2)
    0 => "17"
    1 => "16"
1 => Array (2)
  id => "30"
  where => Array (1)
    0 => "17"
2 => Array (2)
  id => "31"
  where => Array (1)
    0 => "17"

但在这种情况下,我想成为这样的人:

Array (4)
0 => Array (2)
  id => "29"
  where => "17"
1 => Array (2)
  id => "29"
  where => "16"
2 => Array (2)
  id => "30"
  where => "17"
3 => Array (2)
  id => "31"
  where => "17"

感谢您的帮助。

【问题讨论】:

    标签: php arrays explode


    【解决方案1】:

    你必须遍历 $shower 数组:

    $shower = explode(", ",$sor["site_id"]);
    // this is explode my site_id
    
    foreach($shower as $show) {
        $pages_show[]=array("id"=>$sor["id"],"where"=>$show);
    }
    

    【讨论】:

      【解决方案2】:

      如果 $shower 是数组,那么 $shower[$i++] nem?

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2021-02-26
        相关资源
        最近更新 更多