制作两个数组

发布于 2024-08-30 22:59:56 字数 1400 浏览 9 评论 0原文

我想创建一个数组来告诉我的网站页面在 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"

感谢您的帮助。

I'd like to make an array which tells my site's pages where to show in PHP.
In $sor["site_id"] i've got two or four chars-lenght strings. example: 23, 42, 13, 1
In my other array (called to $pages_show) i want to give all the site_ids to an other 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
);

This script gives me the following result:

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"

But in this case I'd like to be this:

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"

Thanks for your help.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

音盲 2024-09-06 22:59:56

您必须循环遍历 $shower 数组:

$shower = explode(", ",$sor["site_id"]);
// this is explode my site_id

foreach($shower as $show) {
    $pages_show[]=array("id"=>$sor["id"],"where"=>$show);
}

You have to loop over the $shower array:

$shower = explode(", ",$sor["site_id"]);
// this is explode my site_id

foreach($shower as $show) {
    $pages_show[]=array("id"=>$sor["id"],"where"=>$show);
}
以往的大感动 2024-09-06 22:59:56

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

if the $shower is array, then $shower[$i++] nem?

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文