-2

I an trying build an extension, taking information from php code to an opencart controller

So it's php too but reading the string give me some error

foreach($fields as $key=>$val) {
    $string .= "$key=$val&";
}

I need a way to write this for opencart 2.3.0.2

Fruchtzwerg
  • 10,999
  • 12
  • 40
  • 49
ido beker
  • 29
  • 3

1 Answers1

0

Assuming you are trying to build a query string; try the following:

<?php
$fields = array('key1' => 'value1', 'key2' => 'value2', 'key3' => 'value3');

$count = count($fields);
$i=0;
$string='';

foreach($fields as $key=>$val) {
    $i++;
    if($i < $count) {
    $string .= $key . '=' . $val . '&amp';
    } else {
    $string .= $key . '=' . $val; // the query string ends without '&'
    }
}

echo $string;
// outputs: key1=value1&key2=value2&key3=value3

You can obviously also use: http_build_query() — to generate a URL-encoded query string (http://php.net/manual/en/function.http-build-query.php)

lovelace
  • 1,195
  • 1
  • 7
  • 10