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];
?>
Related
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 11 days ago.
Improve this question
I have print_r array like this. How do I SUM field 'credit' ?
I want to result like this in html :
650000
500000
500000
500000
200000
you can create funtion to sum your array
function sum_credit($array){
$sum = 0;
foreach($array as $val){
$sum += $val->credit;
}
return $sum;
}
and print your data with
echo sum_credit($array);
$creditSum = array_sum(array_column($array,'credit'));
With array_column, a one-dimensional array is created from 'credit' values and then the sum is calculated with array_sum.
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 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 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
In my array I have these values:
[10d, 10e] => 4
The values are retrieved from another website and the important values will be put in this array.
Now I want to have two single values from these like:
[10d] => 2 [10e] => 2
How can I do this?
Now I'm guessing a little because of the description that is not that clear.
$arr = array('10d, 10e' => 4);
$newArr = array();
foreach($arr as $key => $value) {
$newKeys = explode(',', $key);
foreach($newKeys as $item) {
$newArr[trim($item)] = $value / count($newKeys);
}
}
print_r($newArr);
Result
[10d] => 2 [10e] => 2
Some functions you can use:
foreach() to loop the input array
explode() to split the key
trim() to remove whitespace
count() to, well, count array items
And, of course, / to divide ;-)
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);