Jump to content

Struggling with creating a new array based on two existing arrays


galvin

Recommended Posts

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 ( [0] => Array ( [0] => 1 [teamid] => 1 [1] => Philadelphia [teamcity] => Philadelphia [2] => Eagles [teamname] => Eagles ) [1] => Array ( [0] => 2 [teamid] => 2 [1] => Dallas [teamcity] => Dallas [2] => Cowboys [teamname] => Cowboys ) [2] => Array ( [0] => 3 [teamid] => 3 [1] => New York [teamcity] => New York [2] => Giants [teamname] => Giants ) [3] => Array ( [0] => 4 [teamid] => 4 [1] => Washington [teamcity] => Washington [2] => Redskins [teamname] => Redskins ) [4] => Array ( [0] => 5 [teamid] => 5 [1] => Detroit [teamcity] => Detroit [2] => Lions [teamname] => Lions ) [5] => Array ( [0] => 6 [teamid] => 6 [1] => Minnesota [teamcity] => Minnesota [2] => Vikings [teamname] => Vikings ) [6] => Array ( [0] => 7 [teamid] => 7 [1] => Green Bay [teamcity] => Green Bay [2] => Packers [teamname] => Packers ) [7] => Array ( [0] => 8 [teamid] => 8 [1] => Chicago [teamcity] => Chicago [2] => Bears [teamname] => Bears ) [8] => Array ( [0] => 9 [teamid] => 9 [1] => Tampa Bay [teamcity] => Tampa Bay [2] => Buccs [teamname] => Buccs ) [9] => Array ( [0] => 10 [teamid] => 10 [1] => New Orleans [teamcity] => New Orleans [2] => Saints [teamname] => Saints ) [10] => Array ( [0] => 11 [teamid] => 11 [1] => Carolina [teamcity] => Carolina [2] => Panthers [teamname] => Panthers ) [11] => Array ( [0] => 12 [teamid] => 12 [1] => Atlanta [teamcity] => Atlanta [2] => Falcons [teamname] => Falcons ) [12] => Array ( [0] => 13 [teamid] => 13 [1] => Seattle [teamcity] => Seattle [2] => Seahawks [teamname] => Seahawks ) [13] => Array ( [0] => 14 [teamid] => 14 [1] => San Francisco [teamcity] => San Francisco [2] => 49ers [teamname] => 49ers ) [14] => Array ( [0] => 15 [teamid] => 15 [1] => St. Louis [teamcity] => St. Louis [2] => Rams [teamname] => Rams ) [15] => Array ( [0] => 16 [teamid] => 16 [1] => Arizona [teamcity] => Arizona [2] => Cardinals [teamname] => Cardinals ) [16] => Array ( [0] => 17 [teamid] => 17 [1] => New York [teamcity] => New York [2] => Jets [teamname] => Jets ) [17] => Array ( [0] => 18 [teamid] => 18 [1] => Miami [teamcity] => Miami [2] => Dolphins [teamname] => Dolphins ) [18] => Array ( [0] => 19 [teamid] => 19 [1] => Buffalo [teamcity] => Buffalo [2] => Bills [teamname] => Bills ) [19] => Array ( [0] => 20 [teamid] => 20 [1] => New England [teamcity] => New England [2] => Patriots [teamname] => Patriots ) [20] => Array ( [0] => 21 [teamid] => 21 [1] => Baltimore [teamcity] => Baltimore [2] => Ravens [teamname] => Ravens ) [21] => Array ( [0] => 22 [teamid] => 22 [1] => Cincinnati [teamcity] => Cincinnati [2] => Bengals [teamname] => Bengals ) [22] => Array ( [0] => 23 [teamid] => 23 [1] => Pittsburgh [teamcity] => Pittsburgh [2] => Steelers [teamname] => Steelers ) [23] => Array ( [0] => 24 [teamid] => 24 [1] => Cleveland [teamcity] => Cleveland [2] => Browns [teamname] => Browns ) [24] => Array ( [0] => 25 [teamid] => 25 [1] => Houston [teamcity] => Houston [2] => Texans [teamname] => Texans ) [25] => Array ( [0] => 26 [teamid] => 26 [1] => Tennessee [teamcity] => Tennessee [2] => Titans [teamname] => Titans ) [26] => Array ( [0] => 27 [teamid] => 27 [1] => Jacksonville [teamcity] => Jacksonville [2] => Jaguars [teamname] => Jaguars ) [27] => Array ( [0] => 28 [teamid] => 28 [1] => Indianapolis [teamcity] => Indianapolis [2] => Colts [teamname] => Colts ) [28] => Array ( [0] => 29 [teamid] => 29 [1] => Denver [teamcity] => Denver [2] => Broncos [teamname] => Broncos ) [29] => Array ( [0] => 30 [teamid] => 30 [1] => Kansas City [teamcity] => Kansas City [2] => Chiefs [teamname] => Chiefs ) [30] => Array ( [0] => 31 [teamid] => 31 [1] => Oakland [teamcity] => Oakland [2] => Raiders [teamname] => Raiders ) [31] => Array ( [0] => 32 [teamid] => 32 [1] => San Diego [teamcity] => San Diego [2] => Chargers [teamname] => Chargers ) )

 

