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 3 years ago.
Improve this question
I have an array that looks like this:
Array ( [0] => 1 [1] => 1 [2] => 3 )
I'd like to get the sum of the values excluding the last value.
So in this example I'm looking to find the answer to 1 + 1.
The array could be varying lengths.
You can do like this
$myArray= [1,1,3];
array_pop($myArray); // Remove the last element from array
echo array_sum($myArray); // Sum the values of the array
You can do something like this
<?php $array = array(1, 1, 3);
$sliced = array_slice($array, 0, -1); ?>
This will return you 1 & 1 it won't give you 3
<?php $value = array(1,1,3);
$removed = array_pop($value);
echo array_sum($value);
?>
Another way to do using closure
$array = [1,1,3];
echo array_sum(array_filter($array, function($key) use ($array) {
return count($array) - 1 !== $key;
}, ARRAY_FILTER_USE_KEY));
try this its very simple & fastest way
// initialize array by 'short syntax'
$a = [
1,1,3
];
//remove last element of array no matter length
// This function will reset() the array pointer of the input array after use.
array_pop($a);
// then you can sum of rest array values by array_sum
echo array_sum($a);
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 2 years ago.
Improve this question
I have 2 arrays, I would like to use the keys from the 1st array to search for matched keys from the 2nd array and return those keys from the 2nd array
with the values of the second array.
expected result:
Array
(
'Intermediary/contract/{contract:id}/bank-accounts'[2]
'Manager/action/{action:id}/bank-bills'[2]
)
$arrayOne = [
'/Intermediary/contract//bank-accounts'[2]
'/Manager/action//bank-bills'[2]]
$arrayTwo = [
'/Intermediary/contract/{contract:id}/bank-accounts',
'/Manager/action/{action:id}/bank-bills',
]
So far I've tried, among others,
foreach ($array1 as $key => $value) {
$results = preg_grep('/$key/', $array2);
}
Those are values and not keys in your example arrays.
First get rid of all of the {.....} from $arrayTwo and compute the intersection (same entries) with $arrayOne. Since the keys are preserved in the array that has been replaced you can compute the intersection of keys with $arrayTwo:
$result = array_intersect_key(
array_intersect(preg_replace('/\{[^}]+\}/', '', $arrayTwo), $arrayOne),
$arrayTwo
);
Here's a Demo.
If you really need the keys then the answer is slightly different getting the keys as an array:
$result = array_intersect_key(
array_keys($arrayTwo),
array_intersect(preg_replace('/\{[^}]+\}/', '', array_keys($arrayTwo)),
array_keys($arrayOne))
);
Here's a Demo.
To do it with a loop:
foreach($arrayTwo as $k => $v) {
if(in_array(preg_replace('/\{[^}]+\}/', '', $k), array_keys($arrayOne))) {
$result[] = $k;
}
}
If for whatever reason you need them as keys again, there is array_flip.
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 5 years ago.
Improve this question
Hello to All i get a result from a server like this.
Id=6528537&CCode=250&Fild1=&Fild2=&Fild3=&HeshASM=0
How Can i convert it into a arary in php.
Thanks for the Help.
$query = 'Id=6528537&CCode=250&Fild1=&Fild2=&Fild3=&HeshASM=0';
parse_str($query, $output);
echo '<pre>';
print_r($output);
echo '</pre>';
/*
Array
(
[Id] => 6528537
[CCode] => 250
[Fild1] =>
[Fild2] =>
[Fild3] =>
[HeshASM] => 0
)
*/
You could try to construct the array yourself:
$string = "Id=6528537&CCode=250&Fild1=&Fild2=&Fild3=&HeshASM=0";
$explode = explode("&", $string);
$array = [];
foreach ($array as $key => $value) {
$inner_explode = explode("=", $value);
$array[$inner_explode[0]] = $inner_explode[1];
}
You would explode the string using "&" as separator, that will give you an array with the different parameters like array('id=6528537','ccode=250'...)
Then you will iterate trough that array, and explode again, this time using "=" as separator, this will give you the different parts of the different parameters, like: array('Id','6528537').
Knowing that in the position 0 of that array you will have the key, and in the position 1 the value, you simply add to your array the values in each iteration with $array[$inner_explode[0]] = $inner_explode[1];
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];
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 8 years ago.
Improve this question
Here are the snapshots of the two arrays:
Array ( [assigned_todo_count] => 382 )
Array ( [delayedtasks] => 88 )
and I have used the array_diff_asooc function for subtraction of the arrays but I am not getting the result I expected.
foreach(array_diff_assoc($assigned_todo_count,$delayed_todo_count) as $item)
{
print_r($item);
}
The final result when doing print_r outputs 382. I have to find the difference between two arrays though final output results in the first arrays result.
Try this
$array1 = array("assigned_todo_count"=>382);
$array2 = array("delayedtasks" => 88);
$subtracted = array_map(function ($x, $y) { return $y-$x; } , $array2, $array1);
$result = array_combine(array_keys($array2), $subtracted);
var_dump($result);
It will subtract array1 from array2
The question is quite confusing.
Anyways, let me assumed that Array ( [assigned_todo_count] => 382 ) is stored in $atc variable and Array ( [delayedtasks] => 88 ) is stored in $dt variable.
So what I'll just have to do is:
$difference = $atc['assigned_todo_count'] - $dt['delayedtasks'] ;
if($difference > 15){
//difference is greater than 15
}else{
//difference is less than 15
}
Why don't you use this simple method?
<?php
$array1 = array("assigned_todo_count"=>382);
$array2 = array("delayedtasks" => 88);
$result=$array1['assigned_todo_count']-$array2['delayedtasks'];
echo $result;
?>
Output
294