0

I have to add new elements to a sub array

I used the code to set values in a single depth array but now i have to add values to sub array

foreach($result["data"]  as  $val)
{
   $qry = "SELECT count(tin) as tincnt , count(tout) as toutcnt FROM inout 
   WHERE NAME = '".$val["NAME"]."'";
   $prs = oci_parse($conn,$qry);
   $exec = oci_execute($prs);
   oci_define_by_name($prs,"tincnt",$tin);
   oci_define_by_name($prs,"toutcnt",$tout);
   if(!$exec)
   {
        $result['STATUS']="error";
        $result['MESSAGE']="errcd 3 : error fetching data";
   }
   else
   {
         oci_fetch($prs);
         $val->{'TOTAL_IN'} = $tin;
         $val->{'TOTAL_OUT'} = $tout;
   }
}


$val->{'TOTAL_IN'} = $tin; // gives error Attempt to assign property of non-object

Input

{
     "data": [
      {
       "NAME1": "Sukhwinder",
       "PHONE1": "9516152737",
       "ADDRESS": "Jalandhar"
      },
      {
       "NAME1": "Sapna",
       "PHONE1": "8787878787",
       "ADDRESS": "Jalandhar"
      }
    ],
    "STATUS": "SUCCESS",
    "CNT": "2",
    "HASDATA": true
}

desired output

{
    "data": [
        {
            "NAME1": "Sukhwinder",
            "PHONE1": "9516152737",
            "ADDRESS": "Jalandhar",
            "TOTAL_IN":5
            "TOTAL_OUT":4
        },
        {

            "NAME1": "Sapna",
            "PHONE1": "8787878787",
            "ADDRESS": "Jalandhar",
            "TOTAL_IN":4
            "TOTAL_OUT":4            
        }
    ],
    "STATUS": "SUCCESS",
    "CNT": "2",
    "HASDATA": true
}

2 Answers 2

1

In order to add values to the subarray ($val in your case), you have to pass it by reference. Try this:

foreach($result["data"]  as  &$val)
{
   $qry = "SELECT count(tin) as tincnt , count(tout) as toutcnt FROM inout 
   WHERE NAME = '".$val["NAME"]."'";
   $prs = oci_parse($conn,$qry);
   $exec = oci_execute($prs);
   oci_define_by_name($prs,"tincnt",$tin);
   oci_define_by_name($prs,"toutcnt",$tout);
   if(!$exec)
   {
        $result['STATUS']="error";
        $result['MESSAGE']="errcd 3 : error fetching data";
   }
   else
   {
         oci_fetch($prs);
         $val['TOTAL_IN'] = $tin;
         $val['TOTAL_OUT'] = $tout;
   }
}
Sign up to request clarification or add additional context in comments.

Comments

0

You can use a key variable or a reference in the foreach loop.

Key variable:

foreach($result["data"] as $key => $val) {
  //[...]
  else {
    oci_fetch($prs);
    $result['data'][$key]['TOTAL_IN'] = $tin;
    $result['data'][$key]['TOTAL_OUT'] = $tout;
  }
}

Pro: you don't have to worry about unsetting the reference &$val via unset($val); behind the foreach loop

Con: much longer than reference syntax

Reference:

foreach($result["data"] as &$val) {
  //[...]
  else {
    oci_fetch($prs);
    $val['TOTAL_IN'] = $tin;
    $val['TOTAL_OUT'] = $tout;
  }
}
// this is important, because $val exists outside of the foreach loop
unset($val);

Con: you have to worry about unsetting the reference &$val via unset($val); behind the foreach loop

Pro: much shorter than key variable syntax

Source: https://www.php.net/manual/de/control-structures.foreach.php

Comments

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.