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);
Related
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()
I am trying to do a mysql fetch but it keeps adding numbered and labeled keys to the array. I want it to record only the labeled names and data in the array.
Am I using the wrong mysql call?
global $con,$mysqldb;
$sql="SHOW FIELDS FROM ".$dbtable;
$tr = mysqli_query($con,$sql);
$tl = mysqli_fetch_array($tr);
$tl = mysqli_fetch_array($tr);
$sql="SELECT * FROM ".$mysqldb.".".$dbtable." ORDER BY ".$tl['Field']." LIMIT 3";
$result = mysqli_query($con,$sql);
while($row = mysqli_fetch_array($result)) {
$table[$row[1]] = $row;
}
foreach($table as $item => $data){
foreach(array_keys($data) as $pointer => $field) {
echo"pointer=".$pointer."\t";
echo"field=".$field."\n";
echo "data=".$data[$field]."\n";
}
}
reults
pointer=0 field=0 data=3823
pointer=1 field=PID data=3823
pointer=2 field=1 data=AA
pointer=3 field=symbol data=AA
pointer=4 field=2 data=1
pointer=5 field=value data=1
I want to omit 0, 2, & 4 from the array.
Take a look at the PHP.net manual for the mysqli_fetch_array() function.
You'll see there's an option called resulttype that will accept 1 of 3 values - MYSQLI_ASSOC, MYSQLI_NUM, or MYSQLI_BOTH the default.
Using MYSQLI_ASSOC will remove the numbered keys.
Or check mysqli_fetch_assoc().
Thanks to thebluefox for a speedy response.
I replaced the fetch with:
while($row = $result->fetch_array(MYSQLI_ASSOC)) {
And now the results are being recorded as they should.
This question already has answers here:
How to load MySQLi result set into two-dimensional array?
(3 answers)
Closed 6 years ago.
Looking at all the questions are using the depreciated mysql_fetch_assoc/array, hence I don't think that this is a duplicate question.
I have a MySQL table with 5 columns,
ID | NAME | AGE | GENDER | HEIGHT
If I want to store the values of NAME, AGE, GENDER in a PHP array,
$query=$mysqli->query("SELECT NAME,AGE,GENDER FROM TableName")
while($result = $query->fetch_assoc()
{
$array = [];
}
Will my array be stored in the format of
$array=[[Name,Age,Gender],[Name,Age,Gender]]?
If not, what would be my approach in doing this?
It's very simple. You just have to append the result variable in to main array. Like this,
$array =array();
while($result = $query->fetch_assoc()
{
$array[] = $result;
}
$result is an array (fetch_assoc it returns result-set in array) so just append that into main array to get the desired result. ($array=[[Name,Age,Gender],[Name,Age,Gender]])
$data =[];
$i=0;
while($result = $query->fetch_assoc(){
$data[$i][] = $result;
$i++;
}
Hi in the code below I want to add an extra value for the associative array. For each queryresult in wich elements ["Nietaanwezig"] and ["Aanwezig_diner"] both are 0 I want to add the element ["Nietingevuld"] and set it's value to 1, otherwise i want to add the element ["Nietingevuld"] and set it's value to 0. Albeit I have tried a lot of options, I don't seem to ge a good solution.
// numerically indexed array of places
$namen = [];
$queryresult = [];
// TODO: search database for places matching $_GET["geo"]
$search = $_GET["zoekopdracht"];
if ($search = "diner")
{
$namen = query ("SELECT * FROM gasten WHERE Typegast = 1");
foreach ($namen as $naam)
{
$queryresult [] = [
"Voornaam" => $naam["Voornaam"],
"Achternaam" => $naam["Achternaam"],
"Nietaanwezig" => $naam["Nietaanwezig"],
"Aanwezig_diner" => $naam["Aanwezig_Diner"],
];
}
Don't do it all in a single stage then. Build the new child array, modify it as necessary, THEN insert it into the parent array:
$temp = array('Voornaam' => $naam['Voornaam'], etc....);
if (whatever you want to check) {
$temp['something'] = 'else';
}
$queryresult[] = $temp;
I have written a code in php that deals with php and mysql associative array.
I have wirtten a query in SQL as
$sql=mysql_query("select x,y from table_name");
Extracted value in associative array as
while($row=mysql_fetch_assoc($sql))
{
foreach ($row as $value1=>$value) {
...........
//$a[value1]=convert($row{'y'}); //this is wrong as i am always getting {"x":"value return from function after passing $row{'y'}","y":"value return from function after passing $row{'y'}".....ans so on} i.e same value in both the key.
}
}
What my problem is I want to use some function on one of the value from associative array as convert($row{'y'}) shown above and after the value is return from the function again i want that in associative array as
{"x":"value1","y":"value return from function after passing $row{'y'}".....ans so on} again.
How do I achieve this?
Thanks in advance.
while($row=mysql_fetch_assoc($sql))
{
$row['y'] = convert($row['y']);
}
$result = array();
while($row = mysql_fetch_assoc($sql)) {
if($row == 'someCondition') {
$result = someFunction($row[]);
} else {
$result = $row[];
}
}
now $result will hold all the returned value as an associative array. you can go ahead and use nested if. if you want to add some more condition.
If I am interpreting your question right you want something like this:
while($row = mysql_fetch_assoc($sql) {
$a[] = array(
'x' => $row['x'],
'y' => convert($row['y'])
);
}
Or, if you need the data to look up the y value based on x:
$a[$row['x']] = convert($row['y']);