I'm pretty new to PHP and want to write a "for" statement based on IDs of a page. The problem is that the IDs are not sequential (by that I mean they are not increasing by 1 each time ie. 1,2,3,4,5). Is there a way to use "for" to use specific numbers? For example (pseudocode)
for IDnumbers i = (1,5,7,23,28,34)
echo "function1(i)"
echo "function2(i)"
end
I hope that makes sense. I basically want functions associated with each post ID to be returned, but I want the IDnumbers to be specific. Sorry if this is a basic question!
edit: wow, that really was basic. Thanks guys!
// create an array
$numbers = array(1,5,7,23,28,34);
// loop over it
foreach($numbers as $number){
echo function1($number);
echo function2($number);
}
foreach (array(1,5,7,23,28,34) as $n) {
echo "function1($n)";
echo "function2($n)";
}
foreach( Array(1,5,7,23,28,34) as $i) {
// do stuff with $i
}
Store the numbers in an array and use a foreach.
$IDNumbers = array(1,5,7,23,28,34);
foreach($nums as $num)
{
echo function1($num);
echo function2($num);
}
Read more about php foreach here.
try something like this:
IDnumbers = array(1,5,7,23,28,34)
foreach IDnumbers as id
echo "function1(id)"
echo "function2(id)"
end
IF you MUST use FOR, then you can do this:
$IDNumbers = (1,5,7,23,28,34);
for($i = 0; $i < count($IDNumbers); $i++):
echo function1($IDNumbers[$i]);
endfor;
This is because even though you stored a number as the value in a numeric array, it still orders it sequentially in reference to the identifiers and where they were added. The value may be 7, but its pointer is 2 (0, 1, 2, 3, 4, 5 in this case)
Related
Given that I have the following phone number:
9904773779
I would like to search my data base for other phone numbers which have a least 5 digits in common with the above number, like those:
9933723989
9403793378
My first though was to use a query like this:
select mobileno from tbl_registration where mobileno like '%MyTextBox Value%'
however, this didn't not work.
I think a tidier PHP solution here is using similar_text:
This calculates the similarity between two strings.
Sample demo:
$numbers = array("1234567890", "9933723989", "9403793378");
$key = "9904773779";
foreach ($numbers as $k) {
if (similar_text($key, $k) >= 5) { // There must be 5+ similarities
echo $k . PHP_EOL;
}
}
Output: [ 9933723989, 9403793378]
See IDEONE demo
I think I would go with PHP, although not very tidy and there is probably a better way.
<?php
$strOne = '9904773779';
$strTwo = '9933723989';
$arrOne = str_split($strOne);
$arrTwo = str_split($strTwo);
$arrIntersection = array_intersect($arrOne,$arrTwo);
$count=0;
foreach ($arrIntersection as $key => $value) {
if ($arrOne[$key] === $arrTwo[$key]) {
$count++;
}
}
print_r($count);
?>
In the first stage I split the strings to arrays. I then use array_intersect to identify duplicate values and save them into an array. this saves having to loop through every number. I then loop thru the array of identical values and compare both arrays to see if the values are identical.
I do however look forward to a cooler answer.
I am still learning PHP and have a bit of an odd item that I haven't been able to find an answer to as of yet. I have a multidimensional array of ranges and I need to echo out the values for only the first & third set of ranges but not the second. I'm having a hard time just finding a way to echo out the range values themselves. Here is my code so far:
$array = array(
1=>range(1,4),
2=>range(1,4),
3=>range(1,4)
);
foreach(range(1,4) as $x)
{
echo $x;
}
Now I know that my foreach loop doesn't even reference my $array so that is issue #1 but I can't seem to find a way to reference the $array in the loop and have it iterate through the values. Then I need to figure out how to just do sets 1 & 3 from the $array. Any help would be appreciated!
Thanks.
Since you don't want to show the range on 2nd index, You could try
<?php
$array = array(
1=>range(1,4),
2=>range(1,4),
3=>range(1,4)
);
foreach($array as $i=>$range)
{
if($i!=2)
{
foreach($range as $value)
{
echo $value;
}
}
}
?>
Note: It's not really cool to name variables same as language objects. $array is not really an advisable name, but since you had it named that way, I didn't change it to avoid confusion
Do you want to do this?
foreach ($array as $item) {
echo $item;
}
You can use either print_r($array) or var_dump($array).
The second one is better because it structures the output, but if the array is big - it will not show all of it content. In this case use the first one - it will "echo" the whole array but in one row and the result is not easy readable.
Range is just an array of indexes, so it's up to you how to your want to represent it, you might want to print the first and the last index:
function print_range($range)
{
echo $range[0] . " .. " . $range[count($range) - 1] . "\n";
}
To pick the first and the third range, reference them explicitly:
print_range($array[1]);
print_range($array[3]);
here is one solution:
$array = array(
1=>range(1,4),
2=>range(1,4),
3=>range(1,4)
);
$i=0;
foreach($array as $y)
{ $i++;
echo "<br/>";
if($i!=0){ foreach($y as $x)
{
echo $x;
}
}
}
$index = 0;
foreach ($sxml->entry as $entry) {
$array + variable index number here = array('title' => $title);
$index++;
}
I'm trying to change an array name depending on my index count. Is it possible to change variable name (ie. $array1, $array2 $array3 etc.) in the loop?
Edit:
After the loop has finished, I will generate a number number (depending on the count of $index) and then use this array... probably it's a stupid way of accomplishing what Im trying to do, but I don't have a better idea.
You might want to try this instead:
$index = 0;
$arrays = array();
foreach ($sxml->entry as $entry) {
$arrays[$index] = array('title' => $title);
$index++;
}
While it is technically possible to do what you are asking, using an array of arrays will probably work better from you.
This type of indexing is exactly what arrays are designed for, you have a lot of items and want to be able to refer to them by number.
Unless you have a very specific reason to use the name of the variable to represent it's number you will probably have a much simpler time using it's index in the outer array.
Yes you can user an associate array. Generating a string dynamically based on the iteration number and using that as a key in the array.
You can use variable variables. php.net
PHP supports Variable variables:
$num = 1;
$array_name = 'array' . $num;
$$array_name = array(1,2,3);
print_r($array1);
http://php.net/manual/en/language.variables.variable.php
im trying to create an array from a loop in PHP.
I want the array to end up something like $array(100,100,100,100), this is my code:
$x = 0;
while($x < 5){
$array[$x] = 100;
$x++;
}
echo $array[0];
It outputs 1 instead of 100.
Can someone tell me where i'm going wrong.
Even though it works perfectly for me, you should be initialising the variable beforehand.
$array = array();
For example, if $array is non-empty string, you will see the output of 1.
You can just use the predefined array_fill function for that:
$array = array_fill(0, 5, 100);
The other answers pretty much cover this. I would, however, recommend you use a for loop for this instead of a while loop if you're going to use a loop rather than a function to do it.
$array = array();
for($x=0; $x<5; $x++) {
$array[$x] = 100;
}
In fact, you could make this even shorter.
$array = array();
for($x=0; $x<5; $array[$x++]=100);
It's perfectly valid to have a for loop statement instead of a block. And blocks can go anywhere too; they don't need an if statement, for loop, while loop, or whatever, before them.
is there a way of finding the sizeof an array without using sizeof($array) and / or count($array)?
If you want to know the number of items in an array, you have two solutions :
Using the count() function -- that's the best idea
Looping over all items, incrementing a counter -- that's a bad idea.
For an example using the second idea :
$num = 0;
foreach ($array as $item) {
$num++;
}
echo "Num of items : $num";
But, again : bad idea !
Edit : just for fun, here's another example of looping over the array, but, this time, using array_map() and an anonymous function (requires PHP >= 5.3) :
$array = array(1, 2, 3, 4, 5);
$count = 0;
array_map(function ($item) use (& $count) {
$count++;
}, $array);
echo "Num of items : $count";
Here, too, bad idea -- even if fun ^^
You could use foreach and manually count the number of elements in the array, but I don't see why you would want to since this will provide no advantage over using either the sizeof or count functions.
Even though there is no point doing a foreach or anything else for that matter... what about array_reduce:
array_reduce($array, function($count, $element) {
return $count + 1;
}, 0);
Just for something different :D