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