Page 1 of 1

Array Help

Posted: Sun Jul 10, 2011 3:54 pm
by jfkbrett
Hey guys, I am new to this forum and also PHP coding in general. I am dealing with arrays right now. I have to use the array_diff function and have looked up how to use it. It looks something like this:

<?PHP
$array1 = array("a" => "green", "red", "blue", "red");
$array2 = array("b" => "green", "yellow", "red");
$result = array_diff($array1, $array2);

print_r($result);
?>

The thing is, instead of defining the arrays on this page by green, red, blue, etc., I wish to define the array as a textarea from another page. I realize that exploding is involved for making a textarea into an array, and I have used this in my formula. However, it seems to not be working. Anyone think they could help me? If you have trouble understanding my problem, please tell me.

Re: Array Help

Posted: Sun Jul 10, 2011 5:56 pm
by califdon
Be sure you clearly understand what an array is and what a textarea is. They are only related to the extent that you may exchange data between them. An array is one of the forms in which you can store data temporarily in a PHP script, just like a variable can store a single item of data, only an array can store multiple items of data. A textarea doesn't store anything, it is unknown to PHP, it is an HTML element that displays (not stores) some unformatted text in a browser.

If you will explain what it is you are trying to do (and why), we can probably help you understand this better.

Re: Array Help

Posted: Sun Jul 10, 2011 8:10 pm
by jfkbrett
I have two textareas on my first page. When the user fills these textareas out, there is a button on the bottom which is supposed to be used for removing similar items between the textareas. It links to the next page, where I want to show the difference between the textareas. I want to use an array for each textarea with each line of the textareas being an item, and then use the array_diff function to pick out the differences. Maybe there is another way to do this which doesn't involve arrays.

Re: Array Help

Posted: Sun Jul 10, 2011 8:17 pm
by Jonah Bron
You're on the right track. Post the code you tried with explode(), and we can tell you what's wrong with it.

Re: Array Help

Posted: Sun Jul 10, 2011 8:52 pm
by jfkbrett
<?PHP
$_POST['ListA'] = "ListA";
$listAarray = explode("\n", $_POST['ListA']);
$_POST['ListB'] = "ListB";
$listBarray = explode("\n", $_POST['ListB']);
$result = array_diff($listAarray, $listBarray);
print_r($result); ?>

The bolded is what I believe is wrong. It seems like a right formula, because that is the correct formula for exploding and the correct formula for the array_diff. The problem is that the text areas are on another page, so I need to define the text areas, explode them into arrays, differentiate the arrays, and show the results.

Re: Array Help

Posted: Sun Jul 10, 2011 9:04 pm
by Jonah Bron
You need to assign $_POST['ListA'] to a variable; you got it backward in a twisted sort of way :)

Code: Select all

<?PHP
$listA = explode("\n", $_POST['ListA']);
$listB = explode("\n", $_POST['ListB']);
$result = array_diff($listA, $listB);

print_r($result);
?>

Re: Array Help

Posted: Sun Jul 10, 2011 9:13 pm
by jfkbrett
See, thats a very simple function that should work fine that I have tried. However, when I do that, it states that ListA and ListB on the first two line are "undefined indexes".

"Notice: Undefined index: ListA"
"Notice: Undefined index: ListB"

Re: Array Help

Posted: Sun Jul 10, 2011 10:42 pm
by califdon
That means that on your form, you haven't named the 2 textareas "ListA" and "ListB". Whatever you assign in the form, like <textarea name="ListA" rows=xx cols==yy> will create the indexes in the $_POST array.

Re: Array Help

Posted: Sun Jul 10, 2011 10:57 pm
by jfkbrett
This is the textareas look like:

ListA
<textarea name="ListA" cols="40" rows="20">
</textarea>
<center>
ListB
<textarea name="ListB" cols="40" rows="20">
</textarea>
<input type="button" value="Remove Similar Items" onClick="window.location.href='action.php'">

as you can see, I named both correctly, which is what really confuses me. Maybe it is because the array and textareas are on two separate pages?

Re: Array Help

Posted: Mon Jul 11, 2011 10:23 am
by AbraCadaver
Your form is not posting the data. You are just redirecting with the onclick. Remove that and make sure your form action is set to action.php.

Re: Array Help

Posted: Mon Jul 11, 2011 11:07 am
by jfkbrett
Thanks for that, that actually seems to be a very big step to getting closer. Heres the new adjusted formula:
<center>
ListA
<form action='action.php' method= 'post' >
<textarea name="ListA" cols="40" rows="20">
</textarea>
ListB
<form action='action.php' method= 'post' >
<textarea name="ListB" cols="40" rows="20">
</textarea>
<input type='submit' value='Remove Similar Items'>
</form>
This formula actually gets data on the next page for the array. However, I do the array_diff function and its not getting the difference. For example, I typed in dog, cat, and mouse line by line on List A, and dog and cat line by line on List B. On the next page, cat and mouse show up, when really mouse is the only one that is supposed to because it is the only difference. Here is my formula for the action.php page:
<?PHP
$listA = explode("\n", $_POST['ListA']);
$listB = explode("\n", $_POST['ListB']);
$result = array_diff($listA, $listB);

print_r($result); ?>

Re: Array Help

Posted: Mon Jul 11, 2011 11:24 am
by AbraCadaver
May be some \r in there as well. Try:

Code: Select all

$listA = array_map('trim', explode("\n", $_POST['ListA']));
$listB = array_map('trim', explode("\n", $_POST['ListB']));

Re: Array Help

Posted: Fri Jul 15, 2011 6:26 pm
by jfkbrett
Finally working. Thanks all of you for the great help.