Page 1 of 1

Array Push Question

Posted: Fri Nov 30, 2012 9:42 am
by Pavilion
Hello Everyone:

I've a problem with array_push and am hoping for some advice.

PROBLEM:
PHP picks up an array of Group IDs through the $_POST. The goal is to grab multiple Group ID's and use them to grab all members of every Group ID for display. This seems pretty straight-forward, but as things stand now, php is only returning members of the group most recently run through the foreach statement. It makes sense that I have to push the SELECT results into a multi-dimensional array and then process them for display. And that is where I'm getting hung up. Following is the applicable code:

Code: Select all

$GroupResults = Array();

if (!empty($_POST['bind_GrpArray']))
{

	foreach($_POST['bind_GrpArray'] as $item)
	{
		$prep_Groups = $link->prepare($select_OrgGroups);
		$prep_Groups->execute(array(
			':GrpID'=>$item,
		));
		$PrepResults = $prep_Groups->fetchAll();
	}

		$GroupResults = array_push($GroupResults, $PrepResults);	 // this is where I'm trying to create a multi-dimensional array. It's not working.
}

	foreach($GroupResults as $row) //This foreach does NOT return results. Error message from my CPanel says, "Invalid argument supplied for foreach()"
	{
	echo "<br /> Row: ";
	var_dump($row);
	echo "<br />";
	
		$member = stripslashes($row['LName']) . ", " . stripslashes($row['FName']);
		$memberDate = $row['MemberDate'];
		$group = $row['GroupID'];

		echo "<tr class='memberRows'>";
			echo "<td class='b_lay'>" . $member . "</td>";
			echo "<td class='c_lay'>". $MemberDate ."</td>";
		echo "</tr>";
	}
The last foreach($GroupResults as $row) does NOT return results. The error message in my CPanel says, "Invalid argument supplied for foreach()". What am I doing wrong?

Thanks for the help - in advance:

Pavilion

Re: Array Push Question

Posted: Fri Nov 30, 2012 10:16 am
by Eric!
That error means $GroupResults isn't an array. Are you sure $PrepResults is returning the array you are expecting?

Re: Array Push Question

Posted: Fri Nov 30, 2012 11:33 am
by Pavilion
Hello Eric:

Thanks for responding:
That error means $GroupResults isn't an array. Are you sure $PrepResults is returning the array you are expecting?
Following is the return for $PrepResults
array(1) { [0]=> array(10) { ["GroupID"]=> string(2) "93" [0]=> string(2) "93" ["user_id"]=> string(5) "53284" [1]=> string(5) "53284" ["MemberDate"]=> string(10) "0000-00-00" [2]=> string(10) "0000-00-00" ["LName"]=> string(14) "Duck" [3]=> string(14) "Duck" ["FName"]=> string(8) "Donald" [4]=> string(8) "Donald" } }
Is something "off" with the array itself???

PS. when I var_dump($GroupResults); the result is: int(1).

So... what is happening with the array_push(), that I'm inputting ...
array(1) { [0]=> array(10) { ["GroupID"]=> string(2) "93" [0]=> string(2) "93" ["user_id"]=> string(5) "53284" [1]=> string(5) "53284" ["MemberDate"]=> string(10) "0000-00-00" [2]=> string(10) "0000-00-00" ["LName"]=> string(14) "Duck" [3]=> string(14) "Duck" ["FName"]=> string(8) "Donald" [4]=> string(8) "Donald" } }
And... the results are: int(1)????
====================

the syntax for my array_push() is as follows:

Code: Select all

$GroupResults = array_push($GroupResults, $PrepResults);
Also $GroupResults is declared as an array before my if statement. The declaration syntax is: $GroupResults = Array();

===============================
Thanks again - Pavilion

Re: Array Push Question

Posted: Fri Nov 30, 2012 10:36 pm
by Eric!
The reason it isn't an array is you've assigned the return value to $GroupResults.

just do:

Code: Select all

array_push($GroupResults, $PrepResults);

Re: Array Push Question

Posted: Sat Dec 01, 2012 3:10 am
by twinedev
To elaborate more, from the docs for array_push
Return Values:
Returns the new number of elements in the array.
So in your case it was int(1) as you were adding 1 row to it.

(see http://php.net/manual/en/function.array-push.php) The fun part is some php array functions just act upon the arrays in the parameter (such as this one), others return the results and you have to reassign them back (like you were attempting to do). When in doubt, http://php.net/manual/en/ref.array.php is a good friend (years ago I would always get confused between array_push, array_pop, array_shift and array_unshift, and usually would have to go look them up every time to find the one I needed. Now its engraved in my braid :-)

Re: Array Push Question

Posted: Sat Dec 01, 2012 3:40 am
by Benjamin
Honestly, I've been programming for 12 years and I still have to look up the parameter order of implode ;)

Re: Array Push Question

Posted: Sat Dec 01, 2012 9:33 am
by Pavilion
Eric! wrote:The reason it isn't an array is you've assigned the return value to $GroupResults.

just do:

Code: Select all

array_push($GroupResults, $PrepResults);
Thanks everyone :)

Eric - you hit the nail on the head. I used your syntax and now all data is picked up. But... as always that leads to another problem :) Once I fully assemble $GroupResults (it will be a multi-dimensional array) how do I remove duplicates. For instance, one individual (Donald Duck) can be a member of multiple groups. So... how do I eliminate duplicate records for the final $GroupResults multi-dimensional array?

Thanks again for your help:

Pavilion

Re: Array Push Question

Posted: Sun Dec 02, 2012 8:23 am
by Eric!
This trick might work for you. You could try something like this which uses php's serialize and unserialize callbacks to modify the array structure so array_unique can work on all the elements:

Code: Select all

$GroupResults = array_map("unserialize", array_unique(array_map("serialize", $GroupResults)));
As long as ALL the elements in your duplicates are the same it should work for you. The resulting array structure might be a little different than when you started, but it should remove the duplicates.

I assume this script checks the user's credentials before it starts processing the post data, but you might want to do a little filtering on your posted data before you start processing it. Look to see if it is_array and perhaps var_filter sanitize the strings stripping the low and high values. You never know what someone might post to your script. It's good you're using PDO but just to keep your database garbage free. Also taking this precaution will make sure things like serialize/unserialize won't choke on non-ascii data.

Code: Select all

$your_values=filter_var($your_values, FILTER_SANITIZE_STRING, FILTER_FLAG_STRIP_HIGH | FILTER_FLAG_STRIP_LOW);
P.S. I can never remember parameter ordering. My brain refuses to retain those details. I've given in to using an IDE with code completion, even though code completion really annoys me.