0

How can i change the array name gotten from the database : for example

If i print this json_encode($rows)

I have :

{"access":"MTAPI"}

But, in the Json format, i want "access" to be called "Type".

How may i achieve this ?

Secondly, how may i push more data into the array to appear as such :

{"access":"MTAPI", "alias":"result"}

I tried but it's messy:

array_push($rows, "alias", "result");

Thirdly, if i have a whole structure will multiple sql statements that i need to perform to built it, should i use a giant array, fit everything in then pass to json_encode ? or does json_encode do some kind of concatenation ?

The array of $row

 $sth =  $dbh->query("SELECT access FROM NodeAttributes WHERE Node_ID = '$nodeid[$i]'")or die(mysql_error());
            $rows = array();
            while($r = $sth->fetch(PDO::FETCH_ASSOC)) {
                $rows[] = $r;
            }

3 Answers 3

2

But, in the Json format, i want "access" to be called "Type".

How may i achieve this?

Unset the "access" key and set "Type" in its place:

$rows['Type'] = $rows['access'];
unset($rows['access']);
echo json_encode($rows);

Secondly, how may i push more data into the array to appear as such

Just add extra key/value pairs:

$rows['alias'] = 'result';

array_push is a function that only very rarely has a legitimate use case, don't prefer it.

Thirdly, if i have a whole structure will multiple sql statements that i need to perform to built it, should i use a giant array, fit everything in then pass to json_encode ? or does json_encode do some kind of concatenation ?

It's not clear what you mean. "Giant array" vaguely sounds like the simplest practical solution, but without clarification that doesn't say much.

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

2 Comments

thank you for your reply. I tried the renaming and this is what i get instead : {"0":{"access":"SNMP"},"Type":null} Then when i tried adding the extra key/value pairs, i have this output: {"0":{"access":"SNMP"},"alias":"result"} Is the output interpreted the sae as with the sample i provided above ? And about the last paragraph, I intend to build a structure that looks like this link So i was asking if it's gonna be 1 big array with sub arrays in.
@beginningperl: It seems like you meant to write $row instead of $rows in the original question; the output you give is not the whole output of json_encode, right?
0

try this

$arr_temp['Type'] = $rows['access'];
$arr_temp['alias'] = "result";

echo json_encode($arr_temp);

you need to create a temp array and push the values and index you want then encode it as json

UPDATE 2:

you can use this in your query like this-->

 $sth =  $dbh->query("SELECT access FROM NodeAttributes WHERE Node_ID = '$nodeid[$i]'")or die(mysql_error());
$rows = array();
while($r = $sth->fetch(PDO::FETCH_ASSOC)) {
       $rows['Type'] = $r['access'];
       $rows['alias'] = "result";
       echo json_encode($rows);
 }

1 Comment

This is my output : {"Type":null,"alias":"result"}. Is the problem from my code ?
0

You can able to store result to your own variable then encode this as json.

$output = array( 'Type' => $row[0]['access'] );

json_encode($output);

4 Comments

This perfectly replaces Type with "access" but the value for access is now null.
may I know the values of $rows. Please give the value of $rows here. php code : print_r($rows); . Based upon this I can able to provide you the solution
` Array ( [0] => Array ( [access] => SNMP ) ) ` It's something of that sort.
Try $output = array( 'Type' => $rows[0]['access'] );

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.