Why can't I loop through an array? - php

I get these numbers seperated by commas from a textarea but I get an error when I try to loop through them. How do I do it? This is my code:
$numbers = $_GET['numbers'];
foreach($numbers as $number){
echo $number;
}

You should first make an array out of $numbers. You can do this by adding this line:
$numbers = explode(',', $_GET['numbers']);
Then, before you use them in the foreach loop you should use trim() to remove whitespace from the start and end:
foreach($numbers as $number){
$number = trim($number);
echo $number
}

If $_GET['numbers'] is a comma-separated list, it's not an array.
foreach(explode(",",$_GET['numbers']) as $number)

Related

PHP remove values below a given value in a "|"-separated string

I have this value:
$numbers= "|800027|800036|800079|800097|800134|800215|800317|800341|800389"
And I want to remove the values below 800130 including the starting "|". I guess it is possible, but I can not find any examples anywhere. If anyone can point me to the right direction I would be thankful.
You could split the input string on pipe, then remove all array elements which, when cast to numbers, are less than 800130. Then, recombine to a pipe delimited string.
$input= "|800027|800036|800079|800097|800134|800215|800317|800341|800389";
$input = ltrim($input, '|');
$numbers = explode("|", $input);
$array = [];
foreach ($numbers as $number) {
if ($number >= 800130) array_push($array, $number);
}
$output = implode("|", $array);
echo "|" . $output;
This prints:
|800134|800215|800317|800341|800389
This should work as well:
$numbers= "|800027|800036|800079|800097|800134|800215|800317|800341|800389";
function my_filter($value) {
return ($value >= "800130");
}
$x = explode("|", $numbers); // Convert to array
$y = array_filter($x, "my_filter"); // Filter out elements
$z = implode("|", $y); // Convert to string again
echo $z;
Note that it's not necessary to have different variables (x,y,z). It's just there to make it a little bit easier to follow the code :)
PHP has a built in function preg_replace_callback which takes a regular expression - in your case \|(\d+) - and applies a callback function to the matched values. Which means you can do this with a simple comparison of each matched value...
$numbers= "|800027|800036|800079|800097|800134|800215|800317|800341|800389";
echo preg_replace_callback("/\|(\d+)/", function($match){
return $match[1] < 800130 ? "" : $match[0];
}, $numbers);
Use explode and implode functions and delete the values that are less than 80031:
$numbers= "|800027|800036|800079|800097|800134|800215|800317|800341|800389";
$values = explode("|", $numbers);
for ($i=1;$i<sizeof($values);$i++) {
if (intval($values[$i])<800130) {
unset($values[$i]);
}
}
// Notice I didn't start the $i index from 0 in the for loop above because the string is starting with "|", the first index value for explode is ""
// If you will not do this, you will get "|" in the end in the resulting string, instead of start.
$result = implode("|", $values);
echo $result;
It will print:
|800134|800215|800317|800341|800389
You can split them with a regex and then filter the array.
$numbers= "|800027|800036|800079|800097|800134|800215|800317|800341|800389";
$below = '|'.join('|', array_filter(preg_split('/\|/', $numbers, -1, PREG_SPLIT_NO_EMPTY), fn($n) => $n < 800130));
|800027|800036|800079|800097

Split a string using foreach?

Is it possible to split a string using foreach?
For example my string value is 10
using foreach echos 1,
then echo 2,
then echo 3,
and so on to till it reach the string value.
Yes you can but looking at what you try to achieve, you really don't need to split instead you are trying to increment using loop till the string value reached. In PHP you can cast string to integer and by default PHP will do that for you if you are trying to perform + - * % / operations.
Using Foreach
$string = '10';
$numbers = range(1, $string);
foreach($numbers as $number){
echo $number;
}
Using For
$string = '10';
for($i = 1; $i <= $string; $i++){
echo $i;
}
If splitting string is what your objective, you can use str_split() function and loop like the above foreach()

Can't do implode method to delimite number range

I have a range of number, say that it from 1 to 10.
//$front_id = 1;
//$until_id = 10;
foreach (range($front_id, $until_id) as $number)
{
print_r($number)
//12345678910
}
It print a range of number between two variables as expected.
However, in the print result is it combine all number without any delimiter.
So, I tried to add a delimiter of comma here:
$numbers = implode(',', $number);
But it doesn't work.
Message: implode(): Invalid arguments passed
You can store $number in array. Right now $number is not an array. That why you cannot implode. Define array first. Then inside foreach() you can store every $number in $numbers array.
$front_id = 1;
$until_id = 10;
$numbers = array();
foreach (range($front_id, $until_id) as $number)
{
$numbers[] = $number;
}
echo implode(',',$numbers);

Count how many char in array numbers

I've an array got with preg_match_all.
This array contain 4 values and each value is a number.
I want to display ONLY the value that have 10 or more character.
Example:
Array_Value1 = 1234567890
Array_Value2 = 01234
Array_Value3 = 449125
Array_Value4 = 991234581210
I want to show with an echo only Array_Value1 and Array_Value4 because formed of 10 or more characters!
How can I do it?
I tried with count_char or str_len but I see a message that say "Array to String conversion".
Anyone can help me?
first of all you have to put all your values inside an array:
$array = array(1234567890, 01234, 449125, 991234581210);
after it you can use a simple foreach:
foreach($array as $value) {
if(strlen((string)$value) >= 10)
echo $value;
}
you should add a separator after each echo, or you will have an output like
1234567890991234581210
foreach ($values as $value)
{
if (strlen((string)$value) >= 10)
echo $value;
}
Try this-
foreach($my_arr as $val)
{
if(strlen((string)$val) >=10)
// more than or equal to 10
else
// less than 10
}
Try this:
<?php
$values = array(123123123, 53453453452345, 123, 9979797979797979);
$string = implode(',',$values);
preg_match_all('/(\d{10,})/', $string, $matches);
print_r($matches[1]);
?>
Maybe it's more time-consuming... hehe

is it possible to use regex to search inside an array using php

I have an array of string i need to search an string inside the array using regex is it possible if so please explain..
$a = preg_grep("/search_word/",$array_of_strings);
print_r($a);
You can use a foreach loop to loop through all the elements and use a preg_match on each of them. If it matches, add it to an array of matches.
foreach($array as $check) {
if (preg_match("/expression/", $check)) $matches[] = $check;
}
Very simple example.
You can iterate through the array using a foreach loop and search for the key in each element. An example:
<?php
$days = array('Sunday','Monday','Tuesday');
$key = "Sunday";
foreach($days as $day) {
if(preg_match("/$key/",$day)) {
echo "Key $key found !!";
}
}
?>

Categories