Remove duplicates from array inside another array [duplicate] - php

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;

Related

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()

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
}

Get position of element in array created through mysql search - PHP [duplicate]

This question already has answers here:
Finding the position of an element in a simple array
(2 answers)
Closed 8 years ago.
I search a database that returns a bunch of values. These values are stored into an array called $result_array. I then want to find the position of an element within that array. Here is my current code
public function cardPosition($cid, $did) {
$query = "SELECT cid FROM card WHERE did = '$did' ORDER BY id ASC";
$result = mysql_query($query);
$result_array = array();
while($row = mysql_fetch_assoc($result)) {
$result_array[] = $row;
}
while ($correct_cid = current($result_array)) {
if ($correct_cid == $cid) {
$cardPosition = key($result_array);
}
next($result_array);
}
return $cardPosition;
}
I use mysql_fetch_assoc() because I want to assign every element a key value. I then use the second while loop to search for the element in the array that is the $correct_cid value. I then assign the value of the key to $cardPosition but when I return $cardPosition I get nothing. How can I get the position of the element within the array?
Update
I have used
$position = array_search($cid, $result_array);
but still get nothing as a result.
I know my query works because I ran
$num = mysql_num_rows($result);
and I get the correct number of rows.
you can find the position of array with array_search() function. suppose we have an array.
$a = array(
'blue' => 'nice',
'car' => 'fast',
'number' => 'none'
);
echo array_search("car",array_keys($a));
$position = array_search("search_text", $result_array);

Undefined index from a foreach() loop [duplicate]

This question already has answers here:
Reference - What does this error mean in PHP?
(38 answers)
Closed 8 years ago.
ItemDb class
public function getRandomItem() {
$query = "SELECT * FROM `items` ORDER BY RAND() LIMIT 2";
return $this->query($query);
}
Index.php
$item = new Item();
$result = $item->getRandomItem();
while ($row = $result->fetch_assoc()) {
foreach ($row as $key => $value) {
//I want to put them in a array but the two items need to be separated
}
}
I get two different items out of the database how can I split them and put them in one array separated like:
$array[$key][$value]
Sorry for my English its my second language and I hope you guys understand me.
You need to declare $itemArray[$key] before you use it. So your code needs to look like
$itemArray = array();
while ($row = $result->fetch_assoc()) {
foreach ($row as $key => $value) {
if(!isset($itemArray[$key])) {
$itemArray[$key] = array(); //Declare it
}
$itemArray[$key][] = $value;
}
}

Create an array from values from mysql table [duplicate]

This question already has answers here:
Creating an array from a MySQL table
(2 answers)
Closed 10 years ago.
I am using PHPlot to make a graph.
I have an issue in generating the array from a MySQL table.
Basivally, I want to array is as follows:
$values = array($arrayx);
array('a',-3),
array('b',5),
array('c',7),
array('d',8),
array('e',12),
array('f',-6),
//);
$graph->SetDataValues($values);
$graph->SetXTickPos('none');
$graph->SetXTickLabelPos('none');
Part of the code where I tried to retrieve values from table to feed the array
$query="SELECT * FROM tasks";
$result=mysql_query($query);
//using a for loop to add values to the array
while ($resource=mysql_fetch_array($result)){
$thedate = $resource["date"];
$title = $resource2["title"];
$innerarray = "array('.$thedate.', $title),";
}
$values = array($innerarray).");";
$graph->SetDataValues($values);
$graph->SetXTickPos('none');
$graph->SetXTickLabelPos('none');
//Draw it
$graph->DrawGraph();
}
The way I'm doing the $innerarray and $values seems wrong. Can you please help me fix it?
Thank you
try replacing
$innerarray = "array('.$thedate.', $title),";
with
$innerarray = array($thedate, $title);
$new = array();
while(for condition ){
$new[] = '\''.thedate[$i].''\','.$title[$i].'\';
}
var_dump($new);
this an idea, you need to edit the code to make it working
I assume it is this that you want:
$sql="SELECT datefield, titlefield FROM tasks";
....
while (list($thedate,$thetitle) = mysql_fetch_array($result)) {
$values[] = array($thedate,$thetitle);
}
echo $values[0][0]; // will output your 1st date
echo $values[0][1]; // will output your 1st title

Categories