Problems with implode() - Programmers Heaven

Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

Categories

Welcome to the new platform of Programmer's Heaven! We apologize for the inconvenience caused, if you visited us from a broken link of the previous version. The main reason to move to a new platform is to provide more effective and collaborative experience to you all. Please feel free to experience the new platform and use its exciting features. Contact us for any issue that you need to get clarified. We are more than happy to help you.

Problems with implode()

NeakNeak Posts: 87Member
I have a file like this:
[code] Title0
Date0
Author0
Content0

Title1
Date1
Author1
Content1[/code]


I explode it, so I get an array with two parts. I call it $exploded:
[code] 0
Title0
Date0
Author0
Content0
1
Title1
Date1
Author1
Content1[/code]

So, I want to add a part. I make a new array like this:
[code] $NewStuff[0] = $_POST['title2'];
$NewStuff[1] = $_POST['author2'];
$NewStuff[2] = $_POST['date2'];
$NewStuff[3] = $_POST['content2'];
[/code]
Obviously, I call this $NewStuff.

I want to add $NewStuff to the end of $exploded, so I do this:
[code]array_unshift($exploded, $NewStuff);[/code]

I believe that this gives me an array like this:
[code] 0
Title0
Date0
Author0
Content0
1
Title1
Date1
Author1
Content1
NewStuff
Title2
Date2
Author2
Content2[/code]

Now, I want to implode all of this, so I get a file like this:
[code] Title0
Date0
Author0
Content0

Title1
Date1
Author1
Content1

Title2
Date2
Author2
Content2[/code]


So, I implode:
[code] // implode the parts, then implode them all together
foreach ($exploded as $index => $line) // Go through each part of $exploded
{
[red] $implodedFirst[$index] = implode("
", $exploded[$line]); // Implodes each part[/red]
// depending on single new line.
}
$imploded = implode("

", $implodedFirst); // Implode by 2x new line...[/code]

But, much to my delight, I get this beautiful error.
[code]
Warning: Illegal offset type in /home/exirptco/public_html/news/mod.php on line 209

Warning: implode(): Bad arguments. in /home/exirptco/public_html/news/mod.php on line 209

Warning: Illegal offset type in /home/exirptco/public_html/news/mod.php on line 209

Warning: implode(): Bad arguments. in /home/exirptco/public_html/news/mod.php on line 209

Warning: Illegal offset type in /home/exirptco/public_html/news/mod.php on line 209

Warning: implode(): Bad arguments. in /home/exirptco/public_html/news/mod.php on line 209[/code]
Line 209 is the red one in the implosion code. Thanks to anyone who can help me fix this problem.

Comments

  • NeakNeak Posts: 87Member
    Meh. I know it's a lot to read and stuff but I [red]REALLY[/red] need help with this problem! I've been working on this problem alone for over a month now and I've come to the decision that I CAN'T solve it!
    Please help!
  • ManningManning Posts: 1,621Member
    : I believe that this gives me an array like this:
    : [code] 0
    : Title0
    : Date0
    : Author0
    : Content0
    : 1
    : Title1
    : Date1
    : Author1
    : Content1
    : NewStuff
    : Title2
    : Date2
    : Author2
    : Content2[/code]

    array_unshift() places the new element at the start, so that is definitely not what you end up with. To find out what the new array looks like, use print_r(). Then you won't have to guess what it looks like anymore.

    If the above doesn't reveal anything, use print_r() again in your loop to see what kind of data you're working with.

    : So, I implode:
    : [code] // implode the parts, then implode them all together
    : foreach ($exploded as $index => $line) // Go through each part of $exploded
    : {
    : [blue]echo "$index = $index
    $line = " . print_r($line);[/blue]
    : [red]// $implodedFirst[$index] = implode("
    ", $exploded[$line]); // Implodes each part[/red]
    : [green] $implodedFirst[] = implode("
    ", $exploded[$index]); // I think this is what you need[/green]
    : // depending on single new line.
    : }
    : $imploded = implode("

    ", $implodedFirst); // Implode by 2x new line...[/code]

  • NeakNeak Posts: 87Member
    : : I believe that this gives me an array like this:
    : : [code] 0
    : : Title0
    : : Date0
    : : Author0
    : : Content0
    : : 1
    : : Title1
    : : Date1
    : : Author1
    : : Content1
    : : NewStuff
    : : Title2
    : : Date2
    : : Author2
    : : Content2[/code]
    :
    : array_unshift() places the new element at the start, so that is definitely not what you end up with. To find out what the new array looks like, use print_r(). Then you won't have to guess what it looks like anymore.

    Heh. It was a typo. I meant to have the new information added to the front of the array, as the data is displayed on the viewing page in the order that it is in the array. Sorry about that.

    : : So, I implode:
    : : [code] // implode the parts, then implode them all together
    : : foreach ($exploded as $index => $line) // Go through each part of $exploded
    : : {
    : : [blue]echo "$index = $index
    $line = " . print_r($line);[/blue]
    : : [red]// $implodedFirst[$index] = implode("
    ", $exploded[$line]); // Implodes each part[/red]
    : : [green] $implodedFirst[] = implode("
    ", $exploded[$index]); // I think this is what you need[/green]
    : : // depending on single new line.
    : : }
    : : $imploded = implode("

    ", $implodedFirst); // Implode by 2x new line...[/code]
    :

    I'll try this. I guess I didn't fully understand the syntax of a foreach loop before. Thanks for the help.
  • NeakNeak Posts: 87Member
    [b][red]This message was edited by Neak at 2004-6-23 20:39:53[/red][/b][hr]
    It works. I had one other problem which was that I was using r+ instead of w+, but other than that it works perfectly. Thanks a lot!


  • ManningManning Posts: 1,621Member
    : [b][red]This message was edited by Neak at 2004-6-23 20:39:53[/red][/b][hr]
    : It works. I had one other problem which was that I was using r+ instead of w+, but other than that it works perfectly. Thanks a lot!

    No problem. Your foreach could probably be shrunk down a little bit more actually. The way you are using it now is as if $exploded was an associative array, but it really isn't. So you can drop the " => $line" bit, which leaves you with:

    foreach ($exploded as $index) {
    }

    But there is always "if it ain't broke, don't fix it", so it wouldn't hurt anything to leave it as is :)
  • NeakNeak Posts: 87Member
    : No problem. Your foreach could probably be shrunk down a little bit more actually. The way you are using it now is as if $exploded was an associative array, but it really isn't. So you can drop the " => $line" bit, which leaves you with:
    :
    : foreach ($exploded as $index) {
    : }
    :
    : But there is always "if it ain't broke, don't fix it", so it wouldn't hurt anything to leave it as is :)
    :

    Already did. I read up on foreach loops after reading your advice.
Sign In or Register to comment.