Indexing certain elements within a PHP array - php

I have a PHP array created by the mysqli_fetch_array() function. The array holds several rows, and without looping through them, can I grab each row one by one?
I have tried:
$links= mysqli_fetch_array($links_result);
echo $links['link'][0];
But I can't seem to get it to work. What could I be doing wrong? Is this possible?

when you do
$links= mysqli_fetch_array($links_result);
you get the next row in result set in $links just access them by the column names.
you can loop over the results like
if(mysqli_num_rows($links_result)) {
while($links= mysqli_fetch_array($links_result)) {
//each row in here
var_dump($links);
}
}

If the field name is 'link'
Without looping:
echo $links[0]['link'];
where '0' is your row number
so, for row #505 it will be:
echo $links[505]['link'];
Just make sure it exists ;)

Without loop you can grab each row of array using recursive function
like this:
function printarray_withoutloop($userarray,$elementindex)
{
if(count($userarray) > $elementindex + 1)
{
echo $userarray[$elementindex]['link'];
printarray_withoutloop($userarray,$elementindex + 1)
}
elseif(count($userarray) == $elementindex + 1)
{
echo $userarray[$elementindex]['link'];
}
}
$links= mysqli_fetch_array($links_result);
$elementindex=0;
printarray_withoutloop($links,$elementindex);

Related

How to check the value is present inside an array and insert the value into database using PHP

I need some help. I need to insert the the value if its present inside the user input array using PHP and MySQL. I am explaining my table below.
db_images:
id image subcat_id
Here I need to insert into above table as the following json array value.
$subcat=array(array("id"=>63),array("id"=>64));
$imageArr=array(array("image"=>"abc.png","id"=>63));
Here I need to match both array if any value from $subcat array is present inside the second (i.e-$imageArr) array then the resepective image will insert into the table and if not present the the blank image value will insert with the respective subcat_id . Please help.
For every element in the subcat array, you can iterate on the imageArr and check if the ids match (nested loop), like this:
foreach($subcat as $s) {
$flag = false;
foreach($imageArr as $i) {
if ($s['id'] == $i['id']) {
// insert ($i['image'], $s['id']) into db
$flag = true;
break;
}
}
if ($flag == false) {
// insert $s['id'] into db
}
}
Hi you can even do in the following way with the help of array_column and in_array with loop reduced.
<?php
$subcat=array(array("id"=>63),array("id"=>64));
$imageArr=array(array("image"=>"abc.png","id"=>63), array("image"=>"abc.png","id"=>65));
foreach($imageArr as $image){
/* array_column will do the job with in_array to search in the multi dimension array */
if(in_array($image['id'], array_column($subcat, 'id'))){
echo 'exists'. $image['id'].'<br>';
}else{
echo 'not exists'. $image['id'].'<br>';
}
}

I want a query result using codeigniter

i have an array result. i want to print 2 rows(product data) in first page.
next 2 rows in second page and so on. if anybody knows this,please help me to solve it
my array
$data['product_list']
foreach($data['product_list'] as $dat)
{
echo $dat->prd_id;
echo $dat->prd_name;
}
You are doing a foreach loop on an associative array and then trying to access the the contents as objects by using ->. I can only given assumption of what you might be doing. If your array is already populated with a name and id like you have described in your foreach loop this is how you would access the contents in the loop:
foreach($data['product_list'] as $dat)
{
echo $dat['prd_id'];
echo $dat['prd_name'];
}
That is how you would print out the contents providing you had the data stored in your array like so:
$data['product_list'][0] = array('prd_id'=>'id0','prd_name'=>'name0');
$data['product_list'][1] = array('prd_id'=>'id1','prd_name'=>'name1');
$data['product_list'][2] = array('prd_id'=>'id2','prd_name'=>'name2');
Better you try with array_slice();
<?php
$a=array("red","green","blue","yellow","brown");
print_r(array_slice($a,2));
?>

Get out of loop

I have such rows in my table in MySQL ( I wont post all the data which is in "table1", because I use only this) :
From this table I get each value with foreach loop and group it by "time_from" ascending order, because I need to start from the lowest one.
With echo I print such table in html :
I add the values to the table by checking the "time_from" database with those values that are printed with for loop. The problem is that when I don't know how to stop the loop at the exact time so it should start not from the beginning but rowwhere it stopped.
foreach ($mydbdata as $row) { //only as example,because I get all the data normally,checked everything with echo to get sure.
for($time = 0; $time <= 24; $time++)
{
if($row['time_from'] == $time) {
echo '<td>OK</td>';
}
else {'<td>BAD</td>';}
}
}
The problem is that it prints all the row, adds data corresponding to if clause, but when it goes to second check it prints again all the cells in the same row. I need somehow to stop the loop and get back,but start not from the start, but from that value on which it stopped. Any ideas how to do that? I have tried with "break;" but anyway, it is not starting from the place I need.

php - find next row element value in array

I am returning an array of values using mysql_fetch_array.
In the while loop, i need to perform an if condition on the next element of the array.
E.g
if next($row) == 'test'
{
...
}
The thing is 'next($row)' is returning me the index of the next element in the array. I would like to test for the next $row("name").
if next($row("name")) == 'test'
{
...
} //this code is incorrect
Is there a way of doing this in php?
Thanks a lot for your help :)
if the "next($row)" gives you the index of the next cell in the array then just use it to perform the test. $arr[next($row)] = the next cell value/obj/whatever you have there
foreach ($rows as $k => $row) {
if (isset($rows[$k+1]) && $rows[$k+1] == 'test') //do something
// Do normal stuff here
}

Having an odd problem with mysql_fetch_array()

Why am I not able to get inside the while loop in the getCustomers() function?
$stores = $bl->getStoresForGuide($gID); //Returns 6 stores
$storelist = getStoreList($stores); //Generate the HTML for the store list
$brandlist = getCustomers($stores); //Generate the HTML for brand list
function getStoreList($stores)
{
while ($row = mysql_fetch_array($stores)) {
// Do stuff
}
//return result
}
function getCustomers($stores)
{
echo mysql_num_rows($stores); //Outputs 6
while ($row = mysql_fetch_array($stores)) {
echo "test "; // Outputs nothing
}
// Return some result
}
You're looping twice. The first time you loop, you get to the end, and the pointer isn't reset before you loop again.
If you're sure you want to do this, check out mysql_data_seek, and seek to the beginning of the result. Otherwise, I'd recommend just storing the results and iterating over the array.
You are calling getStoreList first, then by the time you call getCustomers, $stores has already had all its rows fetched.

Categories