Jump to content

[SOLVED] Adding to MultiDimensional Arrays - Frustrating.


UpcomingPhpDev

Recommended Posts

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

 

Link to comment
Share on other sites

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
    )
)

Link to comment
Share on other sites

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

 

Link to comment
Share on other sites

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);
?>

Link to comment
Share on other sites

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

Link to comment
Share on other sites

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

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

<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.

Link to comment
Share on other sites

<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
        )

)

 

Link to comment
Share on other sites

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

Link to comment
Share on other sites

<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
        )

)

Link to comment
Share on other sites

<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
        )

)

Link to comment
Share on other sites

<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
)

Link to comment
Share on other sites

 

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>";

}
?>

Link to comment
Share on other sites

<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

Link to comment
Share on other sites

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>";

}


?>

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.