Find Equal Array php [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 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
)

Related

PHP Add all Values in Array Excluding Last [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 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);

Convert result to a array [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 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];

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

Subtracting two array values 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 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

How can i show array without key [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 9 years ago.
Improve this question
i want show array without []=> mycod is
foreach ($results as $value=>$value2){ //for array $ resullt is array
$array2=print_r($value2);
return true;
}
Array ( [0] => test [1] => test [2] => test [3] => test [4] => test)
Try this:
echo implode("", $array);
Don't use print_r then, or var_dump for that matter. Just set $array2 = $value2;
To only echo the values you can do this:
foreach ($results as $key=>$value){ //for array $ resullt is array
echo $value . "<br />";
}
you can not have array without keys.
you can access array elements by looping through it and can create formatted output. like this:
$strOutput = "";
foreach ($results as $value=>$value2){
$strOutput .= $value2 ." ";
}
echo $strOutput;

Categories