0

I have these code to pull data from two different database.

And I need to eliminate the same row by 'staffid'.

So far I can eliminate the duplicate. But the output is not what I want it to be.

index.php

<?php

    // PART A
    require 'conn.php';
    $query = "SELECT STAFFID FROM TABLE";
    $result = oci_parse($conn, $query);
    oci_execute($result);
    while ($row = oci_fetch_array($result,OCI_NUM)){
        $arr[] = $row[0]; 
    } 


    // PART B
    $url="http://localhost/fetch_data.php";  
    $ch = curl_init(); 
    curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false); 
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); 
    curl_setopt($ch, CURLOPT_URL,$url); 
    $result=curl_exec($ch); 
    curl_close($ch); 


    foreach(json_decode($result) as $result2)
    {
        if(!in_array($result2[0],$arr))
        {
            $total = "".$result2[0].",".$result2[1].",".$result2[2]."<br/>"; 
            echo json_encode($total);
        }       
    }
?>

The output for far for the above code as below

"A001","BILLY","[email protected]"
"A002","JOHN","[email protected]"
"A003","SALLY","[email protected]"

fetch.php

<?php
    include_once('conn.php');

    $arr        = array();
    $query      = "SELECT * FROM TABLE "; 
    $stmt       = sqlsrv_prepare($conn, $query);
    $result     = sqlsrv_execute($stmt);
    $row        = sqlsrv_fetch_array($stmt);

    $array  = array();
    while( $self = sqlsrv_fetch_array( $stmt)) { 

        $array2 = array();
        $array2[]= $self['ID'];  
        $array2[]= $self['NAME'];  
        $array2[]= $self['EMAIL'];  

        $array[] =  $array2;
    }
    echo json_encode($array);
?>

My goal is to change it into JSON as below. I'm missing the bracket and the coma.

[
    ["A001","BILLY","[email protected]"],
    ["A002","JOHN","[email protected]"],
    ["A003","SALLY","[email protected]"]
]

Appreciate if someone can help me to fix this issue. Thanks.

3
  • Yes. I want the output to be like [ ["A001","BILLY","[email protected]"], ["A002","JOHN","[email protected]"], ["A003","SALLY","[email protected]"] ] Commented Oct 30, 2019 at 12:26
  • 1
    And you need that output from index.php right? Commented Oct 30, 2019 at 12:32
  • @PeterM . Yup, I need to echo the output in index.php only. Is it possible ? Commented Oct 30, 2019 at 12:33

1 Answer 1

1

Try changing

foreach(json_decode($result) as $result2)
{
    if(!in_array($result2[0],$arr))
    {
        $total = "".$result2[0].",".$result2[1].",".$result2[2]."<br/>"; 
        echo json_encode($total);
    }       
}

into

foreach(json_decode($result) as $result2)
{
    if(!in_array($result2[0],$arr))
    {
        $total[] = $result2; 
    }       
}
echo json_encode($total);
Sign up to request clarification or add additional context in comments.

2 Comments

Thanks. But how can I add bracket for each row of data ?
@Rozaimi this should do the trick, json_encode does all the magic for your, no need to add the brackets [] on your own. Also don't add <br/> in there, your JSON will be invalid.

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.