PHP - Get Key Of Arrays Based On Max() ?
I have this array:
Code: [Select] Array ( [jan] => 2 [feb] => 1 [mar] => 2 [apr] => 1 ) ..and I want to return: Code: [Select] Array ('jan', 'mar') As in, find the 2 elements with the highest count and put them in an array. What is the simplest way to achieve this? Similar TutorialsI have one array, called for example $MainImages : - Code: [Select] Array ( [0] => Array ( [widgetID] => 143 [image] => image_a.jpg ) [1] => Array ( [widgetID] => 147 [image] => image_b.jpg ) [2] => Array ( [widgetID] => 73 [image] => image_c.jpg ) ) and a second array, we'll call it $widgets: - Code: [Select] Array ( [0] => Array ( [widgetID] => 147 [seo_title] => An excellent class B Widget [name] => Widget B [categoryID] => 1 ) [1] => Array ( [widgetID] => 73 [seo_title] => An excellent class C Widget [name] => Widget C [categoryID] => 1 ) [2] => Array ( [widgetID] => 143 [seo_title] => An excellent class A Widget [name] => Widget A [categoryID] => 1 ) ) I want to merge the value from the [][image] key from the first array into a new [][image] key / value in the second, using a shared value as a reference...so the desired result array is: - Code: [Select] Array ( [0] => Array ( [widgetID] => 147 [seo_title] => An excellent class B Widget [name] => Widget B [categoryID] => 1 [image] => image_b.jpg ) [1] => Array ( [widgetID] => 73 [seo_title] => An excellent class C Widget [name] => Widget C [categoryID] => 1 [image] => image_c.jpg ) [2] => Array ( [widgetID] => 143 [seo_title] => An excellent class A Widget [name] => Widget A [categoryID] => 1 [image] => image_a.jpg ) ) Obviously the order is different and so they need to be merged based on the widgetID values. Also, the same widgetID may be in each array more than once. I've looked at a lot of help but can't find anything to do exactly what I need....any help would be greatly appreciated! I suck with arrays, there I said it . With that said, hopefully someone can help me here. I have two multi-dimensional arrays called $allteams and $chosenteams (the contents of which are listed below). I simply want to compare them and make a new array of the items that are in the first array ($allteams) but NOT in the second array ($chosenteams). NOTE: I tried to use array_diff() but couldn't get it to work since they are multidimensional... FYI, I'm also confused why the info appears to be listed twice but I think that just has to do with associative vs. numeric arrays (i.e. I think I just need to specify one and the amount info would be cut in half), but I'll research this more on my own as it seems like it has to be easy to find $allteams Array ( => Array ( => 1 [teamid] => 1 [1] => Philadelphia [teamcity] => Philadelphia [2] => Eagles [teamname] => Eagles ) [1] => Array ( => 2 [teamid] => 2 [1] => Dallas [teamcity] => Dallas [2] => Cowboys [teamname] => Cowboys ) [2] => Array ( => 3 [teamid] => 3 [1] => New York [teamcity] => New York [2] => Giants [teamname] => Giants ) [3] => Array ( => 4 [teamid] => 4 [1] => Washington [teamcity] => Washington [2] => Redskins [teamname] => Redskins ) [4] => Array ( => 5 [teamid] => 5 [1] => Detroit [teamcity] => Detroit [2] => Lions [teamname] => Lions ) [5] => Array ( => 6 [teamid] => 6 [1] => Minnesota [teamcity] => Minnesota [2] => Vikings [teamname] => Vikings ) [6] => Array ( => 7 [teamid] => 7 [1] => Green Bay [teamcity] => Green Bay [2] => Packers [teamname] => Packers ) [7] => Array ( => 8 [teamid] => 8 [1] => Chicago [teamcity] => Chicago [2] => Bears [teamname] => Bears ) [8] => Array ( => 9 [teamid] => 9 [1] => Tampa Bay [teamcity] => Tampa Bay [2] => Buccs [teamname] => Buccs ) [9] => Array ( => 10 [teamid] => 10 [1] => New Orleans [teamcity] => New Orleans [2] => Saints [teamname] => Saints ) [10] => Array ( => 11 [teamid] => 11 [1] => Carolina [teamcity] => Carolina [2] => Panthers [teamname] => Panthers ) [11] => Array ( => 12 [teamid] => 12 [1] => Atlanta [teamcity] => Atlanta [2] => Falcons [teamname] => Falcons ) [12] => Array ( => 13 [teamid] => 13 [1] => Seattle [teamcity] => Seattle [2] => Seahawks [teamname] => Seahawks ) [13] => Array ( => 14 [teamid] => 14 [1] => San Francisco [teamcity] => San Francisco [2] => 49ers [teamname] => 49ers ) [14] => Array ( => 15 [teamid] => 15 [1] => St. Louis [teamcity] => St. Louis [2] => Rams [teamname] => Rams ) [15] => Array ( => 16 [teamid] => 16 [1] => Arizona [teamcity] => Arizona [2] => Cardinals [teamname] => Cardinals ) [16] => Array ( => 17 [teamid] => 17 [1] => New York [teamcity] => New York [2] => Jets [teamname] => Jets ) [17] => Array ( => 18 [teamid] => 18 [1] => Miami [teamcity] => Miami [2] => Dolphins [teamname] => Dolphins ) [18] => Array ( => 19 [teamid] => 19 [1] => Buffalo [teamcity] => Buffalo [2] => Bills [teamname] => Bills ) [19] => Array ( => 20 [teamid] => 20 [1] => New England [teamcity] => New England [2] => Patriots [teamname] => Patriots ) [20] => Array ( => 21 [teamid] => 21 [1] => Baltimore [teamcity] => Baltimore [2] => Ravens [teamname] => Ravens ) [21] => Array ( => 22 [teamid] => 22 [1] => Cincinnati [teamcity] => Cincinnati [2] => Bengals [teamname] => Bengals ) [22] => Array ( => 23 [teamid] => 23 [1] => Pittsburgh [teamcity] => Pittsburgh [2] => Steelers [teamname] => Steelers ) [23] => Array ( => 24 [teamid] => 24 [1] => Cleveland [teamcity] => Cleveland [2] => Browns [teamname] => Browns ) [24] => Array ( => 25 [teamid] => 25 [1] => Houston [teamcity] => Houston [2] => Texans [teamname] => Texans ) [25] => Array ( => 26 [teamid] => 26 [1] => Tennessee [teamcity] => Tennessee [2] => Titans [teamname] => Titans ) [26] => Array ( => 27 [teamid] => 27 [1] => Jacksonville [teamcity] => Jacksonville [2] => Jaguars [teamname] => Jaguars ) [27] => Array ( => 28 [teamid] => 28 [1] => Indianapolis [teamcity] => Indianapolis [2] => Colts [teamname] => Colts ) [28] => Array ( => 29 [teamid] => 29 [1] => Denver [teamcity] => Denver [2] => Broncos [teamname] => Broncos ) [29] => Array ( => 30 [teamid] => 30 [1] => Kansas City [teamcity] => Kansas City [2] => Chiefs [teamname] => Chiefs ) [30] => Array ( => 31 [teamid] => 31 [1] => Oakland [teamcity] => Oakland [2] => Raiders [teamname] => Raiders ) [31] => Array ( => 32 [teamid] => 32 [1] => San Diego [teamcity] => San Diego [2] => Chargers [teamname] => Chargers ) ) $chosenteams Array ( => Array ( => 2 [teamid] => 2 [1] => Dallas [teamcity] => Dallas [2] => Cowboys [teamname] => Cowboys ) [1] => Array ( => 3 [teamid] => 3 [1] => New York [teamcity] => New York [2] => Giants [teamname] => Giants ) ) Again, I just want to compare those two multi-dimensional arrays and end up with another array that only has the items that are in the first but NOT in the second. So now I'm trying to do it the way below using simply "array_push" Code: [Select] $remainingteams = array(); foreach($allteams as $teamname=> $value) { if (in_array($teamname, $chosenteams)) { } else { array_push($remainingteams,$teamname); } } My end result based on the way the arrays are in this example should be... $remainingteams (really the only difference is that the Cowboys and Giants info is gone) Array ( => Array ( => 1 [teamid] => 1 [1] => Philadelphia [teamcity] => Philadelphia [2] => Eagles [teamname] => Eagles ) [3] => Array ( => 4 [teamid] => 4 [1] => Washington [teamcity] => Washington [2] => Redskins [teamname] => Redskins ) [4] => Array ( => 5 [teamid] => 5 [1] => Detroit [teamcity] => Detroit [2] => Lions [teamname] => Lions ) [5] => Array ( => 6 [teamid] => 6 [1] => Minnesota [teamcity] => Minnesota [2] => Vikings [teamname] => Vikings ) [6] => Array ( => 7 [teamid] => 7 [1] => Green Bay [teamcity] => Green Bay [2] => Packers [teamname] => Packers ) [7] => Array ( => 8 [teamid] => 8 [1] => Chicago [teamcity] => Chicago [2] => Bears [teamname] => Bears ) [8] => Array ( => 9 [teamid] => 9 [1] => Tampa Bay [teamcity] => Tampa Bay [2] => Buccs [teamname] => Buccs ) [9] => Array ( => 10 [teamid] => 10 [1] => New Orleans [teamcity] => New Orleans [2] => Saints [teamname] => Saints ) [10] => Array ( => 11 [teamid] => 11 [1] => Carolina [teamcity] => Carolina [2] => Panthers [teamname] => Panthers ) [11] => Array ( => 12 [teamid] => 12 [1] => Atlanta [teamcity] => Atlanta [2] => Falcons [teamname] => Falcons ) [12] => Array ( => 13 [teamid] => 13 [1] => Seattle [teamcity] => Seattle [2] => Seahawks [teamname] => Seahawks ) [13] => Array ( => 14 [teamid] => 14 [1] => San Francisco [teamcity] => San Francisco [2] => 49ers [teamname] => 49ers ) [14] => Array ( => 15 [teamid] => 15 [1] => St. Louis [teamcity] => St. Louis [2] => Rams [teamname] => Rams ) [15] => Array ( => 16 [teamid] => 16 [1] => Arizona [teamcity] => Arizona [2] => Cardinals [teamname] => Cardinals ) [16] => Array ( => 17 [teamid] => 17 [1] => New York [teamcity] => New York [2] => Jets [teamname] => Jets ) [17] => Array ( => 18 [teamid] => 18 [1] => Miami [teamcity] => Miami [2] => Dolphins [teamname] => Dolphins ) [18] => Array ( => 19 [teamid] => 19 [1] => Buffalo [teamcity] => Buffalo [2] => Bills [teamname] => Bills ) [19] => Array ( => 20 [teamid] => 20 [1] => New England [teamcity] => New England [2] => Patriots [teamname] => Patriots ) [20] => Array ( => 21 [teamid] => 21 [1] => Baltimore [teamcity] => Baltimore [2] => Ravens [teamname] => Ravens ) [21] => Array ( => 22 [teamid] => 22 [1] => Cincinnati [teamcity] => Cincinnati [2] => Bengals [teamname] => Bengals ) [22] => Array ( => 23 [teamid] => 23 [1] => Pittsburgh [teamcity] => Pittsburgh [2] => Steelers [teamname] => Steelers ) [23] => Array ( => 24 [teamid] => 24 [1] => Cleveland [teamcity] => Cleveland [2] => Browns [teamname] => Browns ) [24] => Array ( => 25 [teamid] => 25 [1] => Houston [teamcity] => Houston [2] => Texans [teamname] => Texans ) [25] => Array ( => 26 [teamid] => 26 [1] => Tennessee [teamcity] => Tennessee [2] => Titans [teamname] => Titans ) [26] => Array ( => 27 [teamid] => 27 [1] => Jacksonville [teamcity] => Jacksonville [2] => Jaguars [teamname] => Jaguars ) [27] => Array ( => 28 [teamid] => 28 [1] => Indianapolis [teamcity] => Indianapolis [2] => Colts [teamname] => Colts ) [28] => Array ( => 29 [teamid] => 29 [1] => Denver [teamcity] => Denver [2] => Broncos [teamname] => Broncos ) [29] => Array ( => 30 [teamid] => 30 [1] => Kansas City [teamcity] => Kansas City [2] => Chiefs [teamname] => Chiefs ) [30] => Array ( => 31 [teamid] => 31 [1] => Oakland [teamcity] => Oakland [2] => Raiders [teamname] => Raiders ) [31] => Array ( => 32 [teamid] => 32 [1] => San Diego [teamcity] => San Diego [2] => Chargers [teamname] => Chargers ) ) Can anyone help me figure this out? i've been trying for hours (I know, that's pathetic ) I'm having troubling with trying to create a function to spit out a single array with the following array. I can write it in a away that looks through the arrays manually. the results i am trying to generate is that each item in generated array. Array to convert array( "account" => array( "login", "register", "logout", "edit", ), "p" => array( "report", ), "array1.0" => array( "array2.0" => array( "array3.0", "array3.1 ), "array2.1", ), generating the array will look like this
Array ( [0] => account [1] => account/login [2] => account/register [3] => account/logout [4] => account/edit [5] => p [6] => p/report [7] => array1.0 [8] => array1.0/array2.0 [9] => array1.0/array2.0/array3.0 [10] => array1.0/array2.0/array3.1 [11] => array1.0/array2.1 ) The idea is that id generates a single array with combined labels and arrays inside, etc. I just can't figure out how to create a script that will create this array even If I add a new value or array.
I have this thing that i am trying to make but i cant get it to work.. can anyone help? Code: [Select] function sql_read( $dbname,$dbusername,$dbpassword ) { $names = array(); $password = array(); $connect = @mysql_connect("mysql11.000webhost.com",$dbusername,$dbpassword) or die("Could Not Connect"); @mysql_select_db ($dbname) or die("Could not find DataBase"); $query = mysql_query("select * from users"); $numrows = mysql_num_rows($query); if ($numrows > 0){ while($row = mysql_fetch_assoc($query)){ $names[] = $row["uname"]; $password[] = $row["password"]; $id = $row["id"]; } $return = array($names,$password,$id); }else{ $return = array(); } return $return[]; } $names = array(); $names = sql_read("XXXXXX","XXXXXX","XXXXXXX")[0]; The error i get is Code: [Select] Parse error: syntax error, unexpected '[' in /home/a5480952/public_html/sql/index.php on line 28 Line 28 is "$names = sql_read("XXXXXX","XXXXXX","XXXXXXX")[0];" Please help... i REALLLLD need help with this.. ask questions if you want to know more about what i am trying to do... thanks! I have two arrays, both with the same key values. I'd like to combine them. So for instance... Code: [Select] <?php $array1['abcd'] = array( 'value1' => "blah", 'value2' => "blahblah"); $array1['efgh'] = array( 'value1' => "ha", 'value2' => "haha", 'valuex' => "xyz"); $array2['abcd'] = array('value3' => "three", 'value4' => "four"); $array2['efgh'] = array( 'value3' => "hohoho", 'value6' => "six6"); function combine_arrays($array1,$array2) { //*combining* return $single_array; } echo "<pre>"; print_r(combine_arrays($array1,$array2)); echo "</pre>"; /* would produce ['abcd'] = ( 'value1' => "blah", 'value2' => "blahblah", 'value3' => "three", 'value4' => "four" ) ['efgh'] = ( 'value1' => "ha", 'value2' => "haha", 'valuex' => "xyz", 'value3' => "hohoho", 'value6' => "six6" ) */ ?> What's the easiest way to do this? Ok so this link returns HTML $hitlist = file_get_contents($iMobLink."get_hit_list?user_id=".$iStore[2]."&level=10&auth_key=".$iStore[3]); now to parse it out I have this $entry = explode('<entry>', $hitlist); $hitlist = file_get_contents($iMobLink."get_hit_list?user_id=".$iStore[2]."&level=10&auth_key=".$iStore[3]); $user_id = explode('<user_id>', $hitlist); $user_id = explode('</user_id>', $user_id[1]); $mobname = explode('<mob_name>', $hitlist); $mobname = explode('</mob_name>', $mobname[1]); $paiduser = explode('<paid_user>', $hitlist); $paiduser = explode('</paid_user>', $paid_user[1]); $amount = explode('<amount>', $hitlist); $amount = explode('</amount>', $amount[1]); $paid_time = explode('<placed_time_ago>', $hitlist); $paid_time = explode('</placed_time_ago>', $paid_time[1]); $level = explode('<level>', $hitlist); $level = explode('</level>', $level[1]); $entry = explode('</entry>', $entry[1]); echo $user_id[0]."\n"; echo urldecode($mobname[0])."\n"; echo $amount[0]."\n"; echo $paid_time[0]."\n"; echo $level[0]."\n"; echo $entry[0];} Ok now.... this only returns one entry of the xml when there are more than 10. I need it to return all ten+ of the entrys here is what the xml looks like <outer> − <xml> − <entry> − <target_user> <user_id>512433892</user_id> <mob_name>MR%2025TATT00S%2C%20Level%2015</mob_name> </target_user> − <paid_user> <user_id>554190549</user_id> <mob_name>PRINCE%20OF%20NEW</mob_name> </paid_user> <amount>48000000000</amount> <is_npc>false</is_npc> <placed_time_ago>3 days ago</placed_time_ago> <level>15</level> </entry> − <entry> − <target_user> <user_id>530120192</user_id> <mob_name>EVILNESS%20SIMPLIFIED%2C%20Level%2013</mob_name> </target_user> − <paid_user> <user_id>539494114</user_id> <mob_name>StompinDatAzz</mob_name> </paid_user> <amount>1478608800</amount> <is_npc>false</is_npc> <placed_time_ago>1 day ago</placed_time_ago> <level>13</level> </entry> − <entry> − <target_user> <user_id>526183645</user_id> <mob_name>snatch%20rider%2C%20Level%20112</mob_name> </target_user> − <paid_user> <user_id>450207248</user_id> <mob_name>ShiftyShellShock</mob_name> </paid_user> <amount>999999999</amount> <is_npc>false</is_npc> <placed_time_ago>22 hours ago</placed_time_ago> <level>112</level> </entry> − <entry> − <target_user> <user_id>542145086</user_id> <mob_name>Riding%20Raider%2C%20Level%2043</mob_name> </target_user> − <paid_user> <user_id>513441119</user_id> <mob_name>Level%20U%20uP</mob_name> </paid_user> <amount>334616000</amount> <is_npc>false</is_npc> <placed_time_ago>18 hours ago</placed_time_ago> <level>43</level> </entry> − <entry> − <target_user> <user_id>554043828</user_id> <mob_name>klaibers%20limited%2C%20Level%2011</mob_name> </target_user> − <paid_user> <user_id>522432052</user_id> <mob_name>Sloppy%20Seconds</mob_name> </paid_user> <amount>39344504405</amount> <is_npc>false</is_npc> <placed_time_ago>17 hours ago</placed_time_ago> <level>11</level> </entry> − <entry> − <target_user> <user_id>496266215</user_id> <mob_name>Geheim%20Abwher%2C%20Level%207</mob_name> </target_user> − <paid_user> <user_id>216904061</user_id> <mob_name>yve1</mob_name> </paid_user> <amount>2499912800</amount> <is_npc>false</is_npc> <placed_time_ago>16 hours ago</placed_time_ago> <level>7</level> </entry> − <entry> − <target_user> <user_id>524874924</user_id> <mob_name>MYLILFRIEND%2C%20Level%20104</mob_name> </target_user> − <paid_user> <user_id>536745842</user_id> <mob_name>SUC%20MY%20GLOCK</mob_name> </paid_user> <amount>1615247200</amount> <is_npc>false</is_npc> <placed_time_ago>15 hours ago</placed_time_ago> <level>104</level> </entry> − <entry> − <target_user> <user_id>531185082</user_id> <mob_name>THE%20RAGE%20WITHIN%2C%20Level%2031</mob_name> </target_user> − <paid_user> <user_id>512862859</user_id> <mob_name>The%20Kozmik%20Slop</mob_name> </paid_user> <amount>16536800</amount> <is_npc>false</is_npc> <placed_time_ago>14 hours ago</placed_time_ago> <level>31</level> </entry> − <entry> − <target_user> <user_id>494326270</user_id> <mob_name>RIDE%20HIS%20CUCUMBER%2C%20Level%2016</mob_name> </target_user> − <paid_user> <user_id>554794332</user_id> <mob_name>STDS%20R%20BAD%20MMKAY</mob_name> </paid_user> <amount>337825600</amount> <is_npc>false</is_npc> <placed_time_ago>14 hours ago</placed_time_ago> <level>16</level> </entry> − <entry> − <target_user> <user_id>558355598</user_id> <mob_name>DEATH%20TO%20HITLIST%2C%20Level%2011</mob_name> </target_user> − <paid_user> <user_id>558349535</user_id> <mob_name>Gangsta%20Love</mob_name> </paid_user> <amount>181600000</amount> <is_npc>false</is_npc> <placed_time_ago>10 hours ago</placed_time_ago> <level>11</level> </entry> − <entry> − <target_user> <user_id>545114043</user_id> <mob_name>Ridin%20Dirty%2C%20Level%20130</mob_name> </target_user> − <paid_user> <user_id>510805692</user_id> <mob_name>iced%20baby</mob_name> </paid_user> <amount>481680800</amount> <is_npc>false</is_npc> <placed_time_ago>10 hours ago</placed_time_ago> <level>130</level> </entry> Notice there is more than one energy.. now how to I parse it out to where I can define each entry by a variable or something... any help is appreciated.. Thanks hi all, how can i make this array dynamic. E.g, i want the numbers to be pulled from a database... Code: [Select] $s=array( "1", "2", "3", "4", "5", "6", "7", "8", "9", "10", "11", "12", "13", "14", "15", "16", "17", "18", "19", "20" ); thanks in advance, i just cant work it out I have created two classes. One is called 'move' and the other 'unit'. To make my example easier to follow, I shall use the much loved pokemon series to explain my dilemma. Here are the class declarations. new class unit { var $name, $moves; public function newUnit($name, $moves) { $this->name = $name; $this->moves = $moves; } } new class move { var $name, $attack; public function newMove($name, $attack) { $this->name = $name; $this->moves = $moves; } } Now I shall instantiate these classes (if that makes sense xD) - I just started OOP yesterday, do mind my ignorance. $unit = array(); $move = array(); $unit[0] = new unit; $unit[0]->newUnit("picachu",array("thunderbolt","quick-attack")); Now, let's say I want to populate the $move array with the moves in the unit object (thunderbolt and quick-attack). I can retrieve their attack values from a database: $thunderboltAttack = 200; $quick-attackAttack = 40; I now want to create a function that will create instances of the 'move' class to produce: $move[0] = new move; $move[0]->newMove("thunderbolt",200); $move[1] = new move; $move[1]->newMove("quick-attack",40); Here is my dire attempt to do so: function createUnits($moves){ //$moves is the array holding the unit's moves for($i; $i < count($moves); $i++) { $arr[] =new move; $arr[count($units) - 1]->name = $moves[0]; $arr[count($units) - 1]->attack = $someAttackData; } return $arr; } $moves[] = createUnits; I'm ending up with a multi-dimensional array when I want a linear one like this: $move[0] = new move; $move[0]->newMove("thunderbolt",200); $move[1] = new move; $move[1]->newMove("quick-attack",40); What do I need to do. Do I have to resort to making $move global from within the function and do things that way? I'm not sure if this can be done, but I would like to get the sum for each $.row[''] result ex: if $row['robotics'] had values of 100,150,300 I would like to be able to show a total of 550 Code: [Select] <?php echo "<Table border=1>"; echo "<tr><td></td><td>Robotics</td><td>Chiral Stuctors</td><td>Enriched Uranium</td><td>Mechanical Parts</td><td>Coolant</td><td>Consumer Electronics</td><td>Precious Metals</td><td>Reactive Metals</td><td>Oxygen</td><td>Toxic Metals</td></tr>"; $result = mysql_query("select * from `pi`"); while ($row = mysql_fetch_array($result)) { echo "<tr><td>".$row['pilot']."</td>"; echo "<td>".$row['robotics']."</td>"; echo "<td>".$row['chiralstuctors']."</td>"; echo "<td>".$row['enricheduranium']."</td>"; echo "<td>".$row['mechanicalparts']."</td>"; echo "<td>".$row['collant']."</td>"; echo "<td>".$row['consumerelectronics']."</td>"; echo "<td>".$row['preciousmetals']."</td>"; echo "<td>".$row['reactivemetals']."</td>"; echo "<td>".$row['oxygen']."</td>"; echo "<td>".$row['toxicmetals']."</td></tr>"; } echo "</table>"; ?> Hello, I have 2 array questions that I'm hoping someone can help me with. 1) First, I'm trying to add elements to a previously existing array. I start with: $data = array('text' => $_POST['text'], 'question_type' => $_POST['question_type'], 'solution' => $_POST['solution'], 'filename' => $filename, 'author_id' => $_POST['author']); Then, under certain circumstances, I'm going to want to add the following to the array $data: 'incorrect_solution1' => $_POST['incorrect_solution1'], 'incorrect_solution2' => $_POST['incorrect_solution2'], 'incorrect_solution3' => $_POST['incorrect_solution3']) I tried the concatenation function, combined with an if statement but then PHP thinks that I have a string. 2) Once I have the array, I want to shuffle the solution and 3 incorrect solution indices. I'm not sure how to shuffle just a portion of an array. Any help with either or both questions would be appreciated. Thank you. Okay so here is a very simplified version of my code in order to try and illustrate what I am trying to do: $C1 = array (174,30); $C2 = array (165,80); $S = array($C1, $C2); foreach ($S as $cluster) { $number = '5000'; $another_number = '4565'; $cluster[0] = $number; } I feel like this should be really simple but its not working. So basically I'm trying to change the first value of the array select (e.g. C1 or C2) to whatever is in the variable $number. Any help would be greatly appreciated. Thanks Hi, I need to do this: Write a method to quickly find the array index position of a given number in a ascending sorted array of numbers. e.g. in an array { 1, 2, 5, 10, 102 }, the array index position of a given number 5, would have an answer of 2. We're looking for an answer that's better than O(n). I was using php's built in methods but I need to do this with a basic function using no helper methods. Can anybody help me out with this? Thanks in advance. Hi, I'm having some trouble trying to generate my random string for a name. This is my code: Code: [Select] <?PHP function rand_string( $length ) { $chars = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789"; $size = strlen( $chars ); for( $i = 0; $i < $length; $i++ ) { $str .= $chars[ rand( 0, $size - 1 ) ]; } return $str; } $salt_string = rand_string( 5 ); $test = "1234"; echo "salt_string = $salt_string"; echo "<br>"; echo "test = 1234"; echo "<br>"; $array = array('$test', '$salt_string'); echo "<br>"; echo "array = $array"; echo "<br>"; $imploded = implode($array); echo "<br>"; echo "imploded = $imploded"; ?> And it returns: salt_string = 8pbAe test = 1234 array = Array imploded = $test$salt_string I don't understand why my imploded variable is not 12348pbAe I am trying to learn about sorting and modifying arrays and I am practicing on a book assignment but need some help to grasp what I need to do. If you go here, you will see how it is supposed to work. http://198.86.244.3/mabarckhoff/WEB182/ch11_ex1/ I need to write a case to sort the task when you click the sort button and modify when you click that button. The delete button works and the add task works. I understand the sort array and have added that case, but don't know how to get it to work with the submit. Also there is a promote button so would need 'promote' what ever you have chosen in the dropdown menu to the top. Here is my code. tasklist.php <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml"> <head> <title>Task List Manager</title> <link rel="stylesheet" type="text/css" href="main.css"/> </head> <body> <div id="page"> <div id="header"> <h1>Task List Manager</h1> </div> <div id="main"> <!-- part 1: the errors --> <?php if (count($errors) > 0) : ?> <h2>Errors:</h2> <ul> <?php foreach($errors as $error) : ?> <li><?php echo $error; ?></li> <?php endforeach; ?> </ul> <?php endif; ?> <!-- part 2: the tasks --> <h2>Tasks:</h2> <?php if (count($task_list) == 0) : ?> <p>There are no tasks in the task list.</p> <?php else: ?> <ul> <?php foreach( $task_list as $id => $task ) : ?> <li><?php echo $id + 1 . '. ' . $task; ?></li> <?php endforeach; ?> </ul> <?php endif; ?> <br /> <!-- part 3: the add form --> <h2>Add Task:</h2> <form action="." method="post" > <?php foreach( $task_list as $task ) : ?> <input type="hidden" name="tasklist[]" value="<?php echo $task; ?>"/> <?php endforeach; ?> <label>Task:</label> <input type="text" name="newtask" id="newtask"/> <br /> <label> </label> <input type="submit" name="action" value="Add Task"/> </form> <br /> <!-- part 4: the modify/promote/delete form --> <?php if (count($task_list) > 0 && empty($task_to_modify)) : ?> <h2>Select Task:</h2> <form action="." method="post" > <?php foreach( $task_list as $task ) : ?> <input type="hidden" name="tasklist[]" value="<?php echo $task; ?>"/> <?php endforeach; ?> <label>Task:</label> <select name="taskid"> <?php foreach( $task_list as $id => $task ) : ?> <option value="<?php echo $id; ?>"> <?php echo $task; ?> </option> <?php endforeach; ?> </select> <br /> <label> </label> <input type="submit" name="action" value="Modify Task"/> <input type="submit" name="action" value="Promote Task"/> <input type="submit" name="action" value="Delete Task"/> <br /> <label> </label> <input type="submit" name="action" value="Sort Tasks"/> </form> <?php endif; ?> <!-- part 5: the modify save/cancel form --> <?php if (!empty($task_to_modify)) : ?> <h2>Task to Modify:</h2> <form action="." method="post" > <?php foreach( $task_list as $task ) : ?> <input type="hidden" name="tasklist[]" value="<?php echo $task; ?>"/> <?php endforeach; ?> <label>Task:</label> <input type="hidden" name="modifiedtaskid" value="<?php echo $task_index; ?>" /> <input type="text" name="modifiedtask" value="<?php echo $task_to_modify; ?>" /><br /> <label> </label> <input type="submit" name="action" value="Save Changes"/> <input type="submit" name="action" value="Cancel Changes"/> </form> <?php print_r($_POST); ?> <?php endif; ?> </div><!-- end main --> </div><!-- end page --> </body> </html> index.php <?php if (isset($_POST['tasklist'])) { $task_list = $_POST['tasklist']; } else { $task_list = array(); // some hard-coded starting values to make testing easier $task_list[] = 'Write chapter'; $task_list[] = 'Edit chapter'; $task_list[] = 'Proofread chapter'; } $errors = array(); switch( $_POST['action'] ) { case 'Add Task': $new_task = $_POST['newtask']; if (empty($new_task)) { $errors[] = 'The new task cannot be empty.'; } else { $task_list[] = $new_task; } break; case 'Delete Task': $task_index = $_POST['taskid']; unset($task_list[$task_index]); $task_list = array_values($task_list); break; case 'Sort Task': sort($task_list); print_r($task_list); break; /* case 'Save Changes': case 'Cancel Changes': case 'Promote Task': */ } include('task_list.php'); ?> Let's say I have an array like this: Man1(0), Man1(1), Man1(2), etc. Man2(0), Man2(1), Man2(2), etc. Man3(0), Man3(1), Man3(2), etc. ... and I want to loop through the array. I know I can do: Code: [Select] $i=0; while $i<=99 {//...do something with Man1[$i];} i++; but can I also do a loop with each Man? Like: Code: [Select] $i=0;$a=1; while $i<=99 {//...do something with Man$a[$i];} i++;a++; I've written all the code for each Man separately and I'm thinking there's got to be a way to shorten the code with another loop, but I don't know how. Hope that makes sense. Thanks. Hello, I'm trying to extract information about a ticket through osTicket SOAP API, but I can't seem to figure out how! This is the return from the script (var_dump): array(1) { [0]=> object(stdClass)#2 (2) { ["question"]=> object(stdClass)#3 (4) { ["id"]=> int(25) ["created"]=> string(25) "2011-01-05T01:19:43+01:00" ["name"]=> string(4) "test" ["message"]=> string(4) "qasd" } ["answers"]=> array(1) { [0]=> object(stdClass)#4 (4) { ["id"]=> int(25) ["created"]=> string(25) "2011-01-05T01:25:06+01:00" ["name"]=> string(17) "Andreas Haakonsen" ["message"]=> string(3) "ler" } } } } A ticket can have several answers. I just can't seem to figure out how to extract/use this data and present it on my webpage. Too many arrays for me to understand I guess :p Is there anyone who can help me with some small code to help me figure this one out? Thanks! I've spent many hours trying to solve this with no success. So, here's my challenge. Sample Data from DB: Month Year Measure 5 2010 164 6 2010 31 7 2010 20 8 2010 10 9 2010 10 10 2010 10 12 2010 10 1 2011 10 I need to display this data in a chart, but notice that I don't have data for month 11 and the chart must show all months with data and the month that I don't have data for (11 in this case) will show an interrupted line. I need to put this into an array to display month/year as label and measure as chart value. So, the above data needs to become: Month Year Measure 5 2010 164 6 2010 31 7 2010 20 8 2010 10 9 2010 10 10 2010 10 11 2010 -- 12 2010 10 1 2011 10 The issue here isn't the chart, but how to add the missing month (11) to the array and keep the measures corresponding with their months. Thank you. Ok i am trying to create a simple program that will allow mwe to use an array with 8 numbers, the output form should show the numbers in their original order that the user inputs them, in numerical order, Highest number, lowest number and then the average number. I am confused on what logic i would use to do this. If anyone could help me get started it would be much appreciated. here's waht i have so far. Code: [Select] <?php $Num1 = $_POST['fielda']; $Num2 = $_POST['fieldb']; $Num3= $_POST['fieldc']; $Num4 = $_POST['fieldd']; $Num5 = $_POST['fielde']; $Num6 = $_POST['fieldf']; $Num7 = $_POST['fieldg']; $Num8 = $_POST['fieldh']; $Numbers = array(1,2,3,4,5,6,7,8); $big = max($Numbers); $small = min($Numbers); $average = array_sum($Numbers) / 8; ?> Arrays in C are fixed length:
#define MAX_SIZE 5 int main(void){ int n[ 5 ]; for(int i=0;i<MAX_SIZE;i++){ n[i]=i; } return 1; }If I try to insert an eleement into the array, it will result in undefined behavior. Java compensates for this by creating an actual object class called ArrayList or LinkedList, the former an internal store of array elements that dynamically resizes and the latter just a node tree with pointers to next and previous elements. ArrayList<Integer> arr = new ArrayList<Integer>(); arr.add(1); arr.add(2);It seens that the array() construct in PHP is not a function or a class but an internal construct. How does it dynamically resize arrays? Hello! i want to store data in a array (as seen in the code). Im trying to make it so that a php code search through the arrays and find the correct one. once it does it tells me. I dont know if im close, How ever this is what i have. $carrier = $_POST['carrier']; $carriers = array( array("verizon","tmobile","sprint","att","virgin","textnow","metro","unknown"), array("@vtext.com","@tomomail.net","@messaging.sprintpcs.com","@txt.att.net","@vmobl.com","@textnow.me","@mymetropcs.com","@teleflip.com") ); If ($carrier = |