How to get specific values from an array in while loop - php

$array = array();
while ($row = mysqli_fetch_assoc($result)) {
$user_id = $row["user_id"];
$user_name = $row["user_name"];
}
foreach ($array as $arr) {
echo $arr;
}
Above code echos all the values from the $array how can I get a specific value of this array.
For example something like this echo $arr[2] (but it doesn't work)
Please mention that I'm getting some data from mysql and my purpose by asking this question is to get each value from a column separately.
Thank you if you can help me.

You forgot to fill your array with data...
while($row = mysqli_fetch_assoc($result)){
$array[]= array($row["user_id"], $row["user_name"]);
}
And now you can access to your data
foreach($array as $arr){
echo $arr[0];
echo $arr[1];
}

If you need to get the specific value of an array. you can get by
$array[2]
not
$arr[2]

Please try
$array = array();
while($row = mysqli_fetch_assoc($result)){
$array[] = $row;
}
foreach($array as $arr){
print_r($arr);
}

$array = array();
while($row = mysqli_fetch_assoc($result)){
$user_id = $row["user_id"];
$user_name = $row["user_name"];
$array[] = $row;
}
And use $array[2] to get records in 2nd index.
OR
foreach($array as $arr){
echo $arr[0];
echo $arr[1];
}

Also useful is to use the $key=>$value in your foreach loop.
foreach ($array as $key=>$value) {
echo $key."->".$value."<br />";
}
This will list each of your array items with values and respective keys.

Related

Php sort by filename

I have this code
if($dateOrder){
$order = array(filemtime($filter_files[0]));
for($i=1;$i<$maxnr+1;$i++){
array_push($order,filemtime($filter_files[$i]));
}
array_multisort($order,SORT_DESC,SORT_NUMERIC,$filter_files,SORT_ASC,SORT_NUMERIC);
}
}
//end get image files
How to make possible sort order by filename? For example
picture1 , picture2 , picture3 picture10 , picture11
Here is the working code as per my proposal. The difference from your code is the usage of array_multisort method. PHP array_multiosrt expects single dimension non assoc arrays as its first and second dimension and then the whole data array as the last argument.
<?php
$dateOrder = true;
if($dateOrder){
/*$order = array(filemtime($filter_files[0]));
for($i=1; $i<$maxnr+1; $i++){
array_push($order,filemtime($filter_files[$i]));
}*/
$order = array('picture1', 'picture2', 'picture20', 'picture9', 'picture3', 'picture10', 'picture11');
//array_multisort($order,SORT_DESC,SORT_NUMERIC,$filter_files,SORT_ASC,SORT_NUMERIC);
$names = array();
for($i=0; $i<count($order); $i++) {
preg_match('/^(.+?)(\d+)$/', $order[$i], $matches);
$names[] = array($matches[1], $matches[2]);
}
$name = array();
$number = array();
foreach ($names as $key => $row) {
$name[$key] = $row[0];
$number[$key] = $row[1];
}
array_multisort($name, SORT_ASC, $number, SORT_NUMERIC, $names);
$output = array();
foreach ($names as $row) {
$output[] = $row[0] . $row[1];
}
print_r($output);
}
?>
Fiddle

how to store associative array php MySQL

I want to store result of a query as associative array. Below is my code that generates the query result below.
<?php
$include('config.php') //mysql connection file
$result = mysql_query("SELECT daystime.*, Sprinkler_ID FROM daystime, scheduler WHERE daystime.id = scheduler.DaysTime_ID ORDER BY daystime.id, Sprinkler_ID") or trigger_error(mysql_error());
$data_array = array();
while($rs = mysql_fetch_assoc($result))
{
$key=$rs['id'];
$value=$rs['Sprinkler_ID'];
$data_array[$key] = [$value];
}
foreach ($data_array as $key => $value)
{
echo $key.'=>'.$value.'<br />';
}
?>
This gives me output as
19=>Array
20=>Array
21=>Array
27=>Array
29=>Array
But I should get
19 -> [4,5],
20 -> [5],
21=>[4,6],
// and so on
$value is an array, you can't use echo on arrays
Don't loop just do a var_dump()
$data_array = array();
while($rs = mysql_fetch_assoc($result)){
$data_array[$rs['id']][]=$rs['Sprinkler_ID'];
}
var_dump($data_array);//or print_r($data_array);
<?php
$include('config.php') //mysql connection file
$result = mysql_query("SELECT daystime.*, Sprinkler_ID FROM daystime, scheduler WHERE daystime.id = scheduler.DaysTime_ID ORDER BY daystime.id, Sprinkler_ID") or trigger_error(mysql_error());
$data_array = array();
while($rs = mysql_fetch_assoc($result))
{
$key=$rs['id'];
$value=$rs['Sprinkler_ID'];
$data_array[$key] = [$value];
}
$out = '';
$count = count($data_array);
$iter = 0;
foreach ($data_array as $key => $value)
{
$out.= $key.'=>[';
foreach ($value as $val) {
$out.=$val.',';
}
$out = rtrim($out, ",");
$out.= ']';
if ($iter < ($count-1)) {
$out.=',<br />';
}
$iter++;
}
echo $out;
You need an inner foreach loop to handle printing the array in $value as this is a multi dimensional array. The preceding is an example of how it could look to produce the exact output you requested. If you are looking for a dump of the values, then please use the var_dump solution provided by #meda.
There are two issues in your code. The first is that $value is an array, the second is that this array only contains one item.
Try this:
$data_array = array();
while($rs = mysql_fetch_assoc($result))
{
$key=$rs['id'];
$value=$rs['Sprinkler_ID'];
$data_array[$key][] = $value;
}
foreach ($data_array as $key => $values)
{
echo $key.'=> [';
foreach($values as $value)
echo $value . ',';
echo ']<br />';
}

