Passing variable to array - php

I am passing data base objects to an array.
I need to include another variable to the array. The variable is $latitud_usuario.
Here is the code:
if ($result->num_rows > 0) {
while ($obj = $result->fetch_object()) {
$arr[] = array('nombre_doctor' => $obj->nombre_doctor,'apellido1_doctor' => $obj->apellido1_doctor,'apellido2_doctor' => $obj->apellido2_doctor,'ciudad_doctor' => $obj->ciudad_doctor, 'latitud_doctor' => $latitud_usuario);
}
}
}
echo json_encode($arr);
If I create the array including only fetched objects, the JSON sent is ok, but after including the last array object:
'latitud_doctor' => $latitud_usuario
the JSON is not received as it should.
I guess this last array object expression is wrong.
Any hint is welcome.

Try this
if ($result->num_rows > 0) {
while ($obj = $result->fetch_object()) {
$arr[] = array('nombre_doctor' => $obj->nombre_doctor,'apellido1_doctor' => $obj->apellido1_doctor,'apellido2_doctor' => $obj->apellido2_doctor,'ciudad_doctor' => $obj->ciudad_doctor, 'latitud_doctor' => $latitud_usuario);
$arr['latitud_doctor']=$latitud_usuario;
}
}
}
echo json_encode($arr);

Here's a version that works (using a dummy $obj object):
$obj = (object) array('nombre_doctor'=> 6, 'apellido1_doctor' => 'whatever1', 'apellido2_doctor' => 'whatever2',
'ciudad_doctor' => 'Montreal', 'latitud_usuario' => '35463');
$arr[] = array('nombre_doctor' => $obj->nombre_doctor,'apellido1_doctor' => $obj->apellido1_doctor,
'apellido2_doctor' => $obj->apellido2_doctor,'ciudad_doctor' => $obj->ciudad_doctor,
'latitud_doctor' => $obj->latitud_usuario);
echo json_encode($arr);

Related

Error trying to use json_encode() on an array() from mysqli fetch_array()

I'm really having trouble trying to execute the function below:
function retrieve_as_JSON($sql_statement)
{
$r = oquery($sqli); // oquery runs all that conn()->query() stuff
if($r->num_rows)
{
$arr = array();
while($tmp = $r->fetch_array( MYSQLI_ASSOC ))
{
$arr[] = json_encode($tmp);
}
return json_encode($arr);
}
}
$tmp in the loop is:
Array ( [code] => ecb36c8e9e70b1622fb85ce1af7ba824
[prsn] => a6abd41ca4376f1ccb5d8425e9e97ca6
[type] => Isento
[motn] => Nome da Mãe
[natu] => Joseense
[nati] => Brasileiro
[mrts] => Solteiro(a)
[conj] => Nome da esposa
[prof] => Analista/Programador
[skcl] => Caucasiano/Branca
[lafr] => 0 )
The result is "[false]". Without the json_encode(), it return a regular array() filled with information. Using just one time the json encode function, on return or inside the while, it still gives nothing...
Any help?

Php array and json

help me to convert the following array in to json.
I tried to convert the array.
Array
(
[0] => Array
(
[c_code] => 200001
[itemname] => 303 10CAP
[c_pack_code] => PK0075
[c_web_img_link] =>
)
[1] => Array
(
[c_code] => 200005
[itemname] => 3P 4TAB
[c_pack_code] =>
[c_web_img_link] =>
)
)
current result for the following code is
public function searchOrder($idx, $data) {
if (!empty($data)) {
$result = OrderbukModel::func_get_searchlist($idx,$data);
if (!empty($result)) {
$resultArray[] = $result;
print_r(json_encode($result));
} else {
$resultArray[$idx] = ["Mysql returns empty result !"];
print_r(json_encode($resultArray));
exit;
}
}
}
now i got the result is like
[{"c_code":"200001","itemname":"303 10CAP","c_pack_code":"PK0075","c_web_img_link":""},{"c_code":"200005","itemname":"3P 4TAB","c_pack_code":"","c_web_img_link":""}]
But I need the result as follows
[{"c_code":"2000001","c_code":"200005"},
{"itemname":"303 10CAP","itemname":"3P 4TAB"},
{"c_pack_code":"PK0075","c_pack_code":""},
{"c_web_img_link":"","c_web_img_link":""}]
Example of how you can you make the json from array. Collect the data in two different array and after loop marge them and store the result in another array after that encode them.
Note: Your desired JSON is not a valid format, you can't use same index
for two data.
Online Example: https://3v4l.org/kdPDI
$arr = array(
array(
'c_code' => '200001',
'itemname' => '303 10CAP',
'c_pack_code' => 'PK0075',
'c_web_img_link' => ''
),
array(
'c_code' => '200005',
'itemname' => '3P 4TAB',
'c_pack_code' => '',
'c_web_img_link' => ''
)
);
$res1 = array();
$res2 = array();
foreach($arr as $val){
$res1['c_code'][] = $val['c_code'];
$res1['itemname'][] = $val['itemname'];
$res2['c_pack_code'][] = $val['c_pack_code'];
$res2['c_web_img_link'][] = $val['c_web_img_link'];
}
$out = array(array_merge($res1, $res2));
echo json_encode($out);

