Create php key and value array [duplicate] - php

This question already has answers here:
JSON encode MySQL results
(16 answers)
Closed 3 years ago.
I am trying to create a key and value array from database record, but the array just getting the final record! Here is a snippet of my code:
$cateogiresArr = array();
while ($row = mysqli_fetch_row($result))
{
$cateogiresArr["categoryname"] = $row[1];
$cateogiresArr["description"] = $row[2];
}
header("Content-type:application/json");
$json_categories = json_encode($cateogiresArr);

On each iteration add new array with required data to $cateogiresArr:
while ($row=mysqli_fetch_row($result))
{
$cateogiresArr[] = [
"cateogryname" => $row[1],
"description" => $row[2],
];
}

Related

Remove duplicates from array inside another array [duplicate]

This question already has answers here:
php multi-dimensional array remove duplicate [duplicate]
(6 answers)
Closed 3 years ago.
I have an array who have another one inside, but when i get the response from database i have duplicates, and i don't want to have this, any help?
I have used the solution provided in the link and it doesn't work
"Warning: Illegal offset type in"
$atividadesArray = array();
foreach ($result as $row) {
$idAtividade = $row['idAtividade'];
if (!isset($atividadesArray[$idAtividade])) {
$atividadesArray[$idAtividade]['idAtividade'] = $row['idAtividade'];
$atividadesArray[$idAtividade]['Periodo'] = $row['Periodo'];
$atividadesArray[$idAtividade]['Mes'] = $row['Mes'];
$atividadesArray[$idAtividade]['haveClasses'] = $row['haveClasses'];
$atividadesArray[$idAtividade]['Destinatarios'] = $row['Destinatarios'];
$atividadesArray[$idAtividade]['Nome'] = array();
$atividadesArray[$idAtividade]['Grupo'] = array();
$atividadesArray[$idAtividade]['Departamento'] = array();
}
$atividadesArray[$idAtividade]['Nome'][] = $row['Nome'];
$atividadesArray[$idAtividade]['Grupo'][] = $row['Grupo'];
$atividadesArray[$idAtividade]['Departamento'][] = $row['Departamento'];
}
foreach ($atividadesArray as $idAtividade => $t ) {
$json[]=$t;
}
echo json_encode($json);
Just do not add the item, if it already exists in the array:
...
// Search for the element in the array
if (array_search($row['Departamento'], $atividadesArray[$idAtividade]['Departamento']) === false) {
// Add only if nothing found
$atividadesArray[$idAtividade]['Departamento'][] = $row['Departamento'];
}
Note === operator is required to not mess element with key 0 (the first added) and false boolean value;

PHP Return Array in JSON , what happen this return include index? How To Fix? [duplicate]

This question already has answers here:
mysqli_fetch_array Gives Me Duplicate Rows
(4 answers)
Closed 3 years ago.
enter image description here
$table = $this->Execute("select * from data ");
$result = array();
while($row = mysqli_fetch_array($table))
{
array_push($result, $row);
}
return $result;
this my code,
i dont know why my result including the index
Its because of this statement:
while($row = mysqli_fetch_array($table))
You are getting numeric indexes as well as text keys.
Replace this by:
while($row = mysqli_fetch_assoc($table)) // will return only associate (string) keys.
OR
while($row = mysqli_fetch_array($table, MYSQLI_ASSOC)) // will return only associate (string) keys.
This will not include numeric indexes.
References:
mysqli_fetch_assoc()
mysqli_fetch_array()

extract value fom Row in table contain array element [duplicate]

This question already has an answer here:
How to extract and access data from JSON with PHP?
(1 answer)
Closed 7 years ago.
i have in my row an array
when
i go this result
{"type":"person1","id":"12"}
I want just to get the id as result value 12
Use json_decode:
$value = ' {"type":"person1","id":"12"}';
$result = json_decode($value, true);
echo $result['id']; // 12
$addedBy = json_decode($row['added_by']);
$id = $addedBy['id'];

If Array is Empty PHP - Highcharts [duplicate]

This question already has answers here:
How to check whether an array is empty using PHP?
(24 answers)
Closed 7 years ago.
I am creating a form to filter the data on my database (Mysql) with queries based on the user's selections.
I am using this code to generate the "data.json" file:
How can I improve the code and check if the array returns empty values? and instead of drawing a chart without bars tells the user to change the selections.
<?php
$rows1 = array();
$rows1['name'] = $varLabel;
while($rr = mysqli_fetch_array($TableData)) {
$rows1['data'][] = $rr[$varLabel];
}
$rows = array();
$rows['name'] = "Registros";
foreach($TableData as $r) {
settype($r['cnt'], "integer");
$rows['data'][] = $r['cnt'];
}
$result = array();
array_push($result,$rows);
array_push($result,$rows1);
file_put_contents("data.json", json_encode($result));
?>
Since your array is created by looping over your database result set which you access using mysqli, Use mysqli_num_rows
if(mysqli_num_rows($TableData)==0)
{
// no data
}

Create json Object by php from mysql result [duplicate]

This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
JSON encode MySQL results
I want to use php to create a json object like below. it will return a string as a response from result sql query.
{"Orders":[
{"DeliveryId":"DeliveryId","CustomerName":"CustomerName","PhoneNumber":"PhoneNumber","Address":"Address"},
{"DeliveryId":"DeliveryId","CustomerName":"CustomerName","PhoneNumber":"PhoneNumber","Address":"Address"}
]
}
my code
<?php
mysql_connect("mysql12.000webhost.com","a4602996_longvan","longvan2012");
mysql_select_db("a4602996_lv");
$id=$_POST[user];
$sql=mysql_query("select * from testlongvan where Status = 'PACKED'" );
$json = array();
if(mysql_num_rows($sql)){
while($row=mysql_fetch_row($sql)){
$json['Orders'][]=$row;
}
}
//while($row=mysql_fetch_assoc($sql))
//$output[]=$row;
print(json_encode($json));
mysql_close();
?>
But when use my code the result is not what I expected:
{"Orders":[
["longvan","10/12/2012","Be34433jh","Long Van","115 Pham Viet Chanh, quan Binh Thanh","http://longvansolution.tk/image/sample.jpg","PACKED","0909056788"],
["takeshi","24/12/2012","BF6464633","Vn-zoom","16 nguyen cuu van, quan binh thanh","http://longvansolution.tk/image/hoadon3.jpg","PACKED","098897657"]
]}
Can you help me?
You have to create an array for each row to specify the field name and value.
$json['Orders'][] = array('DeliveryId' => $row[0], 'CustomerName' => $row[1], ...);
Or use mysqli_fetch_assoc() function if the table column name is exactly what you want to use in your JSON:
$rows = array();
while($r = mysqli_fetch_assoc($sql)) {
$rows[] = $r;
}
$data = array('Orders' => $rows);
print json_encode($data);

Categories