PHP: How to loop through properties with sequential names - php

If I have a table with columns called image1, image2, image3, etc and I pulled those in an object-oriented manner, like this:
$images->image1
$images->image2
$images->image3
etc.
Is there any way to process these in a loop? Something like:
for (i=1; i<count($images); $i++) {
$array[$i] = $images->image?
}
I realize the above won't work, but was wondering if someone could tell me a way that will work. Thanks.

Pulling them from the DB in an array might be a better option, but you can do it with objects by using variable variables:
for ($i = 1; i < $length; $i++) {
$array[] = $images->{'image' . $i};
}

You should use an array instead.
However, you can also use variable variables for this madder.

for ($i = 1; i < $len; $i++) {
$array[] = $images->{'image' + $i};
}

Related

PHP skip for loop Iteration based on a query result set

Lets say for example I have a result set from a MySQL query that has produced values: 3,5,10,11 and so forth...
I would like to iterate through a for loop in PHP but exclude the any iteration that would equal any number in results of my MySQL query.
As it stands I currently have:
for($i = 1; $i <= $num_rows; $i++)
{
if($i == 3 or $i == 5)
{
continue;
}
//Rest of loop...
As you will all appreciate hard coding these values is very time consuming and not very efficient. If any one could help it would be greatly appreciated.
If you can gather automatically those values you are currently hard coding, you can use in_array($search, $array[, $strict])
Like this :
$bypass = array(3, 5);
for($i = 1; $i <= $num_rows; $i++)
{
if( in_array($i, $bypass) )
{
continue;
}
// rest of the loop
}
PS : I prefer much more the "Dont Panic" answer, which doesn't use too many loops. (in_array will loop through the array to find your value). See his answer : https://stackoverflow.com/a/38880057/3799829
If your query results are returned as an array you can use
if(in_array($i, $results)) to do a check of $i against the results
Add your values into an array while you fetch your query results. I used PDO here for example, but you should be able to adapt it to whichever database extension you're using:
while ($row = $stmt->fetchObject()) {
$exclude[$row->value] = true;
// whatever else you're doing with the query results
}
If you use the values as keys in the array of things you want to skip, checking for them in your for loop should be more efficient than using in_array.
for($i = 1; $i <= $num_rows; $i++) {
if (isset($exclude[$i])) continue;
// rest of loop

PHP - for loops through associative array

I have an associative array $_POST, which has 3 key value pairs (There are other key value pairs which I am not interested in).
$_POST[Var1]
$_POST[Var2]
$_POST[Var3]
How do I use a for loop to loop through and echo the values in each?
for ($i = 1; $i <= 3; $i++){
echo $_POST['Var' . '$i'];
}
This does not seem to work.
Get rid of the single quotes around $i as that makes it a literal string and your variable is not interpolated:
for ($i = 1; $i <= 3; $i++){
echo $_POST['Var' . $i];
}
This is basic PHP. I strongly recommend reading the manual to learn more about the fundamentals of PHP.
Check this. While using php varibles dont put upper commas for the variables.
<?php
$_POST["file1"];
$_POST["file2"];
$_POST["file3"];
for ($i = 1; $i <= 3; $i++){
echo $_POST['file'.$i];
}
Or this way:
$_POST["Var{$i}"];

How to know the possible sets of n numbers?

I have a set of numbers S = [1,2,3,4,5,6]; And I want to make all possible pairs among them but one thing to know the pair could not be repeat.
i have used this :-
$pairs = array('1','2','3','4','5','6');
$count = count($pairs);
$array = array();
for($i = 0;$i <= $count; $i++){
for($j = 1; $j < $count; $j++){
if($i < $j){
$array[$i][] = $pairs[$i].','.$pairs[$j];
}
}
}
I want some thing like this :-
S = [1,2,3,4,5,6];
[1,2],[1,3],[1,4],[1,5],[1,6]
[2,3],[2,4],[2,5],[2,6]
[3,4],[3,5],[3,6]
[4,5],[4,6]
[5,6]
If anyone have any better suggestion please do reply as soon as possible.
Build a double nested foreach loop over your array, build your pairs and check if you already got them.
Pseudocode:
pairs = []
foreach(entry in array) {
foreach(entry2 in array) {
pair = [entry, entry2]
if(!alreadyexists(pair in pairs) {
add(pair to pairs)
}
}
}
havefun()
Btw: You won't get hurt if you try something yourself, this problem is rather trivial.

cannot put msSQL column names into array

I'm trying to put the column names of a msSQL table into an array. Using
for ($i = 0; $i < mssql_num_fields($result); ++$i) {
echo mssql_field_name($result) . "<br><br>";
}
the column names print to the screen just fine. Also get_type() shows that they are strings. However, when I try to put them into an array like this:
$column_names = array();
for ($i = 0; $i < mssql_num_fields($result); ++$i) {
$current_column = mssql_field_name($result);
array_push($column_names, $current_column);
}
var_dump($column_names); gives me an array (albeit the expected length) of boolean values. All false. I would expect to see an array containing the names of all my columns. What am I doing wrong here? Thank you
Looks like you are missing the $i argument on the mssql_field_name call. Try this maybe:
$column_names = array();
for ($i = 0; $i < mssql_num_fields($result); ++$i) {
$column_names[] = mssql_field_name($result, $i);
}
http://php.net/manual/en/function.mssql-field-name.php

How do I create a parameter list for a function?

I am creating an array of arrays in the following way:
$final_array = array();
for($i = 0; $i < count($elements); $i++) {
for($j = 0; $j < count($elements); $j++) {
if($i!=$j)
$final_array[] = array_intersect($elements[$i], $elements[$j]);
}
}
I am trying to find out the list of elements that occur in all the arrays inside the $final_array variable. So I was wondering how to pass this to array_intersect function. Can someone tell me how to construct args using $final_array[0], $final_array[1], ... $final_array[end_value] for array_intersect? Or if there is a better approach for this, that would be great too.
I am looking for a way to construct the following:
array_intersect($final_array[0], $final_array[1], $final_array[2], ...)
Why do you need to do all of this work? Just use call_user_func_array.

Categories