Store array into array of array

Im generating Chart4PHP.
In sample it takes data like this
$p->data = array(array(array("2010/10",-48),array("2011/01",238),array("2011/02",395)));
I have array "rows" constructed of row[date][units].
Im storing it in this way:
$rows = array();
for(...)
{
$row[date] = $mydate;
$row[units]= $myunits;
$rows[]=$row;
}
What I should make additionally to be able to use it as $p->data = $rows;
To add the extra array container, call array() with the rows array as the argument.
$data = array(array('date' => "2010/10", 'units' => -48),
array('date' => "2011/01", 'units' => 238),
array('date' => "2011/02", 'units' => 395));
foreach ($data as $d) {
$mydate = $d['date'];
$myunits = $d['units'];
$rows[] = array($mydate, $myunits);
}
$p->data = array($rows);

Handling foreach or forloop with array values PHP MYSQL

Output Array (The variable name is $r)
Array
(
[Jan-14] => 7588793.52
[Feb-14] => 9944970.87
[Mar-14] => 8567790.20
[Apr-14] =>
[May-14] =>
[Jun-14] =>
[Jul-14] =>
[Aug-14] =>
[Sep-14] =>
[Oct-14] =>
[Nov-14] =>
[Dec-14] =>
)
What I have done so far is..
while($r = mysql_fetch_assoc($query)) {
$series1['data'][] = $r['Jan-14'];
$series2['data'][] = $r['Feb-14'];
.... it will go till Dec-14.......
array_push($result,$series1);
array_push($result,$series2);
.... it will go till Dec-14.......
}
Expected Output:
The code should look something like this this (dynamic)
while($r = mysql_fetch_assoc($query)) {
for($i=1;$i<=count($r);$i++){
$series.$i['data'][] = ??????????
array_push($result,$series.$i);
..................
}
}
Help me out. Don't talk about the db structure or normalization. The db was given by my client.
Thanks,
Kimz
Do you mean like this?
$result = array();
foreach($r as $month => $value) {
$result[] = array('data' => array($value));
}

Creating JSON array with information retrieved from database

I am trying to create a JSON array with the information selected from a database but I cannot give the array a name.
while($row = mysql_fetch_array($result))
{
$arr = array('isim' => $row['ename'], 'yer' => $row['eplace'], 'top' => $row['society'], 'tar' => $row['edate'], 'saat' => $row['ehour']);
echo json_encode($arr);
}
I want to see the result;
{"events":[{"isim":"eere","yer":"dddd","top":"asdfsdffgdfgdfg","tar":"2013-10-18","saat":"12:46"}{"isim":"fhjfr","yer":"yhjrhj","top":"ryjryjrj","tar":"2013-10-30","saat":"12:45"}{"isim":"sfsgsg","yer":"sfgssfg","top":"sgsfgsg","tar":"2013-10-31","saat":"12:45"}]}
But I cannot see the
{"events":[
in the beggining and
]}
at the end.
Thank you.
To generate valid JSON, you first need to add everything to a multi-dimensional array and only then when that is complete, encode it:
$arr = array();
while($row = mysql_fetch_array($result))
{
$arr[] = array('isim' => $row['ename'], 'yer' => $row['eplace'], 'top' => $row['society'], 'tar' => $row['edate'], 'saat' => $row['ehour']);
// or perhaps just: $arr[] = $row;
}
echo json_encode($arr);
Also note that the mysql_* functions are deprecated.
To put everything under an events key, you would need something like:
$arr['events'][] = array('isim' => $row['ename'], 'yer' => $row['eplace'], 'top' => $row['society'], 'tar' => $row['edate'], 'saat' => $row['ehour']);

Categories