Search code examples
phpforeacharray-pushphp-5.2

PHP 5.2 array_push inserts a null value


Hi I'm encountering a weird problem iterating and pushing a value in an array(for better viewing i use json data). Below is the data i'm using in my foreach for the data below. I actually don't know what the problem is but i'm using php 5.2

[
{
account_grp: "SOLD",
sold_to: "1000005071",
sold_to_name: "MARIA LUZ MANALILI",
ship_to: "1000005071",
ship_to_name: "MARIA LUZ MANALILI",
division: "20",
division_desc: "Pharma Sales",
salesman: "E000112086",
salesman_name: null,
doc_type: "DA",
posting_date: "20170707",
clearing: "2100030778",
doc_no: "1400009660",
assignment: "5110074314",
ref_no: "RC - RECLASS",
amt_w_vat: "6496.62",
vat: "779.5944",
amt_net_vat: "5717.0256",
cust_group2_desc: "MEAD JOHNSON",
cust_group_desc: "Reg Accnt-Non-Chain",
gl_account: "0000123001",
gl_account_desc: "TRADE RECEIVABLE",
check_date: null,
check_details: "RC - DAIF",
remarks: "RC - DAIF",
posting_key: "09",
line_item: "001",
fiscal_year: "2017",
tax_class: "1",
doc_no2: "1400009660"
},
{
account_grp: "SOLD",
sold_to: "1000004711",
sold_to_name: "PAZ PHARMACY",
ship_to: "1000004711",
ship_to_name: "PAZ PHARMACY",
division: "20",
division_desc: "Pharma Sales",
salesman: "E000071970",
salesman_name: "IGLORIA, JOAN DELICANO",
doc_type: "DA",
posting_date: "20170707",
clearing: "1700004713",
doc_no: "1400009658",
assignment: "5120008912",
ref_no: "RC - RECLASS",
amt_w_vat: "72783.62",
vat: "8734.0344",
amt_net_vat: "64049.5856",
cust_group2_desc: "INDEPENDENT",
cust_group_desc: "Reg Accnt-Non-Chain",
gl_account: "0000123001",
gl_account_desc: "TRADE RECEIVABLE",
check_date: null,
check_details: "RC - Tech Reason",
remarks: "RC - Tech Reason",
posting_key: "09",
line_item: "001",
fiscal_year: "2017",
tax_class: "1",
doc_no2: "1400009658"
},
{
account_grp: "SOLD",
sold_to: "1000003609",
sold_to_name: "DR. ANDEYLEF JOY S. CABUTIHAN",
ship_to: "2000003312",
ship_to_name: "DR. ANDELEYLEF JOY S. CABUTIHAN",
division: "20",
division_desc: "Pharma Sales",
salesman: "E000112086",
salesman_name: null,
doc_type: "DA",
posting_date: "20170714",
clearing: "2100031395",
doc_no: "1400009796",
assignment: "5110077391",
ref_no: "RC - RECLASS",
amt_w_vat: "11577.10",
vat: "1389.2520",
amt_net_vat: "10187.8480",
cust_group2_desc: "MEAD JOHNSON",
cust_group_desc: "Reg Accnt-Non-Chain",
gl_account: "0000123001",
gl_account_desc: "TRADE RECEIVABLE",
check_date: null,
check_details: "RC - DAIF",
remarks: "RC - DAIF",
posting_key: "09",
line_item: "001",
fiscal_year: "2017",
tax_class: "1",
doc_no2: "1400009796"
},
{
account_grp: "SOLD",
sold_to: "1000005447",
sold_to_name: null,
ship_to: "2000004135",
ship_to_name: null,
division: "20",
division_desc: "Pharma Sales",
salesman: "E000112086",
salesman_name: null,
doc_type: "DA",
posting_date: "20170720",
clearing: "2100031892",
doc_no: "1400010004",
assignment: "5130007369",
ref_no: "RC - RECLASS",
amt_w_vat: "11581.83",
vat: "1389.8196",
amt_net_vat: "10192.0104",
cust_group2_desc: "MEAD JOHNSON",
cust_group_desc: "Reg Accnt-Non-Chain",
gl_account: "0000123001",
gl_account_desc: "TRADE RECEIVABLE",
check_date: null,
check_details: "RC - DAIF",
remarks: "RC - DAIF",
posting_key: "09",
line_item: "001",
fiscal_year: "2017",
tax_class: "1",
doc_no2: "1400010004"
}
] 

here's my code

public function create($aData)
{

    $aValues = array();


    foreach ($aData as $key => $aReport) 
    {


        $sData = implode(',', $aReport);


        array_push($aValues, "(". $sData. ")");

    }
    echo json_encode($aValues);die();


}

and when i echo the $aValues here's the output

[
null,
"(SOLD,1000004711,PAZ PHARMACY,1000004711,PAZ PHARMACY,20,Pharma Sales,E000071970,IGLORIA, JOAN DELICANO,DA,20170707,1700004713,1400009658,5120008912,RC - RECLASS,72783.62,8734.0344,64049.5856,INDEPENDENT,Reg Accnt-Non-Chain,0000123001,TRADE RECEIVABLE,,RC - Tech Reason,RC - Tech Reason,09,001,2017,1,1400009658)",
null,
null
]

Solution

  • They are displaying null because json_encode is failing on the data (not properly formatted?). Make sure you are passing in properly formatted data to be encoded.

    http://php.net/manual/en/function.json-encode.php