Store data into array variable php

If i echo inside the while loop i get 4,2 values and if i echo outside the while loop then i only get 2. I want to get the data from $row into the values array. is something missing in this code?
$query = oci_parse($con, "SELECT count(*) FROM Counter GROUP BY Blog_name");
oci_execute($query);
while($row = oci_fetch_array($query))
{
$s = $row[0].',';
$values = explode(',', $s);
echo $values[0]; // 4
echo $values[1]; // 2
}
echo $values[0]; // 2
echo $values[1]; // 2
Try this,
$Values = array();
while($row = oci_fetch_array($query))
{
$Values[] = $row[0];
}
echo $Values[0];
First, $values as you're using it is just a string, and is not an array.
Try adding before the while loop.
$values = array();
Second, there's really no need to use explode here, it's an unnecessary step. If you only want the first column in the row, you can simply add that column to $values. (Currently you are overwriting the contents of $values at each iteration of the loop because you are missing the [].)
$values[] = $row[0];
If you're trying to put ALL of the columns in each row into values, try:
$values[] = $row;
If you're trying to individually put the contents of each column into it's own index in $values, try:
while($row = oci_fetch_array($query))
{
foreach($row as $column)
{
$values[] = $column;
}
}
Do like this:
$s = array();
while($row = oci_fetch_array($query)) {
$s[] = $row[0];
}
echo $s[0];
print_r($s);
$query = oci_parse($con, "SELECT count(*) FROM Counter GROUP BY Blog_name");
oci_execute($query);
$values = array();
while($row = oci_fetch_array($query))
{
$values[] = $row[0];
}
print_r($values);

How do I display all the values of a multi dimentional array?

Hey I have an array that contains an array:
$outPutResults = array();
while($row = mysqli_fetch_array($results))
{
$outPutResults[] = $row['industry'];
$outPutResults[] = $row['location'];
$outPutResults[] = $row['title'];
$outPutResults[] = $row['description'];
}
$searchResults[] = $outPutResults;
I am little confused how I would echo the contents.
As far as I am aware $outputresults[] should now be contained within $searchresults[].
To output the content I'm using :
foreach ($searchResults[0] as $item) {echo $item;}
But this is only echoing the first set of results so rather than having to repeat the above and changing the number (0) each time how would i do it so it outputs all the sub arrays?
Just nest your foreach:
foreach ($searchResults as $row) {
foreach($row as $item) {
echo $item;
}
}
$outPutResults = array();
$i = 0;
while($row = mysqli_fetch_array($results))
{
$outPutResults[$i] = $row;
$i++;
}
foreach ($outPutResults as $items)
foreach($items as $item)
echo $item;
Why are you doing
$searchResults[] = $outPutResults;
instead of just
$searchResults = $outPutResults;
Then you don't have to worry about nesting your foreach statements.
You should nest two loops. For example like this:
foreach ($searchResults as $array) {
foreach ($array as $item){
echo $item;
}
}

Ignore value from assoc array when display

$result = mysql_query($sql_result);
$newArray = array();
$index=0;
while($row = mysql_fetch_assoc($result)){
$newArray[$index] = $row;
$index++;
}
I wanna ignore the value - from my assoc array when display. Help me please.
If you want to remove all columns containing a certain value you could use array_filter:
function removeEmpty($v){
return $v != '-';
}
while($row = mysql_fetch_assoc($result)){
$newArray[$index] = array_filter($row,"removeEmpty");
$index++;
}

Categories