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;
}
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 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);
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
please help me.
so how to find word position in sentece.
ex : one two five six seven two.
finding : two.
answer : 2 and 6
$needle = 'two';
$positions = array_keys(
array_filter(
str_word_count($sentence, 1),
function ($value) use ($needle) {
return $value == $needle;
}
)
);
EDIT
If you really need the offset of the first word to be 1 rather than 0, then modify the above so:
$positions = array_map(
function($position) {
return ++$position;
},
array_keys(
array_filter(
str_word_count($sentence, 1),
function ($value) use ($needle) {
return $value == $needle;
}
)
)
);
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 8 years ago.
Improve this question
Let´s say I´ve got an array like [3,2,8,4] (just an example, it can have more or less values).
I want the numbers to be in the same order but instead use numbers 1-4 (if there are 4 values as in this example), ie. [2,1,4,3].
How can I accomplish this?
$data = [3,2,8,4];
$keys = array_keys($data);
array_multisort($data, SORT_ASC, $keys);
array_walk($keys, function(&$value) { ++$value; });
var_dump($keys);
,You could go with ArrayReplace.
<?php
$base = array(3 2, 8, 4);
$replacements = array(0 => 2, 1 => 1, 2 => 4, 3 => 3);
$store = array_replace($base, $replacements);
?>
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];
?>