Jump to content


 


Register a free account to unlock additional features at BleepingComputer.com
Welcome to BleepingComputer, a free community where people like yourself come together to discuss and learn how to use their computers. Using the site is easy and fun. As a guest, you can browse and view the various discussions in the forums, but can not create a new topic or reply to an existing one unless you are logged in. Other benefits of registering an account are subscribing to topics and forums, creating a blog, and having no ads shown anywhere on the site.


Click here to Register a free account now! or read our Welcome Guide to learn how to use this site.

Photo

foreach loop


  • Please log in to reply
8 replies to this topic

#1 KamakaZ

KamakaZ

  • Members
  • 739 posts
  • OFFLINE
  •  
  • Gender:Male
  • Location:Victoria
  • Local time:08:39 AM

Posted 10 May 2013 - 01:11 AM

I have a delphi program that is POSTing a PHP page the following JSON

 

$_POST['JSONValues'] =

{
 "DBID":"480",
 "COUNT": 2,
 "ITEMS":
  [
   {
    "ID":"1",
    "NAME":"TEST1",
    "TYPE":"CURRENCY",
    "VALUE":"2640",
    "RED":"200",
    "ORANGE":"250",
    "GREEN":"500",
    "LAST_RUN":"1368201941"
   },
   {
    "ID":"2",
    "NAME":"TEST2",
    "TYPE":"CURRENCY",
    "VALUE":"1957011.02",
    "RED":"15000",
    "ORANGE":"20000",
    "GREEN":"500000",
    "LAST_RUN":"1368201941"
   }
  ]
}

 

I have tried all sorts of loops 

$json = json_decode($_POST[JSONValues]);
 
foreach ($json as $key) 
 
foreach ($json as $key => $value)

 

Nothing seems to want to work... :(

 

Could someone please point me in the right direction?

 


There's no place like 127.0.0.1
There are 10 types of people in the world, those that can read binary, and those who can't.


BC AdBot (Login to Remove)

 


#2 groovicus

groovicus

  • Security Colleague
  • 9,963 posts
  • OFFLINE
  •  
  • Gender:Male
  • Location:Centerville, SD
  • Local time:03:39 PM

Posted 10 May 2013 - 08:43 AM

json_decode is sometimes weird. Are you actually getting any output if you dump the json object? Have you looked at the API for any hints? Are you getting error messages?

 

You have been around long enough to know that "Nothing seems to want to work" tells us nothing helpful.



#3 KamakaZ

KamakaZ
  • Topic Starter

  • Members
  • 739 posts
  • OFFLINE
  •  
  • Gender:Male
  • Location:Victoria
  • Local time:08:39 AM

Posted 11 May 2013 - 03:22 AM

Sorry, let me explain a bit more.

My Delphi program builds up a json string (above) and the uses and Indy component to post the string to my PHP page. I have the print_r(json_decode($_POST['JSONValues'])) but can't figure out how to get the data from the array.

I would post the output but I'm on my phone.

There is no documentation of the 'API' since currently it's not really an api.

There's no place like 127.0.0.1
There are 10 types of people in the world, those that can read binary, and those who can't.


#4 groovicus

groovicus

  • Security Colleague
  • 9,963 posts
  • OFFLINE
  •  
  • Gender:Male
  • Location:Centerville, SD
  • Local time:03:39 PM

Posted 11 May 2013 - 01:13 PM

If the json example you posted above is the actual return data, then you are not returning an array.  In PHP, arrays are represented inside parenthesis, not curly braces. Nor does PHP use square braces to denote arrays. You can't access the values like an array, because you don't have a valid array.

 

Just a hunch.



#5 KamakaZ

KamakaZ
  • Topic Starter

  • Members
  • 739 posts
  • OFFLINE
  •  
  • Gender:Male
  • Location:Victoria
  • Local time:08:39 AM

Posted 11 May 2013 - 06:23 PM

No that string wasn't an array the is the JSON string, I was getting an array from json_decode of that string. I will var_dump the json_decode once I get near a computer

There's no place like 127.0.0.1
There are 10 types of people in the world, those that can read binary, and those who can't.


#6 KamakaZ

KamakaZ
  • Topic Starter

  • Members
  • 739 posts
  • OFFLINE
  •  
  • Gender:Male
  • Location:Victoria
  • Local time:08:39 AM

Posted 12 May 2013 - 08:56 PM

Hi Groovicus,

 

Here's a more detailed post...

 

Using this PHP code

<?php
 require_once('Database.php');
 $objDB = new Database;

 if (!empty($_POST)){
  if (array_key_exists('JSONValues', $_POST)){
   $arJSON = json_decode($_POST['JSONValues']);
   var_dump($arJSON);
  } else {
   echo "JSONValues not found in $_POST";
  }
 } else {
  echo "$_POST is empty";
 }
?>

 

Give me this array to work with

object(stdClass)#2 (3) {
  ["DBID"]=>
  string(3) "480"
  ["COUNT"]=>
  int(2)
  ["ITEMS"]=>
  array(2) {
    [0]=>
    object(stdClass)#3 (8) {
      ["ID"]=>
      string(1) "1"
      ["NAME"]=>
      string(5) "TEST1"
      ["TYPE"]=>
      string(8) "CURRENCY"
      ["VALUE"]=>
      string(1) "0"
      ["RED"]=>
      string(3) "200"
      ["ORANGE"]=>
      string(3) "250"
      ["GREEN"]=>
      string(3) "500"
      ["LAST_RUN"]=>
      string(10) "1368445787"
    }
    [1]=>
    object(stdClass)#4 (8) {
      ["ID"]=>
      string(1) "2"
      ["NAME"]=>
      string(5) "TEST2"
      ["TYPE"]=>
      string(8) "CURRENCY"
      ["VALUE"]=>
      string(10) "1957011.02"
      ["RED"]=>
      string(5) "15000"
      ["ORANGE"]=>
      string(5) "20000"
      ["GREEN"]=>
      string(6) "500000"
      ["LAST_RUN"]=>
      string(10) "1368445787"
    }
  }
}

 

