UpcomingPhpDev Posted December 21, 2008 Share Posted December 21, 2008 Hey guys, Ive tried so many foreach loops and while combinations using so many different functions that I just dont know what to do now. The problem I belive is im getting confused, Hopefully some1 who has used multidimensional arrays before can help me Basically the problem is I am trying to add fields from a text area into a ArrayArray, But as they come out of the textarea box they are not individual values, They are one big value appended to one key. e.g. [0] => This is line 1 This is line 2 This is line 3 So I implode then explode the values, Now I need to add them to the MAIN city name array and remove the exsiting key, Here is my Code as you can see where Im trying to go. <table cellspacing="0"> <form method="POST"> <tr> <td><input value="Ny" name="City[Ny][]" /></td> <td><textarea name="City[][]"></textarea></td> </tr> <tr> <td><input value="Ca" name="Country[Ca][]" /></td> <td><textarea name="City[][]"></textarea></td> </tr> <input type="submit" /> </form> </table> <?php $City = $_POST['City']; print "<PRE>"; print_r($City); print "</PRE>"; $No = 0; foreach($City as $CityId) { $New = implode("\n",$CityId); $New = explode("\n",$New); $Count = count($New); //echo $Count; print "<PRE>"; print_r($New); print "</PRE>"; } ?> Any Help greatly appreciated. Thanks in advance Quote Link to comment Share on other sites More sharing options...
DeanWhitehouse Posted December 21, 2008 Share Posted December 21, 2008 cam you post the result of the print_r Quote Link to comment Share on other sites More sharing options...
UpcomingPhpDev Posted December 21, 2008 Author Share Posted December 21, 2008 cam you post the result of the print_r Yes sure, Thanks Array ( [Ny] => Array ( [0] => this is ny line 1 this is ny line 2 ) [Ca] => Array ( [0] => this is ca line 1 this is ca line 2 ) ) Array ( [0] => Ny ) Array ( [0] => this is ny line 1 [1] => this is ny line 2 ) Array ( [0] => this is ca line 1 [1] => this is ca line 2 ) Basically what im trying to achieve is something like Array ( [Ny] => Array ( [0] => this is ny line 1 [1] => this is ny line 2 ) [Ca] => Array ( [0] => this is ca line 1 [1] => this is ca line 2 ) ) Quote Link to comment Share on other sites More sharing options...
DeanWhitehouse Posted December 21, 2008 Share Posted December 21, 2008 ok, i understand try <tr> <td><input value="Ny" name="City[Ny][]" /></td> <td><textarea name="City[Ny][]"></textarea></td> </tr> <tr> <td><input value="Ca" name="Country[Ca][]" /></td> <td><textarea name="City[Ca][]"></textarea></td> </tr> Quote Link to comment Share on other sites More sharing options...
UpcomingPhpDev Posted December 21, 2008 Author Share Posted December 21, 2008 ok, i understand try <tr> <td><input value="Ny" name="City[Ny][]" /></td> <td><textarea name="City[Ny][]"></textarea></td> </tr> <tr> <td><input value="Ca" name="Country[Ca][]" /></td> <td><textarea name="City[Ca][]"></textarea></td> </tr> Hi, Yes ive tried this, The problem is it adds the values as one big line e.g. Array - Gb Array - 0 => this is line 1 this is line 2 this is line 3 I need something like Array - Gb Array - 0 => this is line 1 1=> this is line 2 2 => this is line 3 Sorry if I didnt make it clear, Thx in advance Quote Link to comment Share on other sites More sharing options...
DeanWhitehouse Posted December 21, 2008 Share Posted December 21, 2008 What does this give you <table cellspacing="0"> <form method="POST"> <tr> <td><input value="Ny" name="City[Ny][]" /></td> <td><textarea name="City[Ny][]"></textarea></td> </tr> <tr> <td><input value="Ca" name="City[Ca][]" /></td> <td><textarea name="City[Ca][]"></textarea></td> </tr> <input type="submit" /> </form> </table> <?php $City = $_POST['City']; $Ny = explode("/n", $City[Ny][0]); $Ca = explode("/n", $City[Ca][0]); print_r($Ny); print_r($Ca); ?> Quote Link to comment Share on other sites More sharing options...
UpcomingPhpDev Posted December 21, 2008 Author Share Posted December 21, 2008 What does this give you <table cellspacing="0"> <form method="POST"> <tr> <td><input value="Ny" name="City[Ny][]" /></td> <td><textarea name="City[Ny][]"></textarea></td> </tr> <tr> <td><input value="Ca" name="City[Ca][]" /></td> <td><textarea name="City[Ca][]"></textarea></td> </tr> <input type="submit" /> </form> </table> <?php $City = $_POST['City']; $Ny = explode("/n", $City[Ny][0]); $Ca = explode("/n", $City[Ca][0]); print_r($Ny); print_r($Ca); ?> This gives me Array ( [0] => Ny ) Array ( [0] => Ca ) Doesnt seem to work, Is their anyway you can do it through a loop? The problem being is that I have over 30 cities, I only posted 2 with the foreach loop to prevent the post being to long. Thanks again for helping <table cellspacing="0"> <form method="POST"> <tr> <td><input value="Ny" name="City[Ny][]" /></td> <td><textarea name="City[Ny][]"></textarea></td> </tr> <tr> <td><input value="Ca" name="Country[Ca][]" /></td> <td><textarea name="City[Ca][]"></textarea></td> </tr> <input type="submit" /> </form> </table> <?php $City = $_POST['City']; print "<PRE>"; print_r($City); print "</PRE>"; $No = 0; foreach($City as $CityId) { $New = implode("\n",$CityId); $New = explode("\n",$New); $Count = count($New); echo $Count; print "<PRE>"; print_r($New); print "</PRE>"; } ?> This gives me Array ( [Ny] => Array ( [0] => sfgs dfgfdg ) [Ca] => Array ( [0] => fd gfdgd ) ) ------------------------------------------------ 2 Array ( [0] => sfgs [1] => dfgfdg ) 2 Array ( [0] => fd [1] => gfdgd ) If there is some way to add the last 2 loop printed arrays to the boss array(ny,Ca), Thats the aim Quote Link to comment Share on other sites More sharing options...
DeanWhitehouse Posted December 21, 2008 Share Posted December 21, 2008 Hmm not sure when you are using custom keys like that. Maybe look through this http://djw-webdesign.awardspace.com/code.php?snippet=8 If that doesn't help i might be able to make a code to do it. Quote Link to comment Share on other sites More sharing options...
UpcomingPhpDev Posted December 21, 2008 Author Share Posted December 21, 2008 Hmm not sure when you are using custom keys like that. Maybe look through this http://djw-webdesign.awardspace.com/code.php?snippet=8 If that doesn't help i might be able to make a code to do it. Can you check my above post, I just edited it again, I belive im close to achieving it, I just dont know how to add those last 2 printed arrays to the main arays. Thanks for the help Quote Link to comment Share on other sites More sharing options...
premiso Posted December 21, 2008 Share Posted December 21, 2008 Alright man. Your problem is you need a delimieter to know how line 1 is different than line 2. Either an enter break "\n" or something like this is line1 | this is line 2 In order to do it how you want it is virtually impossible unless you know how many words are allowed or there is a specific word you want. The line break would be the easiest. So when you enter in the text box it looks like: this is line 1 this is line 2 Then you can easily explode that by "\n" in to an array and put that into the "ny" section. Quote Link to comment Share on other sites More sharing options...
DeanWhitehouse Posted December 21, 2008 Share Posted December 21, 2008 try removing the implode. Quote Link to comment Share on other sites More sharing options...
premiso Posted December 21, 2008 Share Posted December 21, 2008 <table cellspacing="0"> <form method="POST"> <tr> <td><input value="Ny" name="City[Ny][]" /></td> <td><textarea name="City[][]"></textarea></td> </tr> <tr> <td><input value="Ca" name="Country[Ca][]" /></td> <td><textarea name="City[][]"></textarea></td> </tr> <input type="submit" /> </form> </table> <?php $City = $_POST['City']; print "<PRE>"; print_r($City); print "</PRE>"; $No = 0; foreach($City as $key => $CityId) { if (!is_array($CityId)) { $CityId = explode("\n", $CityId); $New[$key] = $CityId; } //$New = implode("\n",$CityId); //$New = explode("\n",$New); //$Count = count($New); //echo $Count; print "<PRE>" . print_r($New,1) . "</PRE>"; } ?> Give that a try. Quote Link to comment Share on other sites More sharing options...
UpcomingPhpDev Posted December 21, 2008 Author Share Posted December 21, 2008 <table cellspacing="0"> <form method="POST"> <tr> <td><input value="Ny" name="City[Ny][]" /></td> <td><textarea name="City[][]"></textarea></td> </tr> <tr> <td><input value="Ca" name="Country[Ca][]" /></td> <td><textarea name="City[][]"></textarea></td> </tr> <input type="submit" /> </form> </table> <?php $City = $_POST['City']; print "<PRE>"; print_r($City); print "</PRE>"; $No = 0; foreach($City as $key => $CityId) { if (!is_array($CityId)) { $CityId = explode("\n", $CityId); $New[$key] = $CityId; } //$New = implode("\n",$CityId); //$New = explode("\n",$New); //$Count = count($New); //echo $Count; print "<PRE>" . print_r($New,1) . "</PRE>"; } ?> Give that a try. Hi, thanks for offering ur help This returns. Its worked a bit, But still hasnt solved the problem, Let me try editing it a bit. Thanks in advance again Array ( [Ny] => Array ( [0] => Ny ) [0] => Array ( [0] => Ny line 1 Ny Line 2 ) [1] => Array ( [0] => Ca Line 1 Ca Line 2 ) ) [0] => Array ( [0] => dsggdfdfhfdghghghfgh hgfhgfhgfhgfh ) [1] => Array ( [0] => fdgdfgdfg dfgfdgdfgfd ) ) Quote Link to comment Share on other sites More sharing options...
UpcomingPhpDev Posted December 21, 2008 Author Share Posted December 21, 2008 Well, Is their not a way to add 1 Array to the other array, The best I can produce is this? Ive tried to simplify the code by shortening the html fields to make it more readable, Il never try to use multidimensional arrays anymore, there to much confusiion <form method="POST"> <input value="Ny" name="Country[Ny][]" /> <textarea name="Country[Ny][0]"></textarea> <input type="submit" /></form> <?php $Country = $_POST['Country']; print "<PRE>"; print_r($Country); print "</PRE>"; foreach($Country as $CountryId) { $New = implode("\n",$CountryId); $New = explode("\n",$New); print "<PRE>"; print_r($New); print "</PRE>"; } ?> OUTPUT Array ( [Ny] => Array ( [0] => this is line 1 ny this is line 2 ny ) ) Array ( [0] => this is line 1 ny [1] => this is line 2 ny ) I just need to somehow turn that into this. Array ( [Ny] => Array ( [0] => this is line 1 ny [1] => this is line 2 ny ) ) Thanks in advance Quote Link to comment Share on other sites More sharing options...
redarrow Posted December 21, 2008 Share Posted December 21, 2008 <form method="POST"> <textarea name="Country[Ny][]"></textarea> <textarea name="Country[Ny][]"></textarea> <input type="submit" /></form> <?php $Country = $_POST['Country']; print "<PRE>"; print_r($Country); print "</PRE>"; foreach($Country as $CountryId) { print "<PRE>"; print_r($New); print "</PRE>"; } ?> Array ( [Ny] => Array ( [0] => john [1] => john ) ) Quote Link to comment Share on other sites More sharing options...
DeanWhitehouse Posted December 21, 2008 Share Posted December 21, 2008 To join two array's http://uk3.php.net/array-merge Quote Link to comment Share on other sites More sharing options...
UpcomingPhpDev Posted December 21, 2008 Author Share Posted December 21, 2008 <form method="POST"> <textarea name="Country[Ny][]"></textarea> <textarea name="Country[Ny][]"></textarea> <input type="submit" /></form> <?php $Country = $_POST['Country']; print "<PRE>"; print_r($Country); print "</PRE>"; foreach($Country as $CountryId) { print "<PRE>"; print_r($New); print "</PRE>"; } ?> Array ( [Ny] => Array ( [0] => john [1] => john ) ) You have replaced the input text with a textarea, Also, Wen you insert more then 1 line in each of your two textboxes, were back to square one. - E.g Array ( [Ny] => Array ( [0] => john john john [1] => john fsgfdg fdgfdg Needs to be Array ( [Ny] => Array ( [0] => john [1] => John [2] => john [3] => fsgfdg [4] =>fdgfdg ) ) Quote Link to comment Share on other sites More sharing options...
redarrow Posted December 21, 2008 Share Posted December 21, 2008 <form method="POST"> <input type="text" name="Country[]"></textarea> <textarea name="Country1[]"></textarea> <input type="submit" /></form> <?php $Country = $_POST['Country']; foreach($_POST['Country1'] as $res){ $r=explode(' ',$res); $result=array_merge($r,$Country); print "<PRE>"; print_r($result); print "</PRE>"; } ?> any better Array ( [0] => john [1] => john [2] => john [3] => john [4] => petter [5] => paul [6] => john ) Quote Link to comment Share on other sites More sharing options...
DeanWhitehouse Posted December 21, 2008 Share Posted December 21, 2008 $r=explode(' ',$res); should be $r=explode('/n',$res); Quote Link to comment Share on other sites More sharing options...
redarrow Posted December 21, 2008 Share Posted December 21, 2008 you can not get it to work if you press the enter key in ie lol with the /n or firefox bad coding lol... Quote Link to comment Share on other sites More sharing options...
DeanWhitehouse Posted December 21, 2008 Share Posted December 21, 2008 ok, then $r = nl2br($res); $r=explode('<br />',$r);//i think that is how nl2br writes br line Quote Link to comment Share on other sites More sharing options...
redarrow Posted December 21, 2008 Share Posted December 21, 2008 nearest ur get m8. <form method="POST"> <input type="text" name="Country[]"></textarea> <textarea name="Country1[]"></textarea> <input type="submit" /></form> <?php $Country = $_POST['Country']; foreach($_POST['Country1'] as $res){ $r = nl2br($res); $r=explode('<br />',$r); $result=array_merge($r,$Country); print "<PRE>"; print_r($result); print "</PRE>"; } ?> Quote Link to comment Share on other sites More sharing options...
UpcomingPhpDev Posted December 21, 2008 Author Share Posted December 21, 2008 <form method="POST"> <input type="text" name="Country[]"></textarea> <textarea name="Country1[]"></textarea> <input type="submit" /></form> <?php $Country = $_POST['Country']; foreach($_POST['Country1'] as $res){ $r=explode(' ',$res); $result=array_merge($r,$Country); print "<PRE>"; print_r($result); print "</PRE>"; } ?> any better Array ( [0] => john [1] => john [2] => john [3] => john [4] => petter [5] => paul [6] => john ) redarrow, you have changed the name of the textarea The reason why I didnt do this in the first place, Is because I have over 30 countires Otherwise I wud have just made the whole array in php by typing it... Multidimensional arrays are just a pain in the asss Thx for the help thoiugh Btw, I have nearly solved it, Just have another issue. Il post the solution wen or if i manage to do it, Been trying this shit for too long Quote Link to comment Share on other sites More sharing options...
redarrow Posted December 21, 2008 Share Posted December 21, 2008 the tightest i can get it by a added ltrim <form method="POST"> <input type="text" name="Country[]"></textarea> <textarea name="Country1[]"></textarea> <input type="submit" /></form> <?php $Country = $_POST['Country']; foreach($_POST['Country1'] as $res){ $r = nl2br($res); $r=explode('<br />',ltrim($r)); $result=array_merge($r,$Country); print "<PRE>"; print_r($result); print "</PRE>"; } ?> Quote Link to comment Share on other sites More sharing options...
redarrow Posted December 21, 2008 Share Posted December 21, 2008 sorry for trying m8 ive done my best.. Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.