Array Help
Moderator: General Moderators
Array Help
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.
<?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
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.
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
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.
- Jonah Bron
- DevNet Master
- Posts: 2764
- Joined: Thu Mar 15, 2007 6:28 pm
- Location: Redding, California
Re: Array Help
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
<?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.
$_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.
- Jonah Bron
- DevNet Master
- Posts: 2764
- Joined: Thu Mar 15, 2007 6:28 pm
- Location: Redding, California
Re: Array Help
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
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"
"Notice: Undefined index: ListA"
"Notice: Undefined index: ListB"
Re: Array Help
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
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?
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?
- AbraCadaver
- DevNet Master
- Posts: 2572
- Joined: Mon Feb 24, 2003 10:12 am
- Location: The Republic of Texas
- Contact:
Re: Array Help
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.
mysql_function(): WARNING: This extension is deprecated as of PHP 5.5.0, and will be removed in the future. Instead, the MySQLi or PDO_MySQLextension should be used. See also MySQL: choosing an API guide and related FAQ for more information.
Re: Array Help
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); ?>
<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); ?>
- AbraCadaver
- DevNet Master
- Posts: 2572
- Joined: Mon Feb 24, 2003 10:12 am
- Location: The Republic of Texas
- Contact:
Re: Array Help
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']));mysql_function(): WARNING: This extension is deprecated as of PHP 5.5.0, and will be removed in the future. Instead, the MySQLi or PDO_MySQLextension should be used. See also MySQL: choosing an API guide and related FAQ for more information.
Re: Array Help
Finally working. Thanks all of you for the great help.