0

I am getting two JSON arrays that I need merged each time a while loop runs a fetch from my database. This is the structure array result I am getting as of now:

{id=1,cid=1,fname=Lorna,vorname=King,gender=female,dob=1985,company=helsana,monthly_amount=150},
{id=2,cid=1,fname=Brian,vorname=King,gender=male,dob=2007,company=helsana,monthly_amount=100}

I need it to look like this:

{id={1,2},cid={1,1},fname={Lorna,Brian},vorname={King,King},gender={female,male},dob={1985,2007},company={helsana,helsana},monthly_amount={150,100}}

Here is the PHP code that I am using:

<?php
include_once "conn.php";
$show= $mysqli->query("SELECT id,cid,fname,vorname,gender,dob,company,monthly_amount FROM family where cid = '1' ");
$arr = array();
while($row = $show->fetch_assoc()){
    $arr[] = $row;
}
$json_response = json_encode($arr, JSON_HEX_TAG | JSON_HEX_APOS | JSON_HEX_QUOT | JSON_HEX_AMP);
echo $str = substr($json_response, 1,-1);
?>

Am kinda new to this please someone help me out.

6
  • 1
    Why on earth would you do that? You're basically asking for help totally mangling your data! Commented Sep 29, 2014 at 12:42
  • You are right it ruins the data. I just want the array arranged in a way I can pick each record and the values associated with it. Commented Sep 29, 2014 at 13:48
  • Why can't you do that now? I see no need to pivot it. Commented Sep 29, 2014 at 14:15
  • I need it actually in this format { 1={id:1, cid:1 fname=Lorna, vorname:King, gender:female, dob:1985, company:helsana, monthly_amount:100}, 2={id:2, cid:1 fname=Brian, vorname:King, gender:male, dob:2005, company:helsana, monthly_amount:150} }. Am struggling to figure out how to do that right now. Commented Sep 29, 2014 at 14:18
  • Eh? That's not at all what you asked for in your question. Commented Sep 29, 2014 at 14:20

2 Answers 2

1

You can use new array_column function from PHP to get what you need . Note that JSON will be of structure {id=[1,2],cid=[1,1],fname=[Lorna,Brian]... so [] instead of {}

<?php
  include_once "conn.php";
  $show= $mysqli->query("SELECT id,cid,fname,vorname,gender,dob,company,monthly_amount FROM           family where cid = '1' ");
  $arr = array();
  while($row = $show->fetch_assoc()){ $arr[] = $row; } 
//
$final = array();
foreach ( array_keys($arr[0]) as $fieldName){
    $final[$fieldName] = array_column($arr, $fieldName);
}

$json_response =  json_encode($final,JSON_HEX_TAG | JSON_HEX_APOS | JSON_HEX_QUOT | JSON_HEX_AMP);
echo $str = substr($json_response, 1,-1);

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

Comments

0

I would seriously recommend against what you're asking. There's a good reason why those results are separate.

Anyway to answer your question here's a way - but again unless there's a specific reason why you want to so this I'd think instead about how you are handling the JSON instead.

Here, I'm just using the first row to set up the array and then manually concatenating the data.

<?php
include_once "conn.php";
$show= $mysqli->query("SELECT id,cid,fname,vorname,gender,dob,company,monthly_amount FROM           family where cid = '1' ");
$arr = array();
$arr[] = $show->fetch_assoc();
while($row = $show->fetch_assoc()){
  $arr['id'] .= ',' . $row['id'];
  $arr['cid'] .= ',' . $row['cid'];
  $arr['dob'] .= ',' . $row['dob'];
  $arr['company'] = ',' . $row['company'];
  $arr['monthly_amount'] = ',' . $row['monthly_amount'];
  $arr['vorname'] = ',' . $row['vorname'];
  $arr['fname'] = ',' . $row['fname'];
} 
$json_response = json_encode($arr, JSON_HEX_TAG | JSON_HEX_APOS | JSON_HEX_QUOT | JSON_HEX_AMP);
echo $str = substr($json_response, 1,-1);

3 Comments

You are right it ruins the data. I just want the array arranged in a way I can pick each record and the values associated with it.
You are right its not advisable to put the data in that format. I just want the array arranged in a way I can pick each record and the values associated with it. I need it actually in this format { 1={id:1, cid:1 fname=Lorna, vorname:King, gender:female, dob:1985, company:helsana, monthly_amount:100}, 2={id:2, cid:1 fname=Brian, vorname:King, gender:male, dob:2005, company:helsana, monthly_amount:150} }. How can this be done?
@CalebNasio - that's kind of what you have already. You're encoding an array that you can easily parse in JavaScript. Have a look here and hopefully you'll see what's going on: jsfiddle.net/2pxvp624

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.