I'm trying to loop this array and post the ID, NAME and VALUE into a database table but can't understand how to use the foreach loop to retrieve the values I'm looking for. I have had a look at the examples here (http://php.net/manual/en/control-structures.foreach.php) but can't figure it out.

 

I'm also a little confused between "json_decode(string)" and "json_decode(string, true)" in regards to returning an "associative" array? (http://php.net/manual/en/function.json-decode.php)


There's no place like 127.0.0.1
There are 10 types of people in the world, those that can read binary, and those who can't.


#7 KamakaZ

KamakaZ
  • Topic Starter

  • Members
  • 739 posts
  • OFFLINE
  •  
  • Gender:Male
  • Location:Victoria
  • Local time:08:39 AM

Posted 13 May 2013 - 01:57 AM

After a lot of trial and error (and var_dump's) I found the following to work for me.

 

Also for some silly reason the array's were not being reset by $array = array(), i had to unset() them.

 

<?php
 require_once('Database.php');
 $objDB = new Database;
 $arJSON = array();
 if (!empty($_POST)){
  if (array_key_exists('JSONValues', $_POST)){
   $arJSON = json_decode($_POST['JSONValues'], false);
  } else {
   echo "JSONValues not found in $_POST";
  }
 } else {
  echo "$_POST is empty";
 }
 if (!empty($arJSON)){
  $arItems = $arJSON->ITEMS;
  foreach ($arItems as $key => $item) {
   $objDB->_insert_keys[] = 'DBID';
   $objDB->_insert_values[] = $arJSON->DBID;
   foreach ($item as $fieldName => $fieldValue) {
    $objDB->_insert_keys[] = $fieldName;
    $objDB->_insert_values[] = $fieldValue;
   }
   
   $objDB->insert('health');
   $objDB->_insert_keys[] = array();
   $objDB->_insert_values[] = array();
   unset($objDB->_insert_keys);
   unset($objDB->_insert_values);
  }
  echo "Done.";
 }
 
?>

There's no place like 127.0.0.1
There are 10 types of people in the world, those that can read binary, and those who can't.


#8 SusanWhite

SusanWhite

  • Banned Spammer
  • 11 posts
  • OFFLINE
  •  
  • Gender:Female
  • Local time:03:09 AM

Posted 24 January 2014 - 12:07 PM

The issue is with the usage of json_decode

 

The correct syntax is json_decode($_POST['json'], true)

 

The use of true would create json object to associative array.



#9 KamakaZ

KamakaZ
  • Topic Starter

  • Members
  • 739 posts
  • OFFLINE
  •  
  • Gender:Male
  • Location:Victoria
  • Local time:08:39 AM

Posted 11 February 2014 - 07:54 PM

Thanks Susan, but this post is almost a year old...


There's no place like 127.0.0.1
There are 10 types of people in the world, those that can read binary, and those who can't.





0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users