$chosenteams

 

Array ( [0] => Array ( [0] => 2 [teamid] => 2 [1] => Dallas [teamcity] => Dallas [2] => Cowboys [teamname] => Cowboys ) [1] => Array ( [0] => 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"

$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 ( [0] => Array ( [0] => 1 [teamid] => 1 [1] => Philadelphia [teamcity] => Philadelphia [2] => Eagles [teamname] => Eagles ) [3] => Array ( [0] => 4 [teamid] => 4 [1] => Washington [teamcity] => Washington [2] => Redskins [teamname] => Redskins ) [4] => Array ( [0] => 5 [teamid] => 5 [1] => Detroit [teamcity] => Detroit [2] => Lions [teamname] => Lions ) [5] => Array ( [0] => 6 [teamid] => 6 [1] => Minnesota [teamcity] => Minnesota [2] => Vikings [teamname] => Vikings ) [6] => Array ( [0] => 7 [teamid] => 7 [1] => Green Bay [teamcity] => Green Bay [2] => Packers [teamname] => Packers ) [7] => Array ( [0] => 8 [teamid] => 8 [1] => Chicago [teamcity] => Chicago [2] => Bears [teamname] => Bears ) [8] => Array ( [0] => 9 [teamid] => 9 [1] => Tampa Bay [teamcity] => Tampa Bay [2] => Buccs [teamname] => Buccs ) [9] => Array ( [0] => 10 [teamid] => 10 [1] => New Orleans [teamcity] => New Orleans [2] => Saints [teamname] => Saints ) [10] => Array ( [0] => 11 [teamid] => 11 [1] => Carolina [teamcity] => Carolina [2] => Panthers [teamname] => Panthers ) [11] => Array ( [0] => 12 [teamid] => 12 [1] => Atlanta [teamcity] => Atlanta [2] => Falcons [teamname] => Falcons ) [12] => Array ( [0] => 13 [teamid] => 13 [1] => Seattle [teamcity] => Seattle [2] => Seahawks [teamname] => Seahawks ) [13] => Array ( [0] => 14 [teamid] => 14 [1] => San Francisco [teamcity] => San Francisco [2] => 49ers [teamname] => 49ers ) [14] => Array ( [0] => 15 [teamid] => 15 [1] => St. Louis [teamcity] => St. Louis [2] => Rams [teamname] => Rams ) [15] => Array ( [0] => 16 [teamid] => 16 [1] => Arizona [teamcity] => Arizona [2] => Cardinals [teamname] => Cardinals ) [16] => Array ( [0] => 17 [teamid] => 17 [1] => New York [teamcity] => New York [2] => Jets [teamname] => Jets ) [17] => Array ( [0] => 18 [teamid] => 18 [1] => Miami [teamcity] => Miami [2] => Dolphins [teamname] => Dolphins ) [18] => Array ( [0] => 19 [teamid] => 19 [1] => Buffalo [teamcity] => Buffalo [2] => Bills [teamname] => Bills ) [19] => Array ( [0] => 20 [teamid] => 20 [1] => New England [teamcity] => New England [2] => Patriots [teamname] => Patriots ) [20] => Array ( [0] => 21 [teamid] => 21 [1] => Baltimore [teamcity] => Baltimore [2] => Ravens [teamname] => Ravens ) [21] => Array ( [0] => 22 [teamid] => 22 [1] => Cincinnati [teamcity] => Cincinnati [2] => Bengals [teamname] => Bengals ) [22] => Array ( [0] => 23 [teamid] => 23 [1] => Pittsburgh [teamcity] => Pittsburgh [2] => Steelers [teamname] => Steelers ) [23] => Array ( [0] => 24 [teamid] => 24 [1] => Cleveland [teamcity] => Cleveland [2] => Browns [teamname] => Browns ) [24] => Array ( [0] => 25 [teamid] => 25 [1] => Houston [teamcity] => Houston [2] => Texans [teamname] => Texans ) [25] => Array ( [0] => 26 [teamid] => 26 [1] => Tennessee [teamcity] => Tennessee [2] => Titans [teamname] => Titans ) [26] => Array ( [0] => 27 [teamid] => 27 [1] => Jacksonville [teamcity] => Jacksonville [2] => Jaguars [teamname] => Jaguars ) [27] => Array ( [0] => 28 [teamid] => 28 [1] => Indianapolis [teamcity] => Indianapolis [2] => Colts [teamname] => Colts ) [28] => Array ( [0] => 29 [teamid] => 29 [1] => Denver [teamcity] => Denver [2] => Broncos [teamname] => Broncos ) [29] => Array ( [0] => 30 [teamid] => 30 [1] => Kansas City [teamcity] => Kansas City [2] => Chiefs [teamname] => Chiefs ) [30] => Array ( [0] => 31 [teamid] => 31 [1] => Oakland [teamcity] => Oakland [2] => Raiders [teamname] => Raiders ) [31] => Array ( [0] => 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 :( )

Link to comment
Share on other sites

Well, it looks like you are getting this from a database.  If so, do it in the query maybe like this:

 

SELECT * FROM all_teams WHERE team_id NOT IN (SELECT team_id FROM chosen_teams)

 

If you're stuck with the arrays, this *might* work (not tested):

 

$remainingteams = array_map('unserialize', array_diff(array_map('serialize', $allteams[0]), array_map('serialize', $chosenteams[0])));

Best to do it in the query.  If you're using mysql use mysql_fetch_assoc() to get rid of the numeric indexes.

Link to comment
Share on other sites

I agree that this does look like a query issue NOT an array problem. If you were to show the two queries you are using we could provide a single modified query to get the "unselected" teams as your result. Or, if you need the selected and the non-selected teams we could also provide a query that gets all the teams and provides a value to identified if they are selected in the result.

 

But, to answer the question directly, the array_dif() function is what would do what you are asking about. But, I think that would be the wrong approach. A change in DB queries is the solution.

 

EDIT: the reason you are getting duplicates is due to the mysql_fetch_ function you are using - I'm guessing mysql_fetch_array()?. Again, if you showed some actual code we could help. I would suggest using mysql_fetch_assoc().

Link to comment
Share on other sites

Thanks for help guys, I really appreciate it.  Here is my code with my two queries if it helps.  I am getting ALLTEAMS from a very simple "teams" mysql table that has just (teamid, teamcity and teamname).  I am getting CHOSENTEAMS by querying the "picks" table which stores everyone's pick. And each entry in the picks table refers to a "teamid" from the teams table...

 


echo "<form id='pick1' action='submit.php' method='post' />";
echo "<table id='submitform'>";

echo "\n<tr><td>High Score Pick:</td>";
echo "\n<td><select name=\"pick1\"/>";
echo "\n<option class='default' value=''>Select a team</option>";

	$sql = "SELECT * from teams";
				$getteams = mysql_query($sql, $connection);
				if (!$getteams) {
				die("Database query failed: " . mysql_error());
				} else {
					$allteams=array();
					while ($teams = mysql_fetch_assoc($getteams)) {
						$allteams[] = $teams;
					}

				}

		$sql = "SELECT teams.teamid, teams.teamcity, teams.teamname 
				FROM teams, picks
				WHERE picks.userid = " . $_SESSION['userid'] . "
				AND picks.teamid = teams.teamid";
				$getchosenteams = mysql_query($sql, $connection);
				if (!$getchosenteams) {
				die("Database query failed: " . mysql_error());
				} else {
					$chosenteams=array();
					while ($pickedteams = mysql_fetch_assoc($getchosenteams)) {
						$chosenteams[] = $pickedteams;
					}

				}


//  I was hoping to create a new array called $remainingteams (and echo the <option> tags for each of those remaining teams since they are in a Select drop down) in this vicinity, but clearly having trouble 

echo "\n</select>";
echo "\n</td>";
echo "</tr>";
echo '<input type="submit" name="submit" value="Submit High Score Pick" class="submitpicksbutton" /></td>';
echo '</tr></table></form>';

Link to comment
Share on other sites

OK, here you go. I substituted the two queries for ONE query that returns the unselected teams. Also, I have reconfigured the logic. I highly recommend you start separating the logic (*i.e. the PHP code) from the presentation (i.e. the HTML). The only PHP code I put in the HTML is echo statements to output the dynamically generated content i build in the PHP logic. There are a lot of benefits to doing this.

 

<?php

//Create and run query to get the unselected teams for user
$query = "SELECT teamid, teamcity, teamname 
          FROM teams
          WHERE teamid NOT IN (SELECT teamid
                               FROM picks
                               WHERE userid = {$_SESSION['userid']})";
$result = mysql_query($query, $connection);

//Check results
if (!$result)
{
    die("Database query failed: " . mysql_error());
}
else
{
    //Create select list options of unselected teams
    $teamOptions = '';
    while ($row = mysql_fetch_assoc($result))
    {
        $teamOptions .= "<option value='{$row['teamid']}'>{$row['teamcity']}-{$row['teamname']}</option>\n"
    }
}

?>

<form id='pick1' action='submit.php' method='post' />
<table id='submitform'>
  <tr>
    <td>High Score Pick:</td>
    <td>
      <select name=\"pick1\"/>
        <option class='default' value=''>Select a team</option>
        <?php echo $teamOptions; ?>
      </select>
    </td>
  </tr>
  <tr>
    <td><input type="submit" name="submit" value="Submit High Score Pick" class="submitpicksbutton" /></td>
  </tr>
</table>
</form>

Link to comment
Share on other sites

Thank you, thank you, thank you!  Works like a charm!  And I appreciate the tip about separating the logic.  My code was definitely starting to get pretty convoluted, but I will do my best to heed your advice going forward.

Thanks again!

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.