Take the 2-minute tour ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

I have a javascript array : disharray = ([aa,11,],[bb,22])

I send this to php as a json object using - var jsoncvrt = JSON.stringify(disharray);

How do i extract the value of the nested arrays so that i can access values like : $a = aa and $b = 11?

I use the below code but get the output as
aa11
bb22

Please note, my server uses php 5.2

$data = json_decode(stripcslashes($_POST['strings']));
foreach ($data as $d => $v) {
    foreach ($v as $v1 => $value) {
        echo $value;
    }
}
share|improve this question
    
can you post a var_dump of $data ? –  sanketh yesterday
    
array(4) { [0]=> array(2) { [0]=> string(2) "aa" [1]=> string(2) "11" } [1]=> array(2) { [0]=> string(2) "bb" [1]=> string(2) "22" } [2]=> array(2) { [0]=> string(2) "cc" [1]=> string(2) "33" } [3]=> array(2) { [0]=> string(2) "dd" [1]=> string(2) "44" }} –  Ash yesterday
    
for what all this code? $d=json_decode($_POST['str'],1); $d[0][1]==11, $d[1][0]=='bb' –  vp_arth yesterday
add comment

1 Answer

up vote 0 down vote accepted

Your code is fine. Just add this at the top of the code

  $values = array();

Now change the inner foreach loop to

 if( sizeof($v) == 2 ){
    $values[$v[0]] = intval($v[1]);
 } 

Now to access, say the value corresponding to 'aa' just use $values['aa']

You can insert it into a table using the following code

  $con = mysqli_connect(HOSTNAME, USERNAME, PASSWORD, DBNAME);
  $query = "INSERT INTO tablename (key, value) VALUES(?, ?);";
  $stmt = $con->prepare($query); 
  if( $stmt ){
       foreach ($values as $key => $value){
            $stmt->bind_param("sd", $key, $value);
            $stmt->execute();
       }
       $stmt->close();  
  }
  $con->close(); 

In the $query variable, the '?' stands for wild card character that can take any value and it is set by calling bind_param() funtion. In the bind_param function, the 's' stands for string and the 'd' stands for integer data type. This is the right way to execute database queries as they void the possibility of SQL Injections.

share|improve this answer
    
Thanks @sanketh, this works as well. Please read the comment i added on the above answer. Can you help me with that? –  Ash yesterday
    
To answer your question, yes, it is possible to store these key value pairs in a database. There are apis in php inorder to connect to databases and run queries etc. The quite popular ones are mysqli and PDO for mysql engine.Read about them. I can't do much without you knowing something about them. –  sanketh yesterday
    
I'm using simple sql statement - INSERT INTO child_dish (dish_name,dish_price,F_id) VALUES ('$v','$v[0]', '$extra')" How do i get these two values using your code. Sorry i am learning PHP :) –  Ash yesterday
    
Updated answer @Ash –  sanketh yesterday
    
Thanks a ton @sanketh . Your explanation was very precise :) and ur solution is perfect –  Ash yesterday
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.