Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 5 years ago.
Improve this question
I have array with numbers and variable number:
$a = array(1,2,3,4,5,6,7,8,9,10);
$v = 5;
I need result all numbers if is higher as $v:
$result = ?? //array(6,7,8,9,10)
<?php
$a = array(1,2,3,4,5,6,7,8,9,10);
$v = 5;
foreach($a as $value){
if($value > $v ){
$new[] = $value;
}
}
echo "<pre>";
print_r($new);
Related
Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 2 years ago.
Improve this question
Please can anybody re-write this function for me for php 7.4
private function flattenArray($array)
{
$objTmp = (object)['aFlat' => []];
array_walk_recursive($array, create_function('&$v, $k, &$t', '$t->aFlat[] = $v;'), $objTmp);
return $objTmp->aFlat;
}
I finally got it..
private function flattenArray($array)
{
$objTmp = (object)['aFlat' => []];
array_walk_recursive($array, function(&$v, $k, &$t){$t->aFlat[] = $v;}, $objTmp);
return $objTmp->aFlat;
}
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 4 years ago.
Improve this question
this is my array:
$a = [1,2,3,4,5];
Processed format:
1,2
1,3
1,4
1,5
2,3
2,4
2,5
3,4
3,5
4,5
The two elements are a group. If only the last element is left, it ends.
Like so:
$a = [1,2,3,4,5];
foreach($a as $key => $value)
{
for($i = $key + 1; $i < count($a); $i++)
{
echo $value.",".$a[$i]."\n";
}
}
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 5 years ago.
Improve this question
For example i have the PHP code like this
if ($rating_count == $rating_notnull) {
$returnValue = 1;
}
if($project_count == $project_notnull) {
$returnValue = 2;
}
Now,I want to store two variable values into one single variable?
Arrays can be used to hold multiple values. They can have a key or not.
<?php
$x = array();
$x[] = 'Some Value';
$x[] = 'Another value';
var_dump($x);
$x = array();
$x['name'] = 'Anil';
$x['other name'] = 'Del Boy';
var_dump($x);
$x = array(
'Name' => 'Batman',
'Status' => 'Busy',
'etc' => 'etc',
);
var_dump($x);
Have a play with it here: https://3v4l.org/LCjtY
As explained in other comments the most efficient way would to be to use an array to store these values.
However, if you do wish to store this in a single variable you could use explode.
This will output it in an array when it is exploding however allows you to store it into one variable
$str = "Hello, this, is, a, variable";
explode(",", $str);
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 9 years ago.
Improve this question
Can you help me with a regular expression for this:
$value = '["number"]';
or
$value = '["number","number"]';
or
$value = '["number","number","number"]';
or
...
...
...
...
$value = '["number","number","number","number","number","number","number"......,"number"]';
//$number can be rand(0, 99.....999);
I need the result to be validated as true or false!
Here is my version:
function test($v)
{
if (preg_match('/^\\[("number")(,"number")*\\]$/', $v))
echo 'ok<br>';
else
echo 'fail<br>';
}
or if "number" is really digits, this one:
function test($v)
{
if (preg_match('/^\\[("[0-9]+")(,"[0-9]+")*\\]$/', $v))
echo 'ok<br>';
else
echo 'fail<br>';
}
NOTE - only positive naturals are accepted, need to change to negative and decimal/floating numbers
Do you really need to use PCRE? Your example is valid json.
$array = json_decode('["number", "number", "123"]');
var_dump($array);
If you need numbers only, you can filter it.
$new_array = array_filter($array, 'ctype_digit');
$result = count($array) == $new_array? $new_array : null;
var_dump($result);
Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 8 years ago.
Improve this question
I have an array and print_r won't display the raw text, how can I print all the values in an array (e.g. pie)
So many ways to do it...
foreach ($array as $item) {
echo $item;
}
echo join(', ', $array);
array_walk($array, create_function('$a', 'echo $a;'));
Maybe you just need some <pre> tags:
echo '<pre>';
print_r($arr);
echo '</pre>';
<?php
$len=count($pie);
for ($i=0;$i<$len;$i++)
echo $pie[$i];
?>