Busch
September 2, 2006, 6:27am
1
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
Jelena
September 2, 2006, 6:30am
2
I’m not sure I understood what you are trying to acomplish. Care to explain it a little bit more?
maskd
September 2, 2006, 6:37am
3
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
Busch
September 2, 2006, 6:41am
4
maskd:
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']);
but won’t that overwrite that last inserting into the array? will that work in a loop?
hamidof
September 2, 2006, 6:42am
5
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
Busch
September 2, 2006, 6:47am
6
thanks, hamidof. that worked.
hamidof
September 2, 2006, 6:50am
7
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*/
Busch
September 2, 2006, 8:17am
8
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)
}
hamidof
September 2, 2006, 8:26am
9
Why do you want it like that? How you want to use the result?
Busch
September 2, 2006, 8:29am
10
im doing something like this:
A community for web designers and developers to discuss everything from HTML, CSS, JavaScript, PHP, to Photoshop, SEO and more.
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?
Busch
September 2, 2006, 9:33am
11
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.