简体   繁体   English

PHP - 帮助构建多维数组

[英]PHP - Help building a multi dimensional array

I would like to know how to get the values into this array. 我想知道如何将值放入此数组中。 Can someone please help? 有人可以帮忙吗?

Each box whether it is the in or outbox should only be listed once and then have multiple ids associated with them. 无论是in还是outbox,每个框都应该只列出一次,然后将多个id与它们相关联。 I need to be able to tell which id came from what box. 我需要能够分辨哪个ID来自哪个盒子。 The ids that are in the array are only samples. 数组中的ID仅为示例。

$arr = 
array(
       'Inbox'=> array('id' => array(8, 9, 15)),
       'Outbox'=> array('id' => array(8, 9, 15))
    );

Thanks 谢谢


$inbox = $db->Query("SELECT * FROM mail_inbox");
$outbox = $db->Query("SELECT * FROM mail_outbox");


foreach($inbox as $key => $array)
{
  $output['Inbox']]['id'][] = $array['msg_seq'];
}
foreach($outbox as $key => $array)
{
  $output['Outbox']]['id'][] = $array['msg_seq'];
}

print_r($output);

This will give me the db fields from the inbox but I have no idea how to get the outbox in there as well. 这将给我收件箱中的数据库字段,但我不知道如何在那里获取发件箱。 I also get undefined index for ['Box'] 我也得到['Box']的未定义索引

Now that I know what you are saying, to input stuff, do something like this to input it into the array: 现在我知道你在说什么,输入东西,做这样的事情把它输入到数组中:

$ID = 9;
$box = "Inbox";
$arr[$box]['id'][] = $ID;

or 要么

$IDs = array(9,5,13);
$box = "Inbox";

$array = array($box => $IDs);

or if you were getting it from a Database 或者如果你从数据库中获取它

$dbarray[0] = array('ID' => 9,
                 'Box' => 'Outbox');

foreach($dbarray as $key => $array)
{
    $output[$array['Box']]['ids'][] = $array['ID'];
}

Multi deminsional arrays 多个deminsional数组

The key or index is the first bracket 键或索引是第一个括号

$array[key]="foo" 

is the same as 是相同的

$array = array('key' => 'foo');

if there is a second bracket, it of the array inside the value part of an array. 如果有第二个括号,则数组的值位于数组的值部分内。 IE IE

$array['key']['key2'] = "bar"; $ array ['key'] ['key2'] =“bar”;

is the same as 是相同的

$array = array('key' => array('key2' => 'bar')); $ array = array('key'=> array('key2'=>'bar'));

Basically, multideminsional arrays are just arrays inside of arrays. 基本上,multideminsional数组只是数组内的数组。


foreach($arr as $box => $array)
{
   echo $box;
   // $box = The Box
    foreach($array['ids'] as $ID)
   {
      echo $ID . ",";
      // $ID = The ID 
   }
   echo "<br>";
}

Sample: 样品:

Outbox 9,13,15,
Inbox 9,13,15,

This goes through each box, echos the box name, and each ID inside of the box, and echos the ID. 这将通过每个框,回显框名称和框内的每个ID,然后回显ID。

To access only one box 仅访问一个框

foreach($arr['Inbox'] as $ID)
{
    echo $ID . ",";
}

Sample Output: 样本输出:

9,13,15,

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM