0

This is the post fields of a CURL request. I need to add multiple county ids to this CURLOPT_POSTFIELDS: -

CURLOPT_POSTFIELDS => '{
  "fields": [
    {
      "field_type": "countries",
      "field_value":[

        {
            "country_id": '.$id.' ,
            "match_type": "exact"
        }
        ]
    }
  ]
}'

I have an array of country ids and need to create this block for all: -

{
                "country_id": '.$id.' ,
                "match_type": "exact"
            }

so that If I have 5 country id's it should look like

CURLOPT_POSTFIELDS => '{
      "fields": [
        {
          "field_type": "countries",
          "field_value":[

            {
                "country_id": '.$id[0].' ,
                "match_type": "exact"
            },
            {
                "country_id": '.$id[1].' ,
                "match_type": "exact"
            },
            {
                "country_id": '.$id[2].' ,
                "match_type": "exact"
            },
            {
                "country_id": '.$id[3].' ,
                "match_type": "exact"
            },
            {
                "country_id": '.$id[4].' ,
                "match_type": "exact"
            }
            ]
        }
      ]
    }'

The number of countries may vary each time. So I need to use a for loop inside this to do it dynamically. Thanks in Advance

3
  • 1
    What is the the problem then? Commented Nov 16, 2018 at 12:14
  • 1
    he wants to do it programatically i guess.. So with a foreach loop Commented Nov 16, 2018 at 12:15
  • The number of countries may vary each time.So I need to use a for loop inside this to do it dynamically Commented Nov 16, 2018 at 12:16

2 Answers 2

3

It is extremely ill-advised to try to manually craft a json string in a loop. You should be building a valid array then converting it to json when you are done.

To build your desired result array, just push new data into the appropriate subarray.

Code: (Demo)

$country_ids = range(1,5);
$result['fields'][0]['field_type'] = "countries";
foreach ($country_ids as $id) {
    $result['fields'][0]['field_value'][] = ["country_id" => $id, "match_type" => "exact"];
}

var_export($result);
echo "\n---\n";
echo json_encode($result, JSON_PRETTY_PRINT);  // this resembles your posted data

Output (as both array and json):

array (
  'fields' => 
  array (
    0 => 
    array (
      'field_type' => 'countries',
      'field_value' => 
      array (
        0 => 
        array (
          'country_id' => 1,
          'match_type' => 'exact',
        ),
        1 => 
        array (
          'country_id' => 2,
          'match_type' => 'exact',
        ),
        2 => 
        array (
          'country_id' => 3,
          'match_type' => 'exact',
        ),
        3 => 
        array (
          'country_id' => 4,
          'match_type' => 'exact',
        ),
        4 => 
        array (
          'country_id' => 5,
          'match_type' => 'exact',
        ),
      ),
    ),
  ),
)
---
{
    "fields": [
        {
            "field_type": "countries",
            "field_value": [
                {
                    "country_id": 1,
                    "match_type": "exact"
                },
                {
                    "country_id": 2,
                    "match_type": "exact"
                },
                {
                    "country_id": 3,
                    "match_type": "exact"
                },
                {
                    "country_id": 4,
                    "match_type": "exact"
                },
                {
                    "country_id": 5,
                    "match_type": "exact"
                }
            ]
        }
    ]
}
Sign up to request clarification or add additional context in comments.

Comments

1

You could do something like this.

$id = array(1,2,3,4,5,6); // Your IDS
$numberOfIds = count($id);  //Total number of ids (no comma after last entry)  
$finalString = ""; // The final string for output.
foreach($id as $key => $value){

    $finalString .= '{
            "country_id": '.$value.',
            "match_type": "exact"
        }';
    if($key < $numberOfIds-1){
       $finalString .= ","; 
    }
}

$curlStuff = '{
  "fields": [
    {
      "field_type": "countries",
      "field_value":[
            '.$finalString.'

        ]
    }
  ]
}';
var_dump($curlStuff);

Will give

string(681) "{
  "fields": [
    {
      "field_type": "countries",
      "field_value":[
            {
            "country_id": 1,
            "match_type": "exact"
        },{
            "country_id": 2,
            "match_type": "exact"
        },{
            "country_id": 3,
            "match_type": "exact"
        },{
            "country_id": 4,
            "match_type": "exact"
        },{
            "country_id": 5,
            "match_type": "exact"
        },{
            "country_id": 6,
            "match_type": "exact"
        }

        ]
    }
  ]
}"

So you could run

CURLOPT_POSTFIELDS => $curlStuff;

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.