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
https://forums.phpfreaks.com/topic/118885-array-same-key-multiple-values/
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.

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

Archived

This topic is now archived and is closed to further replies.

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