JustinK101 Posted December 8, 2006 Share Posted December 8, 2006 Hello, I am trying to do something like this:$myArray = array();while(RESULTS){ array_push($myArray, $row['first_name'] . ' ' . $row['last_name'] => $row['total']);}This doesnt appear to work though, seems you cant specify the key in the function call array_push() how is this possible? I need to use array push because I am doing this work inside a loop, and its not simply one addition, its an unknown number of additions to the array.My Goal is have an array that look like:Bob Smith => 23Joe Blow => 34Jane Doe => 34etc, etc.. Link to comment https://forums.phpfreaks.com/topic/29983-solved-array_push-using-keys-and-values/ Share on other sites More sharing options...
doni49 Posted December 8, 2006 Share Posted December 8, 2006 [code]<?php$myArray = (Array("Bob Smith" => 23),Array("Joe Blow" => 34),Array("Jane Doe" => 34));$myArray = (array_merge($myArray,Array("Justin Kline" => 21));?>[/code] Link to comment https://forums.phpfreaks.com/topic/29983-solved-array_push-using-keys-and-values/#findComment-137762 Share on other sites More sharing options...
drifter Posted December 8, 2006 Share Posted December 8, 2006 $myArray[$row['first_name'] . ' ' . $row['last_name']]=$row['total']; Link to comment https://forums.phpfreaks.com/topic/29983-solved-array_push-using-keys-and-values/#findComment-137764 Share on other sites More sharing options...
JustinK101 Posted December 8, 2006 Author Share Posted December 8, 2006 Ok, I am doing the following:$myArrayKeys = array();$myArrayValues = array();while(RESULTS){array_push($myArrayKeys, $row['first_name'] . ' ' . $row['last_name']);array_push($myArrayValues, $row['total']);}$myArray = array_combine($myArrayKeys, $myArrayValues);This almost works, except if there are duplicate keys it doesnt sum up the values, how can I do this?Example:Bob Smith => 24Bob Smith => 45Joe Smith => 23Sue Smith => 12Joe Smith => 10Currently it displays:Bob Smith => 45Sue Smith => 12Joe Smith => 10How do I have it displayBob Smith = > 69Joe Smith => 33Sue Smith => 12 Link to comment https://forums.phpfreaks.com/topic/29983-solved-array_push-using-keys-and-values/#findComment-137765 Share on other sites More sharing options...
doni49 Posted December 8, 2006 Share Posted December 8, 2006 I think you'll have to check to see if "Bob Smith" is already defined as a key in the array. Then if he is, add the values. If he's not "merge" him into the array (see my reply above).To see if the key is part of the array: http://www.php.net/manual/en/function.in-array.php Link to comment https://forums.phpfreaks.com/topic/29983-solved-array_push-using-keys-and-values/#findComment-137770 Share on other sites More sharing options...
drifter Posted December 8, 2006 Share Posted December 8, 2006 [code]while(){$myArray[$row['first_name'] . ' ' . $row['last_name']]=$myArray[$row['first_name'] . ' ' . $row['last_name']] + $row['total'];}[/code] Link to comment https://forums.phpfreaks.com/topic/29983-solved-array_push-using-keys-and-values/#findComment-137771 Share on other sites More sharing options...
JustinK101 Posted December 8, 2006 Author Share Posted December 8, 2006 Doni, Any prebuilt functions for doing this? Will array_merge() do this for me? Link to comment https://forums.phpfreaks.com/topic/29983-solved-array_push-using-keys-and-values/#findComment-137772 Share on other sites More sharing options...
JustinK101 Posted December 8, 2006 Author Share Posted December 8, 2006 DRIFTER, hit the nail on the head I think. I think its working with his solution, and even better no functions so it will probably be quicker. Nice work. Link to comment https://forums.phpfreaks.com/topic/29983-solved-array_push-using-keys-and-values/#findComment-137774 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.