Get ASSOC array value by numeric index - php

In all the examples of Codeigniter queries at https://www.codeigniter.com/user_guide/database/results.html, I find that the name of the field must be known to get its value.
$query = $this->db->query("SELECT title,name,body FROM table");
foreach ($query->result() as $row)
{
echo $row->title;
echo $row->name;
echo $row->body;
}
For example, if I want to get the title, I will perform row->title. Is there a way to get the title using an index e.g. like $row[0]?

Use result_array function returns the query result as a pure array
$query = $this->db->query("SELECT title,name,body FROM table");
$result = $query->result_array();
foreach($result as $res){
echo $res['title'];
echo $res['name'];
echo $res['body'];
}
if you want to access via index then use array_values:
$result = $query->result_array();
foreach($result as $res){
$r = array_values($res);
echo $r[0];
echo $r[1];
echo $r[2];
}

<?php
$result = $subject_code->result_array();
foreach($result as $res){
$r = array_values($res);
print_r($r);
}
?>
I applied the above code but in o/p index of each value is comming 0
o/p- Array ( [0] => 201 ) Array ( [0] => 202 ) Array ( [0] => 203 ) Array ( [0] => 204 ) Array ( [0] => 205 )

Slightly more elegant way:
$data = $this->db->query("SELECT title,name,body FROM table")->result_array();
array_walk($data,function(&$row) {
$row = array_values($row);
});
And you have $data array with numeric indexes.

Related

Add multiple SQLresults to an array while in a foreach loop

