title: Get the value of an object and assign with index using for loop PHP
body: <p>I have a data passed from javascript and I want to assign into PHP variables with an index.</p>
  
  <p>The result of <code>var_dump($data)</code> is this:</p>
  
  <pre><code>object(stdClass)#2 (5) {
    ["length"]=&gt;
    int(2)
    ["item_id0"]=&gt;
    string(1) "1"
    ["quantity0"]=&gt;
    int(1)
    ["item_id1"]=&gt;
    string(1) "1"
    ["quantity1"]=&gt;
    int(2)
  }
  </code></pre>
  
  <p><strong>PHP Code but not working</strong></p>
  
  <p>The error is in line of <code>$item_id[$i] = $data-&gt;item_id[$i]; $quantity[$i] = $data-&gt;quantity[$i];</code></p>
  
  <p>Error output something like this: <code>Undefined property: stdClass::$item_id</code></p>
  
  <p>It seems that in <code>$data-&gt;item_id[$i]</code> part, the <code>item_id[$i]</code> is not equal as <code>item_id0</code>, which I am expecting to be.</p>
  
  <pre><code>$data = json_decode(file_get_contents("php://input"));
  $length = $data-&gt;length;
  
  for($i=0; $i&lt;=$length; $i++){
  
  $item_id[$i] = $data-&gt;item_id[$i];
  $quantity[$i] = $data-&gt;quantity[$i];
  
  $sql = "INSERT INTO transaction_details(item_id, quantity) VALUES(:item_id, :quantity)";
  
  try {
    $stmt = $db-&gt;prepare($sql);
    $stmt-&gt;bindParam(':item_id', $item_id[$i]);
    $stmt-&gt;bindParam(':quantity', $quantity[$i]);
  
    $stmt-&gt;execute();
  
    echo '{"notice": {"text": "Transaction Added"}';
  }
  </code></pre>