tags:

views:

138

answers:

1

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. I need to be able to tell which id came from what box. The ids that are in the array are only samples.

$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']

+4  A: 

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

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

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

is the same as

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

Basically, multideminsional arrays are just arrays inside of arrays.


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.

To access only one box

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

Sample Output:

9,13,15,
Chacha102
Thanks Chacha. I believe this is what I'm looking for. :)
K, I updated it with how to input it aswell.
Chacha102
Chacka, are you still here? I'd like to ask you another question.
Yes, I am still here.
Chacha102
Great. Thanks.. My question is: I see that you built the *exact* same array structure that I built but you used a shorter way of doing it. Just to satisfy my curiosity, would it be possibe to use the array structure I posted and still populate it that way? I hope I'm making sense here.
Yep, give me one second.
Chacha102
No probelem. Thanks a bunch!
Something like that suits your fancy?
Chacha102
Yes, exactly but to dynamically get those int values from my database into the array, how would I do that? I would need to put the array into a foreach loop, right?
Yes, added that into my example aswell.
Chacha102
Damn. I wish I could say that understood that example. For some reason, multidimensional arrays have always been hard for me grasp. Would you mind explaining a bit? Sorry that I'm so dense. Making them has always been easier than being able to get the data back out of them.
Ok, I tried your code and it works great. This is the line I don't get: $output[$array['Box']]
You have nested $array['box']]. Can you please explain that?
That uses `$array['box']` as the key, so if `$array['box']` was Inbox, the statement is the same as `$output['Inbox'];`
Chacha102
Ok, thanks. Let me ask you this. I have two database tables. An in and outbox. What I would like to do, is to put all the values from both of these tables into a single array. Is this possible or would I have to have 2 seperate arrays?
Put one array under $array['outbox'] and one in $array['inbox'], it is still two arrays, but they are inside a single variable.
Chacha102
Chacha, I am going to post what I have so far by updating my original post with some more code. If you don't mind having a look, I'd appreciate it.
I'm going to edit what you have right now and make it work.
Chacha102
Thank you Chacha.
I editted your main post, that should work correctly.
Chacha102
Chacha, thank you. You're right, it does work. What I was doing before you started helping me was nesting the foreach loops. I created an array for the different types of boxes like: array('inbox', 'outbox'); and then tried to do a foreach based on each box type. It didn't work though. Probably the most important question I can ask you is, is there a way to work out the arrays on paper first or maybe visualize it somehow so that I will know how to write these loops? Again, my problem is not generating the array but pulling the data back out.
Always Print_r your array, and then wrap that in `<pre>` tags. That is the best way to visualize it.
Chacha102
Ok, Chacha, Thanks for taking the time to help me and also for the excellent explanations. I'm going to bookmark this one and use it for future reference. Thanks again!