• 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Creating an associative array through database query

//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.


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


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

Thanks. Exactly what I was looking for.

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

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.

Yeah, I know whta you mean.

Thanks guys.

That is a quicker way to do the same thing.

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

True Smile

Digg   Delicious   Reddit   Facebook   Twitter   StumbleUpon  

  Theme © 2014 iAndrew  
Powered By MyBB, © 2002-2020 MyBB Group.