Loading and summing data from array into a variable [duplicate] - php

This question already has answers here:
How can I add all of my array values together in PHP?
(4 answers)
Closed 3 years ago.
I have an array and I want to sum up the values of the array such as [0][1][2] and assign the result of the sum to a variable
My array
Array ( [0] => 1 [1] => 2 [2] => 0 )
Can anyone provide me a solution that would be really helpful?

Use php array_sum() function.
$val = array_sum($yourArray);
It will sum up all of your array elements into a variable.

Related

Explode by dashed in php [duplicate]

This question already has answers here:
split a string into two based on a substring with all combinations
(2 answers)
Closed 7 months ago.
hello everyone i am getting value like Array ( [2] => FAB(CBY-DJ9)--9 ), i want two separate value like this FAB(CBY-DJ9) and 9 by loop, kindly help me i am a beginner.
hello everyone i am getting value like Array ( [2] => FAB(CBY-DJ9)--9 ), i want two separate value like this FAB(CBY-DJ9) and 9 by loop, kindly help me i am a beginner.hello everyone i am getting value like Array ( [2] => FAB(CBY-DJ9)--9 ), i want two separate value like this FAB(CBY-DJ9) and 9 by loop, kindly help me i am a beginner
With explode() you can seperate them.
$item = 'FAB(CBY-DJ9)--9';
list($code, $number) = explode('--', $item);

How to get first key that is dynamic in multidimentional array in php? [duplicate]

This question already has answers here:
Get first key in a (possibly) associative array?
(25 answers)
Closed 2 years ago.
I have a multidimensional array and I want the first key that is
dynamic. How do I get that in PHP?
I want to display the customer name in frontend. But as you can see in the first
array there is the number [12345] => account number (the dynamic part). So if i
want to display customer name how can I get this with this dynamic
account number.
Array
(
[12345] => Array(
['customername'] => ABC
['customerid'] => 456
)
)
Try with Foreach
foreach($array as $value){
echo $value['customername'].'<br>';
echo $value['customerid'].'<br>';
}

How can I unset many values in array [php] [duplicate]

This question already has answers here:
Better way to unset multiple array elements
(6 answers)
Closed 4 years ago.
I have an array of this form :
Array
(
[1] => A
[2] => B
[3] => C
)
I want to delete values where key = 1 and 2...
I have tried this
unset($array['1']);
unset($array['2']);
But I need to call 2 times unset... It's possible without 2 calls ?
Thanks, have a good day!
You can try like
$keyToRemove = array('1', '2');
foreach($keyToRemove as $key) {
unset($arr[$key]);
}
Also you can do it like this way too
$arr = array_diff_key($arr, array_flip($keyToRemove));
Similar answer you can check it here

php unset array element while keeping numbers in order [duplicate]

This question already has answers here:
How to reindex an array?
(6 answers)
Closed 7 years ago.
I apologize in advance for this question - I'm sure it's been answered before, (hell, I've done it before), I just can't find anything in the search and my brain is drawing a blank...
I have an array in PHP where print_r outputs:
Array
(
[0] => VALUE
[1] => VALUE
[2] => VALUE
[3] => VALUE
[4] => VALUE
)
I use unset($_SESSION['item'][$lineID]); to unset a particular line from the array, and I'm left with:
Array
(
[0] => VALUE
[1] => VALUE
[3] => VALUE
[4] => VALUE
)
Element [2] has been removed, so the resulting array is no longer numerically in order
(e.g. - [0][1][3][4] instead of [0][1][2][3] ).
What PHP command do I run on the resulting array to "take out blanks" and reformat it so that it is numerically in order from 1 to n
To reset array keys after unset()
$array = array_values($array);

Random selection from sub array [duplicate]

This question already has answers here:
Selecting a random element from a PHP associative array
(4 answers)
Closed 8 years ago.
I need to select a random item (img1, img2, etc.) from simple nested arrays. I'm sure this is easy but I am stumped. The array has this format:
Array
(
[0] => Array
(
[homepage_image] => img1
)
[1] => Array
(
[homepage_image] => img2
)
)
$fields is the name of the main array.
I've tried using:
$random = array_rand($fields);
But of course that just gives me 0 or 1. How do I randomly get img1, img2, etc?
Use array_rand() to find a random key of your array:
$image = $fields[array_rand($fields)]['homepage_image'];

Categories