Jump to content

implode function with foreach !!!


yami007

Recommended Posts

I thought this would bring me a cool result and be done but

<?php
foreach($_GET as $keyname => $value) {

   echo implode(" , ", "$keyname=$value");

}
?>

 

this what it displays :

Warning: implode() [function.implode]: Bad arguments. in C:\AppServ\www\test\link.php on line 17

i know why it dosnt work, but dont know how to get it to work!!

Link to comment
Share on other sites

care to explain what it is you are trying to do?

 

 

well, i'm trying to get the query strings from the url and produce a new link.

for example, the actual link is index.php?action=news&article=1

with my code i want to output something like this : action=news&article=1

but it's not working !!

 

Link to comment
Share on other sites

The implode function "implodes" an array into a string seperated by the "glue" given in the first argurment...

 

The second argument required for the implode function is the array of values you wish to implode.

 

You are getting the error because you are passing a string...

Also; why is your first arguement ' , ' when your desired result is seperated by ampersands?

 

$strQuery = '';
foreach ($_GET as $k => $v):
   $strQuery .= $k . '=' . $v . '&';
endforeach;

$strQuery = substr($strQuery, -1);

 

That should give the desired result.

//EDIT

If you wish to have the leading questionmark change

 

$strQuery = '';
//to
$strQuery = '?';

 

 

Link to comment
Share on other sites

It shouldn't do that. Are you sure you're not echoing out the first variable previously? Or have it twice in your url?..

 

Yes i am sure, i tried your code independtly on a file like this :

<?php
foreach($_GET as $key => $val):
$vars[] = "$key=$val";
echo implode('&', $vars);
endforeach;
?>

with nothing else and poited the url to test.php?id=1&name=john

and it still multiplies the firs key.

Link to comment
Share on other sites

That's not my code.. You tried to add an unnecessary endforeach; to my code and put it in the wrong place which is what was making it not work.

 

<?php
foreach($_GET as $key => $val)
$vars[] = "$key=$val";
echo implode('&', $vars);

Link to comment
Share on other sites

That's not my code.. You tried to add an unnecessary endforeach; to my code and put it in the wrong place which is what was making it not work.

 

<?php
foreach($_GET as $key => $val)
$vars[] = "$key=$val";
echo implode('&', $vars);

 

Oh thanks, it worked ;)

thanks everyone : )

Link to comment
Share on other sites

lol never thought,

 

echo $_SERVER['QUERY_STRING'];

 

haha yeah i just remembered that, was fixin' to post that just now till I saw your post

yes i know it works with server global lol

but what drove me to all this trouble is that i wanted to exclude a key

for example :

i am on the page articles.php?id=1245&title=new+beginning&lang=en

here i only want to get id=1245&title=new+beginning and add lang=en or lang=sp statically os i can change the language.

that's the ponit : )

i really appreciate your help, guys : )

Link to comment
Share on other sites

lol never thought,

 

echo $_SERVER['QUERY_STRING'];

Lol, I forgot about that too.. I was pretty sure there was something like that, I went to test it and I must've typed it in wrong. :facepalm:

it really suprised me the way u did with foreach, i never knew that, still newbie lol

Link to comment
Share on other sites

IF the lang value is always a 2 letter country code you could use

 

echo substr( $_SERVER['QUERY_STRING'], 0, (strlen($_SERVER['QUERY_STRING']) -  );

 

//edit

That code also relies on the lang=?? being the last part of the query string...

Link to comment
Share on other sites

IF the lang value is always a 2 letter country code you could use

 

echo substr( $_SERVER['QUERY_STRING'], 0, (strlen($_SERVER['QUERY_STRING']) -  );

Yeah i'm actually working with just 3 languages, en, fr and ar lol

i will work with this code better

thanks

 

//edit

That code also relies on the lang=?? being the last part of the query string...

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.