Filling Associative Array in a Loop

im getting some data from the database and i want to store 2 values in an associative array. i know for a regular array i’d just put this in the loop and it would be fine:

$store_data[] = $data

but how do i stick 2 piece of data in to create an associative array?

i’ve tried this but no luck:

$store_data[] = ($post_info['username'] => $post_info['total_posts']);

any ideas?
eric

I’m not sure I understood what you are trying to acomplish. Care to explain it a little bit more?

It looks like he wants to fill an associate array with $post_info[‘username’] as the key, and $post_info[‘total_posts’] as the value.

Try:


$store_data[] = array($post_info['username'] => $post_info['total_posts']);

EDIT:
Looking back at that code I don’t think it would work, considering you want to add to an array, not create a new one, this is probably not the best way to do it, but perhaps you could do this:


$temparr = array($post_info['username'] => $post_info['total_posts']);
$store_data = array_merge($store_data, $temparr);

Good luck

but won’t that overwrite that last inserting into the array? will that work in a loop?

Try this:


$index = 0; // array index
$the_array = array();

/*loop start, foreach, while, etc.*/
$the_array[$index]['username'] = $post_info['username'];
$the_array[$index]['total_posts'] = $post_info['total_posts'];
$index++;
/*end loop*/

Also known as hash table

thanks, hamidof. that worked.

There is another way too, but the first one is easier to read:


$the_array = array();

/*loop start, foreach, while, etc.*/
$the_array[] = array('username' => $post_info['username'], 'total_posts' => $post_info['total_posts']);
/*end loop*/

it seems that this method is producing an array within an array. heres what the var_dump looks like:

array(5) {
  [0]=>
  array(1) {
    ["Bob"]=>
    int(100)
  }
  [1]=>
  array(1) {
    ["Bill"]=>
    int(200)
  }
  [2]=>
  array(1) {
    ["Sam"]=>
    int(300)
  }
}

but how can i get the array to dump like this:

array(3) {
  ["Bob"]=>
  int(100)
  ["Bill"]=>
  int(200)
  ["Sam"]=>
  int(300)
}

Why do you want it like that? How you want to use the result?

im doing something like this:

and i am using the following code:

$names = array();

foreach( $posts as $name => $value )
    $names[$name] += $value;

foreach( $threads as $name => $value )
    $names[$name] += $value;

arsort ($names);

foreach($names as $adUserId => $adUserName)
    echo $adUserId . ' - ' . $adUserName . '<br />';

that code works fine when the array is like the second var_dump in my previous post but it won’t work with the first var_dump.

any ideas on how to fix that?

ok, i solved it. to keep from creating that extra “outer” array, i just stored the data in 2 single, non-associative arrays. after all the data was in the 2 arrays, i combined the arrays to make one associative array. i wanted to use array_combine but thats only available in php5 but there’s a work around at php.net.
http://www.php.net/manual/en/function.array-combine.php#58352

so basically my code looks like this:


// loop
$threadOne[] = $thread_info['postusername'];
    $threadTwo[] = $thread_info['total_threads'];
// end loop
$threads = array();
foreach($threadOne as $indexnum => $key)
   $threads[$key] = $threadTwo[$indexnum];

thanks for your help.