0

I am trying to access the values of this array in PHP. It's a multidimensional array. I need to get values from the array and insert it in the DB. Inserting is the second part of the problem. First parts is getting the values from it.

JSON -

{
"itempicture":[
  {
     "status":"3"
  },
  {
     "ItemCode":"001",
     "ItemImage":"image1",
     "ItemCategory":"shirt",
     "ShowOnPOS":"Yes",
     "LastModifiedOn":"2018-06-02 11:53:57"
  },
  {
     "ItemCode":"002",
     "ItemImage":"image2",
     "ItemCategory":"shirt",
     "ShowOnPOS":"Yes",
     "LastModifiedOn":"2018-06-02 11:53:57"
  }]}

Here the "itempicture" is the table name and all the keys, i.e 'itemcode', 'itemimage, etc are the SQL columns'".

I need to get the values of the SQL columns and insert it into the DB.

So far i have tried this -

 $data = file_get_contents($url); 
 $json_array = (array)(json_decode($data));

 print_r($json_array);


foreach($user->itempicture as $mydata)

{
     echo $mydata->itempicture . "\n";
     foreach($mydata->itempicture as $values)
     {
          echo $values->itempicture . "\n";
     }
}  

Using MYSQL in Object-oriented method to insert it in DB, with a simple query like "INSERT INTO table_name VALUES (value1, value2, value3, ...)"

So table name will be the "itempicture" present in the array and values will be the values of the keys in the array.

2 Answers 2

1

This may be of help. Instead of looping through the properties with a foreach and printing, you'll want to use them to build an array to use as parameters for a prepared query, or build the query directly. But this shows you how to access those properties -

<?php

$j='{
"itempicture":[
  {
     "status":"3"
  },
  {
     "ItemCode":"001",
     "ItemImage":"image1",
     "ItemCategory":"shirt",
     "ShowOnPOS":"Yes",
     "LastModifiedOn":"2018-06-02 11:53:57"
  },
  {
     "ItemCode":"002",
     "ItemImage":"image2",
     "ItemCategory":"shirt",
     "ShowOnPOS":"Yes",
     "LastModifiedOn":"2018-06-02 11:53:57"
  }]}';


  $jo=json_decode($j);

  print("status ".$jo->itempicture[0]->status."\n");
  for($i=1;$i<count($jo->itempicture);$i++){
      foreach($jo->itempicture[$i] as $prop=>$val){
          print($prop." = ".$val."\n");
      }
  }

 ?>
Sign up to request clarification or add additional context in comments.

2 Comments

It works and i think it will work for all the json which has different columns also. Can you tell me how can i get the table name? i.e itempicture.
@Abhijeetc50 looping the top level json object ($jo in my example) with a foreach you can catch it. but setting a var on it would assume that it only cycles through the loop once or force it to only cycle once via break. Try this - . foreach($jo as $prop=>$val){ $table_name=$prop;break; }
0

There are some issues I have seen in your code:

  • You cast the result of json_decode($data) to an array, an then later you try to use property-access to access the "itempicture" property. I have removed this cast.
  • In the first foreach use the variable $user, I guess you meant to use $json_array.
  • The inner loop is not really used, you can access the properties directly

After having fixed this issues the code looks like that and works with your given JSON. I have just echo-ed the values since you have not told us which DB-abstraction you use, but you can use the values to populate the database.

$data = file_get_contents($url);
$json_array = (json_decode($data));

print_r($json_array);

foreach($json_array->itempicture as $mydata)
{
    if (!property_exists($mydata, 'ItemCode')) {
        // Ignore entries which are invalid (have no ItemCode property)
        continue;
    }
    echo implode(' - ', [
        $mydata->ItemCode,
        $mydata->ItemImage,
        $mydata->ItemCategory,
        $mydata->ShowOnPOS,
        $mydata->LastModifiedOn,
    ]), PHP_EOL;
}

2 Comments

It works! Thanx a ton. Updated the question for the SQL part.
Instead of fetching it as objects, can we apply loop inside the implode function? So that we don't have to define the columns and the code can be used for any file which has different columns also.

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.