Re: [PHP] multidimensional array problems
Ive followed your example on grouping. Im still trying to understand all of the code but ive made great progess on this with your example. Now I have one last issue and this will be solved. Ill remind here what Im trying to achieve I have a table for leagues, lookup table and team roster. There can be multiple game types for each game i.e. CoD2 - CTF, CoD2 - SD, CoD2 - TDM. If a member is playing CoD2 CTF and CoD2 TDM there should be a table for each game and type showing each member playing that game/type. If a member is signed up for multiple games/types he/she should have a name listed under each game/type. Right now my php script is only sorting by game which is putting the same person in for each instance of the game instead of sorting through each game and then type. So here is my code so far and any help is greatly appreciated. ?php include (db.php); $memroster = SELECT inf_league.game, inf_league.type, inf_member.user_name, inf_member.rank, . inf_member.country, inf_member.email . FROM inf_league . INNER JOIN inf_memberleague ON inf_league.gid = inf_memberleague.l_id . INNER JOIN inf_member ON inf_member.user_id = inf_memberleague.m_id; $roster = array(); $memrosterresults = mysql_query($memroster) or die(mysql_error()); while ($record = mysql_fetch_object($memrosterresults)) { $roster[$record-game][] = $record; } ksort($roster); foreach ($roster as $game = $records) { print table\n; print caption{$game}/caption\n; print thName/th thRank/th thCountry/th thEmail/th\n; foreach ($records as $record) { print tr\n; print td{$record-user_name}/td\n; print td{$record-rank}/td\n; print td{$record-country}/td\n; print td{$record-email}/td\n; print /tr\n; } print /table\n; } ? _ Valentines Day -- Shop for gifts that spell L-O-V-E at MSN Shopping http://shopping.msn.com/content/shp/?ctId=8323,ptnrid=37,ptnrdata=24095tcode=wlmtagline -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP] multidimensional array problems
It's actually quite simple. You simply add another layer of grouping. General case: $result = mysql_query(SELECT a, b, c, d FROM foo ORDER BY a, b, c); while ($record = mysql_fetch_object($result)) { $roster[$record-a][$record-b][] = $record; } ksort($roster); foreach ($roster as $a = $bfield) { print $a; ksort($bfield); foreach ($bfield as $b = $record) { print $a: $b; print_r($record); } } Add real output syntax to taste. On Friday 19 January 2007 4:33 pm, nitrox . wrote: Ive followed your example on grouping. Im still trying to understand all of the code but ive made great progess on this with your example. Now I have one last issue and this will be solved. Ill remind here what Im trying to achieve I have a table for leagues, lookup table and team roster. There can be multiple game types for each game i.e. CoD2 - CTF, CoD2 - SD, CoD2 - TDM. If a member is playing CoD2 CTF and CoD2 TDM there should be a table for each game and type showing each member playing that game/type. If a member is signed up for multiple games/types he/she should have a name listed under each game/type. Right now my php script is only sorting by game which is putting the same person in for each instance of the game instead of sorting through each game and then type. So here is my code so far and any help is greatly appreciated. ?php include (db.php); $memroster = SELECT inf_league.game, inf_league.type, inf_member.user_name, inf_member.rank, . inf_member.country, inf_member.email . FROM inf_league . INNER JOIN inf_memberleague ON inf_league.gid = inf_memberleague.l_id . INNER JOIN inf_member ON inf_member.user_id = inf_memberleague.m_id; $roster = array(); $memrosterresults = mysql_query($memroster) or die(mysql_error()); while ($record = mysql_fetch_object($memrosterresults)) { $roster[$record-game][] = $record; } ksort($roster); foreach ($roster as $game = $records) { print table\n; print caption{$game}/caption\n; print thName/th thRank/th thCountry/th thEmail/th\n; foreach ($records as $record) { print tr\n; print td{$record-user_name}/td\n; print td{$record-rank}/td\n; print td{$record-country}/td\n; print td{$record-email}/td\n; print /tr\n; } print /table\n; } ? -- Larry Garfield AIM: LOLG42 [EMAIL PROTECTED] ICQ: 6817012 If nature has made any one thing less susceptible than all others of exclusive property, it is the action of the thinking power called an idea, which an individual may exclusively possess as long as he keeps it to himself; but the moment it is divulged, it forces itself into the possession of every one, and the receiver cannot dispossess himself of it. -- Thomas Jefferson -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP] multidimensional array problems
It's better to just leave the record as an array and read it that way. while ($row = mysql_fetch_assoc($result)) { print trtd{$row['game']}/td td{$row['type']}/td/tr\n; } And so on. You're not actually dealing with a multi-dimensional array yet; $result is an object from which you are extracting data records as one-dimensional associative arrays. On Saturday 13 January 2007 7:40 pm, nitrox doe wrote: hi all, im very new to php but i think i jumped on the toughest thing to learn. Im trying to create a team roster that will show game, game type and league and then show each member based on the game type. Ive worked out alot of code but just cant figure where im going wrong. so here is my code. Any pointers would be greatly appreciated. this is an example of what im trying to do http://www.chalkthree.com/exampleroster.html php code ?php //begin member league table $memroster = SELECT inf_league.game, inf_league.type, inf_member.user_name, inf_member.rank, . inf_member.country, inf_member.email . FROM inf_league . INNER JOIN inf_memberleague ON inf_league.gid = inf_memberleague.l_id . INNER JOIN inf_member ON inf_member.user_id = inf_memberleague.m_id; $memrosterresults = mysql_query($memroster) or die(mysql_error()); while ($row = mysql_fetch_array($memrosterresults)) { foreach ($row as $game = $type) { echo p; echo $type; foreach ($row as $type = $user_name) { echo $user_name . - . $rank . - . $country . - . $email; } print '/p'; } } //end member league table ? mysql CREATE TABLE `inf_league` ( `gid` int(11) NOT NULL auto_increment, `game` varchar(255) NOT NULL, `type` varchar(255) NOT NULL, `league` varchar(255) NOT NULL, `season` varchar(255) NOT NULL, PRIMARY KEY (`gid`)) TYPE=MyISAM AUTO_INCREMENT=4 ;-- -- Dumping data for table `inf_league`-- INSERT INTO `inf_league` (`gid`, `game`, `type`, `league`, `season`) VALUES (1, 'DF:BHD', 'TKOTH', 'TWL', '2006 1st Quarter');INSERT INTO `inf_league` (`gid`, `game`, `type`, `league`, `season`) VALUES (2, 'CoD2', 'CTF', 'TWL', '2006 2nd QTR');INSERT INTO `inf_league` (`gid`, `game`, `type`, `league`, `season`) VALUES (3, 'CoD2', 'Search Destroy', 'CAL', '2006 4th QTR');-- -- -- Table structure for table `inf_member`-- CREATE TABLE `inf_member` ( `user_id` int(11) NOT NULL auto_increment, `user_level` int(2) NOT NULL default '0', `list_order` int(3) NOT NULL default '0', `user_name` varchar(100) NOT NULL default '', `password` varchar(25) NOT NULL default '', `email` varchar(100) NOT NULL default '', `country` text NOT NULL, `game` text, `rank` varchar(40) default NULL, `qoute` longtext, `config` int(1) default '0', `map` varchar(100) default '', `gun` varchar(100) default '', `brand` varchar(100) default '', `cpu` varchar(20) default '', `ram` varchar(20) default '', `video` varchar(100) default '', `sound` varchar(100) default '', `monitor` varchar(100) default '', `mouse` varchar(100) default '', PRIMARY KEY (`user_id`)) TYPE=MyISAM AUTO_INCREMENT=3 ;-- -- Dumping data for table `inf_member`-- INSERT INTO `inf_member` (`user_id`, `user_level`, `list_order`, `user_name`, `password`, `email`, `country`, `game`, `rank`, `qoute`, `config`, `map`, `gun`, `brand`, `cpu`, `ram`, `video`, `sound`, `monitor`, `mouse`) VALUES (1, 1, 0, 'nitrox', 'test', '[EMAIL PROTECTED]', 'United States', 'CoD2', 'Founder', NULL, 0, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL);INSERT INTO `inf_member` (`user_id`, `user_level`, `list_order`, `user_name`, `password`, `email`, `country`, `game`, `rank`, `qoute`, `config`, `map`, `gun`, `brand`, `cpu`, `ram`, `video`, `sound`, `monitor`, `mouse`) VALUES (2, 1, 1, 'raze', 'itsme', '[EMAIL PROTECTED]', 'United States', NULL, 'Leader', NULL, 0, '', '', '', '', '', '', '', '', '');-- -- -- Table structure for table `inf_memberleague`-- CREATE TABLE `inf_memberleague` ( `l_id` int(4) NOT NULL, `m_id` int(4) NOT NULL) TYPE=MyISAM;-- -- Dumping data for table `inf_memberleague`-- INSERT INTO `inf_memberleague` (`l_id`, `m_id`) VALUES (1, 2);INSERT INTO `inf_memberleague` (`l_id`, `m_id`) VALUES (1, 1);INSERT INTO `inf_memberleague` (`l_id`, `m_id`) VALUES (2, 1);INSERT INTO `inf_memberleague` (`l_id`, `m_id`) VALUES (2, 2); _ Get live scores and news about your team: Add the Live.com Football Page www.live.com/?addtemplate=footballicid=T001MSN30A0701 -- Larry Garfield AIM: LOLG42 [EMAIL PROTECTED] ICQ: 6817012 If nature has made any one thing less susceptible than all others of exclusive property, it is the action of the thinking power called an idea, which an individual may exclusively possess as long as he keeps it to himself;
Re: [PHP] multidimensional array problems
Copying back to the list... Actually, I'd suggest doing a PHP-side grouping. See this article for details: http://www.garfieldtech.com/blog/php-group-by On Saturday 13 January 2007 10:50 pm, nitrox . wrote: I hope this is returning to the mail list so all can read. Thanks for the reply Larry, i appreciate your time spent to reply to me. If i print the way your showing it prints for every instance. Im trying to create a team roster like the following url shows: http://www.chalkthree.com/exampleroster.html I have 3 tables in my db, league table, lookup table and member table. Do you think it would be better possably to do seperate querys and then match them in php? would that be possable the given the setup i have? From: Larry Garfield [EMAIL PROTECTED] To: php-general@lists.php.net Subject: Re: [PHP] multidimensional array problems Date: Sat, 13 Jan 2007 21:51:08 -0600 It's better to just leave the record as an array and read it that way. while ($row = mysql_fetch_assoc($result)) { print trtd{$row['game']}/td td{$row['type']}/td/tr\n; } And so on. You're not actually dealing with a multi-dimensional array yet; $result is an object from which you are extracting data records as one-dimensional associative arrays. On Saturday 13 January 2007 7:40 pm, nitrox doe wrote: hi all, im very new to php but i think i jumped on the toughest thing to learn. Im trying to create a team roster that will show game, game type and league and then show each member based on the game type. Ive worked out alot of code but just cant figure where im going wrong. so here is my code. Any pointers would be greatly appreciated. this is an example of what im trying to do http://www.chalkthree.com/exampleroster.html php code ?php //begin member league table $memroster = SELECT inf_league.game, inf_league.type, inf_member.user_name, inf_member.rank, . inf_member.country, inf_member.email . FROM inf_league . INNER JOIN inf_memberleague ON inf_league.gid = inf_memberleague.l_id . INNER JOIN inf_member ON inf_member.user_id = inf_memberleague.m_id; $memrosterresults = mysql_query($memroster) or die(mysql_error()); while ($row = mysql_fetch_array($memrosterresults)) { foreach ($row as $game = $type) { echo p; echo $type; foreach ($row as $type = $user_name) { echo $user_name . - . $rank . - . $country . - . $email; } print '/p'; } } //end member league table ? mysql CREATE TABLE `inf_league` ( `gid` int(11) NOT NULL auto_increment, `game` varchar(255) NOT NULL, `type` varchar(255) NOT NULL, `league` varchar(255) NOT NULL, `season` varchar(255) NOT NULL, PRIMARY KEY (`gid`)) TYPE=MyISAM AUTO_INCREMENT=4 ;-- -- Dumping data for table `inf_league`-- INSERT INTO `inf_league` (`gid`, `game`, `type`, `league`, `season`) VALUES (1, 'DF:BHD', 'TKOTH', 'TWL', '2006 1st Quarter');INSERT INTO `inf_league` (`gid`, `game`, `type`, `league`, `season`) VALUES (2, 'CoD2', 'CTF', 'TWL', '2006 2nd QTR');INSERT INTO `inf_league` (`gid`, `game`, `type`, `league`, `season`) VALUES (3, 'CoD2', 'Search Destroy', 'CAL', '2006 4th QTR');-- -- -- Table structure for table `inf_member`-- CREATE TABLE `inf_member` ( `user_id` int(11) NOT NULL auto_increment, `user_level` int(2) NOT NULL default '0', `list_order` int(3) NOT NULL default '0', `user_name` varchar(100) NOT NULL default '', `password` varchar(25) NOT NULL default '', `email` varchar(100) NOT NULL default '', `country` text NOT NULL, `game` text, `rank` varchar(40) default NULL, `qoute` longtext, `config` int(1) default '0', `map` varchar(100) default '', `gun` varchar(100) default '', `brand` varchar(100) default '', `cpu` varchar(20) default '', `ram` varchar(20) default '', `video` varchar(100) default '', `sound` varchar(100) default '', `monitor` varchar(100) default '', `mouse` varchar(100) default '', PRIMARY KEY (`user_id`)) TYPE=MyISAM AUTO_INCREMENT=3 ;-- -- Dumping data for table `inf_member`-- INSERT INTO `inf_member` (`user_id`, `user_level`, `list_order`, `user_name`, `password`, `email`, `country`, `game`, `rank`, `qoute`, `config`, `map`, `gun`, `brand`, `cpu`, `ram`, `video`, `sound`, `monitor`, `mouse`) VALUES (1, 1, 0, 'nitrox', 'test', '[EMAIL PROTECTED]', 'United States', 'CoD2', 'Founder', NULL, 0, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL);INSERT INTO `inf_member` (`user_id`, `user_level`, `list_order`, `user_name`, `password`, `email`, `country`, `game`, `rank`, `qoute`, `config`, `map`, `gun`, `brand`, `cpu`, `ram`, `video`, `sound`, `monitor`, `mouse`) VALUES (2, 1
Re: [PHP] multidimensional array problems
Give this a go ?php //begin member league table $memroster = SELECT inf_league.game, inf_league.type, inf_member.user_name, inf_member.rank, . inf_member.country, inf_member.email . FROM inf_league . INNER JOIN inf_memberleague ON inf_league.gid = inf_memberleague.l_id . INNER JOIN inf_member ON inf_member.user_id = inf_memberleague.m_id; $memrosterresults = mysql_query($memroster) or die(mysql_error()); $currentType = FALSE; echo p\n; while ( $row = mysql_fetch_assoc($memrosterresults) ) { if ( $row['type'] != $currentType ) { if ( $currentType !== FALSE ) { echo /p\n; echo p\n; } echo h3{$row['type']}/h3\n; $currentType = $row['type']; } echo {$row['user_name']} - {$row['rank']} - {$row['country']} - {$row['email']}br /\n; } echo /p\n; //end member league table ? This is untested, but it should give you the results you are looking for. Jim Lucas nitrox doe wrote: hi all, im very new to php but i think i jumped on the toughest thing to learn. Im trying to create a team roster that will show game, game type and league and then show each member based on the game type. Ive worked out alot of code but just cant figure where im going wrong. so here is my code. Any pointers would be greatly appreciated. this is an example of what im trying to do http://www.chalkthree.com/exampleroster.html php code ?php //begin member league table $memroster = SELECT inf_league.game, inf_league.type, inf_member.user_name, inf_member.rank, . inf_member.country, inf_member.email . FROM inf_league . INNER JOIN inf_memberleague ON inf_league.gid = inf_memberleague.l_id . INNER JOIN inf_member ON inf_member.user_id = inf_memberleague.m_id; $memrosterresults = mysql_query($memroster) or die(mysql_error()); while ($row = mysql_fetch_array($memrosterresults)) { foreach ($row as $game = $type) { echo p; echo $type; foreach ($row as $type = $user_name) { echo $user_name . - . $rank . - . $country . - . $email; } print '/p'; } } //end member league table ? mysql CREATE TABLE `inf_league` ( `gid` int(11) NOT NULL auto_increment, `game` varchar(255) NOT NULL, `type` varchar(255) NOT NULL, `league` varchar(255) NOT NULL, `season` varchar(255) NOT NULL, PRIMARY KEY (`gid`)) TYPE=MyISAM AUTO_INCREMENT=4 ;-- -- Dumping data for table `inf_league`-- INSERT INTO `inf_league` (`gid`, `game`, `type`, `league`, `season`) VALUES (1, 'DF:BHD', 'TKOTH', 'TWL', '2006 1st Quarter');INSERT INTO `inf_league` (`gid`, `game`, `type`, `league`, `season`) VALUES (2, 'CoD2', 'CTF', 'TWL', '2006 2nd QTR');INSERT INTO `inf_league` (`gid`, `game`, `type`, `league`, `season`) VALUES (3, 'CoD2', 'Search Destroy', 'CAL', '2006 4th QTR');-- -- -- Table structure for table `inf_member`-- CREATE TABLE `inf_member` ( `user_id` int(11) NOT NULL auto_increment, `user_level` int(2) NOT NULL default '0', `list_order` int(3) NOT NULL default '0', `user_name` varchar(100) NOT NULL default '', `password` varchar(25) NOT NULL default '', `email` varchar(100) NOT NULL default '', `country` text NOT NULL, `game` text, `rank` varchar(40) default NULL, `qoute` longtext, `config` int(1) default '0', `map` varchar(100) default '', `gun` varchar(100) default '', `brand` varchar(100) default '', `cpu` varchar(20) default '', `ram` varchar(20) default '', `video` varchar(100) default '', `sound` varchar(100) default '', `monitor` varchar(100) default '', `mouse` varchar(100) default '', PRIMARY KEY (`user_id`)) TYPE=MyISAM AUTO_INCREMENT=3 ;-- -- Dumping data for table `inf_member`-- INSERT INTO `inf_member` (`user_id`, `user_level`, `list_order`, `user_name`, `password`, `email`, `country`, `game`, `rank`, `qoute`, `config`, `map`, `gun`, `brand`, `cpu`, `ram`, `video`, `sound`, `monitor`, `mouse`) VALUES (1, 1, 0, 'nitrox', 'test', '[EMAIL PROTECTED]', 'United States', 'CoD2', 'Founder', NULL, 0, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL);INSERT INTO `inf_member` (`user_id`, `user_level`, `list_order`, `user_name`, `password`, `email`, `country`, `game`, `rank`, `qoute`, `config`, `map`, `gun`, `brand`, `cpu`, `ram`, `video`, `sound`, `monitor`, `mouse`) VALUES (2, 1, 1, 'raze', 'itsme', '[EMAIL PROTECTED]', 'United States', NULL, 'Leader', NULL, 0, '', '', '', '', '', '', '', '', '');-- -- -- Table structure for table `inf_memberleague`-- CREATE TABLE `inf_memberleague` ( `l_id` int(4) NOT NULL, `m_id` int(4) NOT NULL) TYPE=MyISAM;-- -- Dumping data for table `inf_memberleague`-- INSERT INTO `inf_memberleague` (`l_id`, `m_id`) VALUES (1, 2);INSERT INTO `inf_memberleague` (`l_id`, `m_id`) VALUES (1, 1);INSERT INTO `inf_memberleague` (`l_id`, `m_id`) VALUES (2, 1);INSERT INTO
Re: [PHP] multidimensional array sort
Hello Ashley, Monday, March 14, 2005, 12:12:19 PM, you wrote: A I need to do a sort on the whole thing in such a way that: Aa) all the Dir#'s are in ascending order, and Ab) all the User#'s are in ascending order with each Dir#, and Ab) all the File#'s are also in ascending order within each User# http://us3.php.net/manual/en/function.array-multisort.php It's funny, I was in the exact same boat this morning with regards to generating a sign-in sheet for a class where I had pulled out all the students into a multi-dimensional array and needing to sort them by last name ascending. I was just about to delve into array-multisort when it dawned on me that this would be the perfect opportunity for a table join. So, I learned how to do a MySQL join (my first) a few hours ago. It works great! Much less headache too. $sqlRegisteredStudents=SELECT s.id, s.firstName, s.lastName, s.phone, s.email, s.divisionOrFacility, s.supervisorName . FROM . $tbl_registrations . r JOIN . $tbl_students . s . ON r.studentId = s.id . WHERE courseId=' . $_POST['courseID'] . ' . ORDER BY s.lastName ASC; -- Leif (TB lists moderator and fellow end user). Using The Bat! 3.0.2.3 Rush under Windows XP 5.1 Build 2600 Service Pack 2 on a Pentium 4 2GHz with 512MB -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP] multidimensional array sort
Leif Gregory wrote: http://us3.php.net/manual/en/function.array-multisort.php I did go through that, but I can't get it to work and I'm almost willing to bet it's because of the way the array is built. For example, the example on that page says that I should be able to do a sort on $ar[0] and $ar[1], however in my array both of those return 0. In fact, when I do this: echo items in array: . count($data) . br /; for ($i = 0; $i count($data); $i++) { echo data[$i]: . count($data[$i]) . br /; } I get: items in array: 2 data[0]: 0 data[1]: 0 When I do a print_r($data), I get: Array ( [John] = Array ( [KIRASH] = Array ( [0] = 050311_18.00.59__KIRASH.zip [1] = 050311_18.10.20__KIRASH.zip ) [MCCROY] = Array ( [0] = 050312_20.52.28__MCCROY.zip ) ) [Ron] = Array ( [EMBROR] = Array ( [0] = 050314_15.47.56__EMBROR.zip ) ) ) S, I'm at a loss here. I think I need to create the array differently for it to work. -- H | I haven't lost my mind; it's backed up on tape somewhere. + Ashley M. Kirchner mailto:[EMAIL PROTECTED] . 303.442.6410 x130 IT Director / SysAdmin / WebSmith . 800.441.3873 x130 Photo Craft Imaging . 3550 Arapahoe Ave. #6 http://www.pcraft.com . . .. Boulder, CO 80303, U.S.A. -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP] Multidimensional Array manipluation...
--- Dhaval Desai [EMAIL PROTECTED] wrote: $test[0] = hey; $test[1] = hi; $test[2] = hello; Now I want to hold various values in $test[0][hey] = 1 and $test[1][hi] = 2 and $test[2][hello] = 3 Try this instead: $test[0][hey] = 1; $test[1][hi] = 2; $test[2][hello] = 3; Also, remember that you can often learn these types of things with trial and error by using the print_r() function: print_r($test); Chris -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP] Multidimensional Array manipluation...
Hi, Thanx for the reply, well but what about the second part or my mail.. I want to use in_array(value, $array); and if found, I want to update the value of that variable... $test[0][hey] = 1; $test[1][hi] = 2; $test[2][hello] = 3; I want to update $test[0][hey] and set it as 1+1; I think it's possible using splice()m I don't know exactly how to use it with Multi Dimensional arrays Also is there any idea on how can we count() the values in a multi dimensional arrays... Thanx From: Chris Shiflett [EMAIL PROTECTED] Reply-To: [EMAIL PROTECTED] To: Dhaval Desai [EMAIL PROTECTED], [EMAIL PROTECTED] Subject: Re: [PHP] Multidimensional Array manipluation... Date: Thu, 2 Jan 2003 09:04:27 -0800 (PST) --- Dhaval Desai [EMAIL PROTECTED] wrote: $test[0] = hey; $test[1] = hi; $test[2] = hello; Now I want to hold various values in $test[0][hey] = 1 and $test[1][hi] = 2 and $test[2][hello] = 3 Try this instead: $test[0][hey] = 1; $test[1][hi] = 2; $test[2][hello] = 3; Also, remember that you can often learn these types of things with trial and error by using the print_r() function: print_r($test); Chris _ Add photos to your e-mail with MSN 8. Get 2 months FREE*. http://join.msn.com/?page=features/featuredemail -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP] Multidimensional Array manipluation...
$test[0][hey] = 1; $test[1][hi] = 2; $test[2][hello] = 3; I want to update $test[0][hey] and set it as 1+1; If you just want to increment the value: $test[0][hey]++; Also is there any idea on how can we count() the values in a multi dimensional arrays... The function array_count_values() might give you what you want. What are you wanting to count exactly? A good reference for you online is: http://www.php.net/manual/en/ref.array.php Chris -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP] Multidimensional Array manipluation...
Hi, Well, I will be having something like this: $se5f2254321s65s32s65[] will hold various valus like jan_1,jan-2,jan_3,jan_4 etc and then jan_1 will hold 1, jan_2=2,, jan_3=1 etc etc... Depending on certain conditions, I want to increment the values of certain values in the array...like... for($i=0; $i=count($se5f2254321s65s32s65); $i++) { $se5f2254321s65s32s65[$i][jan_1] = 1 } I want to increment it with 1 and so on for the various other values Thanx Best Regards, Dhaval From: Chris Shiflett [EMAIL PROTECTED] Reply-To: [EMAIL PROTECTED] To: Dhaval Desai [EMAIL PROTECTED] CC: [EMAIL PROTECTED] Subject: Re: [PHP] Multidimensional Array manipluation... Date: Thu, 2 Jan 2003 09:30:43 -0800 (PST) $test[0][hey] = 1; $test[1][hi] = 2; $test[2][hello] = 3; I want to update $test[0][hey] and set it as 1+1; If you just want to increment the value: $test[0][hey]++; Also is there any idea on how can we count() the values in a multi dimensional arrays... The function array_count_values() might give you what you want. What are you wanting to count exactly? A good reference for you online is: http://www.php.net/manual/en/ref.array.php Chris _ Protect your PC - get McAfee.com VirusScan Online http://clinic.mcafee.com/clinic/ibuy/campaign.asp?cid=3963 -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP] multidimensional array
On Fri, 19 Apr 2002, Rodrigo Peres wrote: In order to avoid many left joins I took an aproach that I didn't know if it's good, but I couldn't figure out another way. If my cliente has 10 phone numbers I buld an array, serialize it and store in database, so I didn't have to create another table. What do you have against left joins? miguel -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
RE: [PHP] Multidimensional array construction
I haven't seen the rest of this thread so this may be irrelevant ... but foreach($array as $key=$value) { $array[$key] = fred; } ... alters an array while traversing it. Your case seems more complicated but I would have thought nesting foreaches in this way would allow you to access the original element. foreach($array as $key1=$value1) { foreach($array as $key2=$value2) { $array[$key1][$key2] = fred; } } Tim http://www.chessish.com http://www.chessish.com -- From: Darren Gamble [SMTP:[EMAIL PROTECTED]] Sent: 04 December 2001 15:22 To: PHP List; 'Mike Eheler' Subject: RE: [PHP] Multidimensional array construction Good day, Thanks to all who replied. This isn't quite what I needed, though. I _have_ the array (or delimited list would do, too). What I need to do is _CREATE_ the array element $myarray['foo']['bar']['green']['apple'] and set it to some value. Actually traversing said array isn't hard, as you pointed out. foreach() doesn't work, as it just uses a copy of the original array. I think there might be some way to use variable references, but I haven't gotten one to work yet. Any other suggestions? Darren Gamble Planner, Regional Services Shaw Cablesystems GP 630 - 3rd Avenue SW Calgary, Alberta, Canada T2P 4L4 (403) 781-4948 -Original Message- From: Mike Eheler [mailto:[EMAIL PROTECTED]] Sent: Monday, December 03, 2001 5:30 PM To: Martin Towell Cc: 'Darren Gamble'; PHP List Subject: Re: [PHP] Multidimensional array construction I did something like this recently. Here's how I did it: $some_value1 = 'Hello World'; $myarray['foo']['bar']['green']['apple'] = $some_value1; function get_opt($arr, $keys,$sep=':') { $var = $arr; $tmp = split($sep,$keys); foreach ($tmp as $k = $v) { $var = $var[$v]; } if (isset($var)) return $var; return ''; } echo get_opt($myarray, 'foo:bar:green:apple'); It needs refining, but it should do the job. That's entirely from memory, mind you.. it should work, though. Mike Martin Towell wrote: I was thinking that you could use a pointer to var, eg: $var = 'myarray[foo][bar][red][apple]'; // this would obviously be created dynamically, hard coded for testing $$var = $some_value1; echo $myarray[foo][bar][red][apple]; but when I tried it, it didn't work :( looks like eval() to the rescue... -Original Message- From: Darren Gamble [mailto:[EMAIL PROTECTED]] Sent: Tuesday, December 04, 2001 10:37 AM To: PHP List Subject: [PHP] Multidimensional array construction Here's a question for the list: I have a two-dimensional array; essentially a list of arrays. Each element (an array) can have any number of elements. As a small example: ( ( foo , bar , red , apple ), ( foo , bar , red , car), ( foo , green ) ) I would like to traverse this array and place all of the data into another multidimensional array. The following statements illustrate how I'd like to do this from the example: $myarray[foo][bar][red][apple] = $some_value1; $myarray[foo][bar][red][car] = $some_value2; $myarray[foo][green] = $some_value3; Is there any way to easily do this in PHP? I could cheat and use eval(), but there is probably a better way. I have thought of using each() or references, but nothing has come to mind so far. Any ideas? Should I just use eval() ? Darren Gamble Planner, Regional Services Shaw Cablesystems GP 630 - 3rd Avenue SW Calgary, Alberta, Canada T2P 4L4 (403) 781-4948 -- PHP General Mailing List (http://www.php.net/) To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED] To contact the list administrators, e-mail: [EMAIL PROTECTED]
RE: [PHP] Multidimensional array construction
Good day, Thanks to all who replied. This isn't quite what I needed, though. I _have_ the array (or delimited list would do, too). What I need to do is _CREATE_ the array element $myarray['foo']['bar']['green']['apple'] and set it to some value. Actually traversing said array isn't hard, as you pointed out. foreach() doesn't work, as it just uses a copy of the original array. I think there might be some way to use variable references, but I haven't gotten one to work yet. Any other suggestions? Darren Gamble Planner, Regional Services Shaw Cablesystems GP 630 - 3rd Avenue SW Calgary, Alberta, Canada T2P 4L4 (403) 781-4948 -Original Message- From: Mike Eheler [mailto:[EMAIL PROTECTED]] Sent: Monday, December 03, 2001 5:30 PM To: Martin Towell Cc: 'Darren Gamble'; PHP List Subject: Re: [PHP] Multidimensional array construction I did something like this recently. Here's how I did it: $some_value1 = 'Hello World'; $myarray['foo']['bar']['green']['apple'] = $some_value1; function get_opt($arr, $keys,$sep=':') { $var = $arr; $tmp = split($sep,$keys); foreach ($tmp as $k = $v) { $var = $var[$v]; } if (isset($var)) return $var; return ''; } echo get_opt($myarray, 'foo:bar:green:apple'); It needs refining, but it should do the job. That's entirely from memory, mind you.. it should work, though. Mike Martin Towell wrote: I was thinking that you could use a pointer to var, eg: $var = 'myarray[foo][bar][red][apple]'; // this would obviously be created dynamically, hard coded for testing $$var = $some_value1; echo $myarray[foo][bar][red][apple]; but when I tried it, it didn't work :( looks like eval() to the rescue... -Original Message- From: Darren Gamble [mailto:[EMAIL PROTECTED]] Sent: Tuesday, December 04, 2001 10:37 AM To: PHP List Subject: [PHP] Multidimensional array construction Here's a question for the list: I have a two-dimensional array; essentially a list of arrays. Each element (an array) can have any number of elements. As a small example: ( ( foo , bar , red , apple ), ( foo , bar , red , car), ( foo , green ) ) I would like to traverse this array and place all of the data into another multidimensional array. The following statements illustrate how I'd like to do this from the example: $myarray[foo][bar][red][apple] = $some_value1; $myarray[foo][bar][red][car] = $some_value2; $myarray[foo][green] = $some_value3; Is there any way to easily do this in PHP? I could cheat and use eval(), but there is probably a better way. I have thought of using each() or references, but nothing has come to mind so far. Any ideas? Should I just use eval() ? Darren Gamble Planner, Regional Services Shaw Cablesystems GP 630 - 3rd Avenue SW Calgary, Alberta, Canada T2P 4L4 (403) 781-4948 -- PHP General Mailing List (http://www.php.net/) To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED] To contact the list administrators, e-mail: [EMAIL PROTECTED]
RE: [PHP] Multidimensional array construction
I was thinking that you could use a pointer to var, eg: $var = 'myarray[foo][bar][red][apple]'; // this would obviously be created dynamically, hard coded for testing $$var = $some_value1; echo $myarray[foo][bar][red][apple]; but when I tried it, it didn't work :( looks like eval() to the rescue... -Original Message- From: Darren Gamble [mailto:[EMAIL PROTECTED]] Sent: Tuesday, December 04, 2001 10:37 AM To: PHP List Subject: [PHP] Multidimensional array construction Here's a question for the list: I have a two-dimensional array; essentially a list of arrays. Each element (an array) can have any number of elements. As a small example: ( ( foo , bar , red , apple ), ( foo , bar , red , car), ( foo , green ) ) I would like to traverse this array and place all of the data into another multidimensional array. The following statements illustrate how I'd like to do this from the example: $myarray[foo][bar][red][apple] = $some_value1; $myarray[foo][bar][red][car] = $some_value2; $myarray[foo][green] = $some_value3; Is there any way to easily do this in PHP? I could cheat and use eval(), but there is probably a better way. I have thought of using each() or references, but nothing has come to mind so far. Any ideas? Should I just use eval() ? Darren Gamble Planner, Regional Services Shaw Cablesystems GP 630 - 3rd Avenue SW Calgary, Alberta, Canada T2P 4L4 (403) 781-4948 -- PHP General Mailing List (http://www.php.net/) To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED] To contact the list administrators, e-mail: [EMAIL PROTECTED]
RE: [PHP] Multidimensional array construction
This might be interesting ... The function extract() allows you to extract all values from an array and prefix them with a specified string. What I didn't know until just a second ago was that you can supply a function as a string, so ... $my_array = array(a,b,c,d,array(a,b,c,d)); $i = 1; extract($my_array,EXTR_PREFIX_ALL,SOMEVAL.$i++); print pre; print $SOMEVAL_1; print $SOMEVAL_2; print $SOMEVAL_3; print $SOMEVAL_4; print $SOMEVAL_5; print /pre; ... will produce: a b c d [Array] Which is cool. Not quite what you wanted, but maybe you could run with it. Jim -Original Message- From: Darren Gamble [mailto:[EMAIL PROTECTED]] Sent: Tuesday, December 04, 2001 10:37 AM To: PHP List Subject: [PHP] Multidimensional array construction Here's a question for the list: I have a two-dimensional array; essentially a list of arrays. Each element (an array) can have any number of elements. As a small example: ( ( foo , bar , red , apple ), ( foo , bar , red , car), ( foo , green ) ) I would like to traverse this array and place all of the data into another multidimensional array. The following statements illustrate how I'd like to do this from the example: $myarray[foo][bar][red][apple] = $some_value1; $myarray[foo][bar][red][car] = $some_value2; $myarray[foo][green] = $some_value3; Is there any way to easily do this in PHP? I could cheat and use eval(), but there is probably a better way. I have thought of using each() or references, but nothing has come to mind so far. Any ideas? Should I just use eval() ? Darren Gamble Planner, Regional Services Shaw Cablesystems GP 630 - 3rd Avenue SW Calgary, Alberta, Canada T2P 4L4 (403) 781-4948 -- PHP General Mailing List (http://www.php.net/) To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED] To contact the list administrators, e-mail: [EMAIL PROTECTED] -- Jim Musil - Multimedia Programmer Nettmedia - 212-629-0004 [EMAIL PROTECTED] -- PHP General Mailing List (http://www.php.net/) To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED] To contact the list administrators, e-mail: [EMAIL PROTECTED]
RE: [PHP] Multidimensional array construction
oops. That's not correct. I wish it was though! ;) This might be interesting ... The function extract() allows you to extract all values from an array and prefix them with a specified string. What I didn't know until just a second ago was that you can supply a function as a string, so ... $my_array = array(a,b,c,d,array(a,b,c,d)); $i = 1; extract($my_array,EXTR_PREFIX_ALL,SOMEVAL.$i++); print pre; print $SOMEVAL_1; print $SOMEVAL_2; print $SOMEVAL_3; print $SOMEVAL_4; print $SOMEVAL_5; print /pre; ... will produce: a b c d [Array] Which is cool. Not quite what you wanted, but maybe you could run with it. Jim -Original Message- From: Darren Gamble [mailto:[EMAIL PROTECTED]] Sent: Tuesday, December 04, 2001 10:37 AM To: PHP List Subject: [PHP] Multidimensional array construction Here's a question for the list: I have a two-dimensional array; essentially a list of arrays. Each element (an array) can have any number of elements. As a small example: ( ( foo , bar , red , apple ), ( foo , bar , red , car), ( foo , green ) ) I would like to traverse this array and place all of the data into another multidimensional array. The following statements illustrate how I'd like to do this from the example: $myarray[foo][bar][red][apple] = $some_value1; $myarray[foo][bar][red][car] = $some_value2; $myarray[foo][green] = $some_value3; Is there any way to easily do this in PHP? I could cheat and use eval(), but there is probably a better way. I have thought of using each() or references, but nothing has come to mind so far. Any ideas? Should I just use eval() ? Darren Gamble Planner, Regional Services Shaw Cablesystems GP 630 - 3rd Avenue SW Calgary, Alberta, Canada T2P 4L4 (403) 781-4948 -- PHP General Mailing List (http://www.php.net/) To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED] To contact the list administrators, e-mail: [EMAIL PROTECTED] -- Jim Musil - Multimedia Programmer Nettmedia - 212-629-0004 [EMAIL PROTECTED] -- PHP General Mailing List (http://www.php.net/) To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED] To contact the list administrators, e-mail: [EMAIL PROTECTED] -- Jim Musil - Multimedia Programmer Nettmedia - 212-629-0004 [EMAIL PROTECTED] -- PHP General Mailing List (http://www.php.net/) To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED] To contact the list administrators, e-mail: [EMAIL PROTECTED]
Re: [PHP] Multidimensional array construction
I did something like this recently. Here's how I did it: $some_value1 = 'Hello World'; $myarray['foo']['bar']['green']['apple'] = $some_value1; function get_opt($arr, $keys,$sep=':') { $var = $arr; $tmp = split($sep,$keys); foreach ($tmp as $k = $v) { $var = $var[$v]; } if (isset($var)) return $var; return ''; } echo get_opt($myarray, 'foo:bar:green:apple'); It needs refining, but it should do the job. That's entirely from memory, mind you.. it should work, though. Mike Martin Towell wrote: I was thinking that you could use a pointer to var, eg: $var = 'myarray[foo][bar][red][apple]'; // this would obviously be created dynamically, hard coded for testing $$var = $some_value1; echo $myarray[foo][bar][red][apple]; but when I tried it, it didn't work :( looks like eval() to the rescue... -Original Message- From: Darren Gamble [mailto:[EMAIL PROTECTED]] Sent: Tuesday, December 04, 2001 10:37 AM To: PHP List Subject: [PHP] Multidimensional array construction Here's a question for the list: I have a two-dimensional array; essentially a list of arrays. Each element (an array) can have any number of elements. As a small example: ( ( foo , bar , red , apple ), ( foo , bar , red , car), ( foo , green ) ) I would like to traverse this array and place all of the data into another multidimensional array. The following statements illustrate how I'd like to do this from the example: $myarray[foo][bar][red][apple] = $some_value1; $myarray[foo][bar][red][car] = $some_value2; $myarray[foo][green] = $some_value3; Is there any way to easily do this in PHP? I could cheat and use eval(), but there is probably a better way. I have thought of using each() or references, but nothing has come to mind so far. Any ideas? Should I just use eval() ? Darren Gamble Planner, Regional Services Shaw Cablesystems GP 630 - 3rd Avenue SW Calgary, Alberta, Canada T2P 4L4 (403) 781-4948 -- PHP General Mailing List (http://www.php.net/) To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED] To contact the list administrators, e-mail: [EMAIL PROTECTED]