Php sum array std class object [closed] - php

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.

Related

How to deal with the elements in the array in the following format? [closed]

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";
}
}

get all values from arraylist in php [closed]

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 7 years ago.
Improve this question
I have arraylist like : [1, 2, 3, 4, 5];
How can I get each of these values in php?
Is there any function ? I tried array() but it did not return anything.I really appreciate any help.
Let say your $array = [1,2,3,4,5];
To get the values:
foreach($array as $values):
echo $values.'<br/>';
endforeach;
Or if you want based on elements, you can get by this way:
//To get value 1
echo $array[0];
// 0 being here the index of your array
// so echo $array[0] will output 1 since it's the first key of your array
If you have an array like this
$numbers = array(1,2,3,4,5);
with five values (i.e indexed 0 - 4) you can print out all values in the array using the print_r() function like this:
print_r($numbers);
Or you can loop through every element in the array with any PHP looping statement like FOR STATEMENTS, FOREACH STATEMENTS, WHILE STATEMENTS..
e.g
foreach($numbers as $num){
echo $num;
echo '<br/>';
}
You can also get a single value from the array using the index for example my array has 5 values (indexed from 0 - 4) if i want to print out the number '3' i would write
echo $numbers[2];

How to split an array value? [closed]

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 ;-)

Find Equal Array php [closed]

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
i have associate array like this :
$json_data = array();
$jason_data[]= array ('id'=>'1','brand'=>'chanel','name'=>'red');
$jason_data[]= array ('id'=>'3','brand'=>'lacoste','name'=>'green');
$jason_data[]= array ('id'=>'1','brand'=>'chanel','name'=>'red');
$jason_data[0] and $jason_data[2] are Equal
i want find in $jason_data for equal array and echo them
I don't know whats the context of this and your question is vague, but anyway, I hope this is what you want to achieve. (and your variable naming is odd). In your example index zero and two are the same, and since they are multi-dimensional, you can flatten them in a way by using serialize. Try this:
$json_data = array();
$json_data[]= array ('id'=>'1','brand'=>'chanel','name'=>'red');
$json_data[]= array ('id'=>'3','brand'=>'lacoste','name'=>'green');
$json_data[]= array ('id'=>'1','brand'=>'chanel','name'=>'red');
$json_data = array_map('serialize', $json_data);
$values = array_count_values($json_data);
echo '<pre>';
foreach($values as $array => $count) {
if($count > 1) {
$array = unserialize($array);
print_r($array);
}
}
Should output/print the ones that has duplicates:
Array
(
[id] => 1
[brand] => chanel
[name] => red
)

insert array of new item in any position in another array [closed]

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
How can I add new item to array? For example into middle of an array ?Should we use array splice or an array merge ?
Could you explain me the difference between both function ?
Say I have
$a1=array("a"=>"Horse","b"=>"Dog","c"=>"Cow",);
$a2=array("d"=>"Cat");
Now I need to add $a2 in 2 position .
Which one should I use ?
You can use array_splice, except that won't keep your keys.
$a1 = array("a"=>"Horse", "b"=>"Dog", "c"=>"Cow");
$a2 = array("d"=>"Cat");
array_splice($a1, 2, 0, $a2);
// $a1 is now: array("a"=>"Horse", "b"=>"Dog", 0=>"Cat", "c"=>"Cow");
If you want Cat to have a key of d, you can use a mix of array_slice and the array union operator (+):
$a1 = array_slice($a1, 0, 2) + $a2 + array_slice($a1, 2);
// $a1 is now: array("a"=>"Horse", "b"=>"Dog", "d"=>"Cat", "c"=>"Cow");
you can use array_push to add array at any position. ..array_splice can also be used. .
example: array_splice

Categories