0

I tried to push a new array value to JSON

$var=array("code"=>"100");
$sql = SELECT order as orderID, pub as orderCode from cart
while ($row = mysql_fetch_assoc ($sql) {
$var[] = $row;
}
echo '{"status":'. json_encode($var).'}';

I want push this string to the JSON array above

$string = array("total"=>"3000");

and my script displays like this:

{  
"status":{  
   "code":"100",
      "0":{  
            "total":"3000"
          },
      "1":{  
            "orderID":"16",
            "orderCode":"14290290685322"
          },
      "2":{  
            "total":"3000"
          }
      }
   }

and I want var total inside like this:

**////////blablabla
   "1":{ 
      "orderID":"16",
      "orderCode":"14290290685322",
      "total":"3000" 
      }
*///blablba

1 Answer 1

1

Add $var[] = array("total"=>"3000"); just before you encode to JSON

$var=array("code"=>"100");
$sql = SELECT order as orderID, pub as orderCode from cart
while ($row = mysql_fetch_assoc ($sql) {
  $row['total'] = '3000' ;
  $var[] = $row;
}
echo '{"status":'. json_encode($var).'}';
Sign up to request clarification or add additional context in comments.

5 Comments

where i place $string = array("total"=>"3000");
is okay, i'm need to push $string new element to JSON xD
it display error like this Fatal error: Cannot use string offset as an array
that same exactly what i did tehehe but still getting display total separated from JSON ROW
nah, absolutely right (y). thank you very much for help me xD

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.