This question already has answers here:
Sum values in foreach loop php [closed]
(5 answers)
Closed 3 years ago.
I'm tryng to learn PHP and I'm trying to do it by doing things the long way round.
So I came across array_sum() and I was wondering if there is a way to calculate an array without using it.
for example
$my_array = array(10, 80, 30);
I've only got as far as this and I'm stumped. I have looked on google but I haven't found anything.
$implode = implode(",", $my_array );
$explode = explode(",", $implode);
foreach($explode as $test)
{
}
I will always go for native functions as they are optimized as well as tested.[so basically i want to say that use array_sum()],
Apart from above, below is foreach() process to sum all values of a single-dimensional array[just for knowledge sake]
<?php
$result = 0; //create a variable with 0 value
foreach($explode as $test)
{
$result += $test; //add array value to the newly created value
}
echo $result; //print final sum of all array values
Output:-https://3v4l.org/0qMJu
Note:- please read #Markus Zeller comment under question
Related
This question already has answers here:
Sort array not working
(2 answers)
Closed 10 months ago.
I'm trying to sort an array numerically. Here's my code
<?php
$data = '9#Saul,7#Jesse,1#Skyler,6#Walter';
$exp = explode(",",$data);
$expsort = sort($exp);
print_r($expsort);
?>
But it is not working. The output is showing only "1".
You are assigning the value of the sort function -which sorts the argument array itself, and it always returns true and thus you got 1 as a result.
So if you print your original exploded array, it will be sorted. Please note, sort overrides your original array
$data = '9#Saul,7#Jesse,1#Skyler,6#Walter';
$exp = explode(",",$data);
sort($exp);
print_r($exp);
This question already has answers here:
How to use array values as keys without loops? [duplicate]
(6 answers)
Closed 6 years ago.
Please check attached images. These image contains $complex_arr print and $simple_arr print results. I want to convert $complex_arr to $simple_arr output.
How is that possible? What is actually doing each of $complex_arr inside value will be converted as associative simple array like $simple_arr
$arr1 = array("asso1"=>"a", "asso2"=>"1");
$arr2 = array("asso1"=>"b", "asso2"=>"2");
$arr3 = array("asso1"=>"c", "asso2"=>"3");
$complex_arr = array($arr1,$arr2,$arr3);
$simple_arr = array("a"=>"1", "b"=>"2", "c"=>"3");
// print_r($complex_arr);
print_r($simple_arr);
Input:
Output:
You have to write it on our own... here is my idea:
public function makeItSimpler($arr){
$newarr = array();
foreach($arr as $complex){
$newarr[$complex['asso1']]=$complex['asso2'];
}
return $newarr;
}
Perhaps you can do it better... take look at "array-map" http://php.net/manual/de/function.array-map.php
Good Luck
foreach ($complex_arr as $key => $value) {
$simple_arr[$value['asso1']]=$simple_arr[$value['asso2']];
}
With php5.5 (where array_column becomes available) it is:
$simple_arr = array_combine(
array_column($complex_arr, 'asso1'),
array_column($complex_arr, 'asso2')
);
And even simplier (after I reread function manual):
$simple_arr = array_column($complex_arr, 'asso2', 'asso1');
This question already has answers here:
PHP: How to get all possible combinations of 1D array? [duplicate]
(1 answer)
How To Find Words Combination In Php
(1 answer)
Closed 6 years ago.
I want to get all the possible combination from an array. I know this question has been asked several times but didnot worked for me correctly .
The array input should be single dimensional array as well as the output
for example:- input
$new_array=array('c','a','t','m','p');
possible combinations such as ca,ct,cm,cp,cat,cam,cap.... and soo on...
I have tried this but didnot worked correctly .
I have taken the code from PHP CookBook
function pc_array_power_set($array) {
// initialize by adding the empty set
$results = array();
foreach ($array as $element)
foreach ($results as $combination)
array_push($results, array_merge($eleme, $combination));
return $results;
}
$set = array('c', 'a', 't','m','p');
$power_set = pc_array_power_set($set);
print_r($power_set);
I need the output in single dimensional array
This question already has answers here:
Counting occurrence of specific value in an Array with PHP [duplicate]
(6 answers)
Closed 9 years ago.
How can I count how many instances of this value inside an array?
For example:
$array = array(5,5,5,5,3,3,2,1,2,4,5,6,7);
if for example I wanted to count how many 5 are in there how can I do that?
I thought I should use count but count counts all the values inside an array and not a specific one?
I searched through stackoverflow and found no such question
http://php.net/array_count_values
Did you attempt to research this at all?
$key = 5;
$cv = array_count_values($arr);
echo $cv[$key];
That's how you get the count for ONE value.
array_count_values($array)
reference http://php.net/manual/en/function.array-count-values.php
same question at here
If you don't want to use the built-in array_count_values() function, then try this:
$array = array(5,5,5,5,3,3,2,1,2,4,5,6,7);
$testValue = 5;
$count = array_reduce(
$array,
function ($counter, $value) use ($testValue) {
$counter += $value === $testValue;
return $counter;
},
0
);
var_dump($count);
Anything you write yourself is always going to be slower than the built-in function, and requires more code, and require more effort to test, so (as everyone else has said) you really should use array_count_values()
This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
How to create comma separated list from array in PHP?
I have an array as follows;
$array = array(1,2,3,4,5);
I want to print or echo this variable as 1,2,3,4,5. What is the simplest method for this?? I printed array[0] first and then skipped first value and used foreach function to echo all remaining ",".$value.
Try following
echo implode(",", $array);
You can use the implode function.
In the example you showed, it'd be written like this:
implode(',', $array);
$array = array(1,2,3,4,5);
$result = implode(',', $array);
echo $result;