CodeIgniter Forums
Creating an associative array through database query - Printable Version

+- CodeIgniter Forums (https://forum.codeigniter.com)
+-- Forum: Archived Discussions (https://forum.codeigniter.com/forumdisplay.php?fid=20)
+--- Forum: Archived Libraries & Helpers (https://forum.codeigniter.com/forumdisplay.php?fid=22)
+--- Thread: Creating an associative array through database query (/showthread.php?tid=43940)



Creating an associative array through database query - El Forum - 07-28-2011

[eluser]theknight[/eluser]
Code:
//for each put all the associated with that child into an array
            foreach($details as $items)
            {
            
               // $ItemsList[] = $items['Name'];
                $ItemsList = array($items['Name'] => $items['NumberNeeded'],);
                
            }
            
       //put all the items into a string
            foreach($ItemsList as $list){  
                       $body .= $list."\n";
                                
                    }

Each item has a quantity, I want to associate each item with it's quantity and put it inside of a string, so that I can use that string within a mail function.

How do I do this? I can get this to work for a single list.

Thanks


Creating an associative array through database query - El Forum - 07-29-2011

[eluser]ShawnMcCool[/eluser]
If I'm reading this correctly this is more a PHP question than a CodeIgniter question.

Specifically answering your question (as I understand it) you'd do something like this:

$item_quantity_array = array();

foreach($details as $detail)
{
$item_quantity_array[$detail['Name']] = $detail['NumberNeeded'];
}

print_r($item_quantity_array);

foreach($item_quantity_array as $item_name => $item_number_needed)
{
echo "$item_number_needed item(s) of $item_name<br/>";
}


Creating an associative array through database query - El Forum - 07-29-2011

[eluser]theknight[/eluser]
Thanks. Exactly what I was looking for.


Creating an associative array through database query - El Forum - 07-30-2011

[eluser]JoostV[/eluser]
Why store the data into a second array? Seems to me you don't need to
Code:
$body = '';
foreach($details as $detail) {
    $body .= $detail[‘Name’]] . ' of ' . $detail[‘NumberNeeded’] . "\n";
}



Creating an associative array through database query - El Forum - 07-30-2011

[eluser]ShawnMcCool[/eluser]
Yea, I always wonder how to go about responding to this kind of question. Either I could review their code base and rewrite it or I could just answer their question as they phrased it. I figured the former is unlikely.


Creating an associative array through database query - El Forum - 07-30-2011

[eluser]JoostV[/eluser]
Yeah, I know whta you mean.


Creating an associative array through database query - El Forum - 07-30-2011

[eluser]theknight[/eluser]
Thanks guys.

That is a quicker way to do the same thing.

I guess there are a 1000 ways to skin a cat.


Creating an associative array through database query - El Forum - 07-30-2011

[eluser]JoostV[/eluser]
True Smile