Jump to content

Array: same key, multiple values


rednax

Recommended Posts

Hello,

 

I've written a curl script that needs the following POST string...

 

$Fields = array('relist' => '0', 'tgp[1][]' => '1', 'tgp[1][]' => '10', 'tgp[1][]' => '56', 'gallery_url' => $galleryURL, 'gallery_title' => $galleryTitle, 'newthumb' => $newLocation, 'save' => 'Insert');

 

The issue lies with the tgp[1][] key... I need three different values, but with this system only the last value is used.

 

I've tried this:

$catName = "tgp[1][]";
$catNameArray = array($cat_1, $cat_2, $cat_3); 

$Fields = array('relist' => '0', $catName => $catNameArray, 'gallery_url' => $galleryURL, 'gallery_title' => $galleryTitle, 'newthumb' => $newLocation, 'save' => 'Insert');

 

But that doesn't work... any suggestions?

Link to comment
Share on other sites

You can do something like this:

 

curl_setopt($ch, CURLOPT_POSTFIELDS,  array('cat1'=>"washington",
				'cat2'=>"jefferson",
				'cat3'=>"adams",
				'cat4'=>"lincoln",
				'cat5'=>"roosevelt",
				'cat6'=>"clinton",
				'cat7'=>"bush",
				'cat8'=>"reagan"));

 

but if you are trying to send an array as a post var, I don't think that is possible.

Link to comment
Share on other sites

the problem is that each of those keys are different... mine are the same.

 

curl_setopt($ch, CURLOPT_POSTFIELDS,  array('cat[1][]'=>"washington",

'cat[1][]'=>"jefferson",

'cat[1][]'=>"adams"));

 

Is there another way to send this with curl?

Link to comment
Share on other sites

Two options: You either must specify the actual indexes of the arrays like so:

 


curl_setopt($ch, CURLOPT_POSTFIELDS,  array('cat[1][0]'=>"washington",
               'cat[1][1]'=>"jefferson",
               'cat[1][2]'=>"adams"));

 

This is necessary because you cannot have two elements of an array with the same key. By specifying them as you were, you were just overwriting the value each time.

 

Your second option is to specify the postfields as a string like this:

 

curl_setopt($ch, CURLOPT_POSTFIELDS,'cat[1][]=washington&cat[1][]=jefferson&cat[1][]=adams');

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.