I am having some trouble trying to get this done. The issue is:
I have an array that looks like
wants_arr_flat ( [0] => booze [1] => nudes [2]
=> rooms
I want my foreach loop to go through these values making a different SQL statement and then saving the result of that SQL statement on to another array. The code I am trying to work with now is this.
foreach ( $wants_arr_flat as $value ) {
$sql = "SELECT * FROM offers WHERE user_id != $_SESSION[user_id] AND offer='$value'";
$result=$conn->query($sql);
$want_match_arr = mysqli_fetch_row($result);
echo '<pre>'; print_r($want_match_arr); echo '</pre>'; //testing echo
Obviously this is just overwriting the last array each iteration so I only end up with one result in the array.
instead of
$want_match_arr = mysqli_fetch_row($result);
use
$want_match_arr[] = mysqli_fetch_row($result);
If you get multiple rows from SQL then this is better.
$want_match_arr = [];
foreach ( $wants_arr_flat as $value ) {
$sql = "SELECT * FROM offers WHERE user_id != $_SESSION[user_id] AND offer='$value'";
$result=$conn->query($sql);
while ($row = mysql_fetch_assoc($result)) {
$want_match_arr[] = $row;
}
}
echo '<pre>'; print_r($want_match_arr); echo '</pre>'; //testing echo

Make Associative array with three element in array

I want three element in associative array, so far am successful in getting two in the array.
$sql = "SELECT * FROM `notification_table` ";
$resultsd1 = mysqli_query($conn, $sql);
$row = mysqli_fetch_assoc($resultsd1);
$associativeArray = array();
while ($row = mysqli_fetch_assoc($resultsd1))
{
$associativeArray[$row['name']] = $row['price'] ;
}
foreach($associativeArray as $k => $id){
echo $k."=>".$id .' ';
}
And am getting the response like this
name1=>24.725 name2=>24.265
Now i want to add another column in array as well and the name is column is notification_check .
Am not able to get how to add three columns in a single array. Any help will be appreciated.
I want the output like name1=>24.725=>yes_notification name2=>25.43=>no_notification
And when i print_r($row) is show this output Array ( [sno] => 1 [name] => name1 [price] => 23 [notification_check] => yes_notification)
You could shorten this and use mysqli_fetch_all to create an array of all of the data and then manipulate the array using array_column to create the index...
$result = mysqli_fetch_all($resultsd1, MYSQLI_ASSOC);
$associativeArray = array_column($result, null, 'name');

PHP change array values to create new array

this way how i get array from db:
$sql = "SELECT * FROM table_name";
$query = mysqli_query($conn, $sql);
$db_array = array();
// start fetch for words
if (mysqli_num_rows($query) > 0){
while ($row = mysqli_fetch_assoc($query)){
$db_array[] = $row;
}
}
My array looked like this:
Array
(
[cat cute] => animal#cute animal
[cat women] => film
)
How to add '{' and '}' to all values?
I wish i can get new array like this:
Array
(
[cat cute] => {animal#cute animal}
[cat women] => {film}
)
It's hard for me, i am new in php development.
Try this,
$arr = array
(
'cat cute' => 'animal#cute animal',
'cat women' => 'film'
);
array_walk($arr, function(&$item){
$item = '{'.$item.'}';
});
print_r($arr);
Here is the link for array_walk()
Since you want a new array, try this:
<?php
array_map(function($value)
{
return '{' . $value . '}';
}, $arr);
array_walk() in rahul_m's answer modifies your array, while array_map() creates a new one.

Access array variable in CodeIgniter

this is my module
function order_alert($email){
$this->db->select("tbl_customer_registration.cus_mobile");
$this->db->from('tbl_customer_registration');
$this->db->where('tbl_customer_registration.cus_email', $email);
$query = $this->db->get();
echo '<pre>';
print_r($query->result()) ;
}
It return the following result for the above code
Array
(
[0] => stdClass Object
(
[cus_mobile] => 0716352642
)
)
In the contoller I use foreach
foreach($result as $row) :
echo $row['cus_mobile'];
to get [cus_mobile] out of the above array but it gives me all [cus_mobile] rows in the tbl_customer_registration.
How do I get the only [cus_mobile] => 0716352642 out of it.
$row->cus_mobile
since you have an array of objects.. not really sure if i got your question
(UPDATED)
Try this..
foreach($query->result() as $row) {
echo $row->cus_mobile;
}
If I got well your question, what your doing is retrieving data as array of objects and trying to access its data as pure array. To do so as you mean you might retrieve data result as an array there is method called result_array() you might use it instead $query->result().
echo '<pre>';
print_r($query->result_array()) ;
foreach($result as $key => $row) :
echo $row['cus_mobile'];
function order_alert($email){
$this->db->select("tbl_customer_registration.cus_mobile");
$this->db->from('tbl_customer_registration');
$this->db->where('tbl_customer_registration.cus_email', $email);
$query = $this->db->get();
if($query->num_rows()==1)
$result_row = $query->row();
return $result_row;
else
return false;
}
foreach($result_row as $row) {
echo $row->cus_mobile;
}
This will work for if result has one row... now to need to handle the case for greater than one row.Thanks

php getting values out of an array with same 'id' number

I'm having trouble getting the value out of an array
Array
(
[0] => id
[column_name] => id
)
Array
(
[0] => businessType
[column_name] => businessType
)
Array
(
[0] => name
[column_name] => name
)
Array
(
[0] => city
[column_name] => city
)
...
I'm getting those values from
mysql_query("
SELECT column_name
FROM information_schema.columns
WHERE table_name='businessUpgrade'
")
And in my while loop
while($row = mysql_fetch_array($result)){
while($column = mysql_fetch_array($colName)){
if($row[$i] == 1){
//here comes the missing part
}
$i++;
}
}
I tried diffrent things, but according to print_r, the values I need to get have the same number of id (0). Is there any way, I can get the values out of this array.
I found that I should do it with foreach, but somehow everything I try it fails.
There is no need to use 2 while loops.
The $row array is an associative array, so you can use...
$columns = array();
// {query}
while($row = mysql_fetch_array($result)) {
$columns[] = $row['column_name'];
}
var_dump($columns);
Your $columns array now contains an index of all the columns. It's a zero-index, so these can be pulled individually using:
echo $columns[0]; // The first column from the query "id"
echo $columns[2]; // The third column from the query "name"
You can also loop this array as needed.
foreach ($columns as $id => $column) {
if ($id == 0) {
// Do something with the first column:
echo $column;
} elseif ($id == 2) {
// Do something with the third column:
echo $column;
}
}
try this
$res = mysql_query("
SELECT column_name
FROM information_schema.columns
WHERE table_name='businessUpgrade'
");
while($r = mysql_fetch_row($res)){
$arr[]=$r;
}
NOTE . do not use mysql. Try mysqli or PDO instead.
And with this:
while($row = mysql_fetch_array($result)){
while($column = mysql_fetch_assoc($colName)){
if(($column['0'] == $row[$i]) && ($row[$i] == 1)){
//something like this???
}
}
$i++;
}
Your question is a bit messy XD.....i hope this helps anyway.

Categories