Compare Two Associative Array with different value - php

I have two Associative array.
$a1 = array("Peter"=>"a","Ben"=>"b","Joe"=>"c");
$a2 = array("Peter"=>"5","Joe"=>"15");
and Out put result should be...
a=5
b=null
c=15

foreach($a1 as $aKey => $aValue) {
echo $aValue, '=';
echo (isset($a2[$aKey])) ? $a2[$aKey] : 'null';
echo PHP_EOL;
}

foreach($a1 as $key => $value) {
echo $a1[$key].'='.$a2[$key];
}
You will get the output, but this have nothing to do with array comparing.

Related

PHP Transform mixed sequential and associative array to associative array

I would like to build $goal array from $initial only. Any ideas? Thank you
Edit : the question could be how to differentiate associative parts from sequential ones.
$intial=[
"one",
"two"=>"myTwo",
"three",
"four"=>"myFour"
];
$goal=[
"one"=>null,
"two"=>"myTwo",
"three"=>null,
"four"=>"myFour"
];
The 'sequential' parts will have numeric keys, so if your 'associative' keys will always be strings, you could use that to differentiate:
$goal = [];
foreach ($initial as $key => $value) {
if (is_numeric($key)) {
$goal[$value] = null;
} else {
$goal[$key] = $value;
}
}
$goal = [];
foreach($initial as $key => $val){
if(isset($val){
$goal[$key] = $val;
}else{
$goal[$key] = $key;
}
}

Find difference value from two array in php [duplicate]

This question already has answers here:
Compare two array and get all differences
(2 answers)
Closed last year.
I want to get difference value from array and also want to compare that difference value is from which array.
First Array => Variable : $first_array
Array {607,608,609}
Second Array => Variable : $second_array
Array {607,608,609,610}
want to get output like Difference value : 610...... From Array :- $second_array
How can i get ? please help me ....
You can use array_diff()
$result=array_diff($first_array,$second_array);
print_r($result);
Try this :
$first_array=array(607,608,609);
$second_array=array(607,608,609,610);
$result=calculate_diff($second_array,$first_array);
print_r($result);
function calculate_diff($array1,$array2)
{
$diff = [];
$larger_array = $array2;
$smaller_array = $array1;
if(count($array1) > count($array2))
{
$larger_array = $array1;
$smaller_array = $array2;
}
foreach($larger_array as $ele)
{
if(!in_array($ele,$smaller_array))
{
$diff[] = $ele;
}
}
return $diff;
}
it will work
<?php
$a=Array(607,608,609,610);
$b=Array (607,608,609);
$result=array_diff($a,$b);
print_r($result);
?>
or else try this
<?php
$array2 = array(607,608,609);
$array1 = array(607,608,609,275);
foreach ($array1 as $value)
{
if(in_array($value, $array2))
{
$key = array_search($value, $array2);
$key1 = array_search($value, $array1);
unset($array2[$key]);
unset($array1[$key1]);
//echo "yes<br>";
}
}
$merge = array_merge($array1,$array2);
print_r($merge);
?>
Use array_diff
<?php
$a=Array(607,608,609,610);
$b=Array (607,608,609);
$res=array_diff($a,$b);
print_r($res); // output 610
?>
Try this:
$a = array(607,608,609,610);
$b = array(607,608,609);
$c = array_diff($a,$b);
print_r($c);
Try This:
$a1=array(607,608,609,610);
$a2=array(607,608,609);
$result=array_diff($a1,$a2);
print_r($result);
Output :- Array ( [3] => 610 )
Try this by merging them and then using array_diff, array_diff_assoc and array_unique.
https://3v4l.org/Z1vpe
$first = Array(607,608,609,610);
$second = Array(800,607,608,609);
$Fcount = count($first);
$arr = array_merge($first, $second);
$arrc= array_diff($arr, array_diff_assoc($arr, array_unique($arr)));
foreach ($arrc as $key => $value){
if($key < $Fcount){
echo "first array ". $value . "\n";
}else{
echo "second array " . $value . "\n";
}
}
Edited to add how to find which array the value is in. https://3v4l.org/W7rch
$a1=array("a"=>"red","b"=>"green","c"=>"blue","d"=>"yellow");
$a2=array("e"=>"red","f"=>"green","g"=>"blue","r"=>"black");
$d = array_merge($a1,$a2);
output:array("a"=>"red","b"=>"green","c"=>"blue","d"=>"yellow","e"=>"red","f"=>"green","g"=>"blue","r"=>"black")
$result=array_diff($d,array_intersect($a2,$a1));
print_r($result);
output:array("d"=>"yellow","r"=>"black");

Using foreach on multidimensinal arrays

I tried to use a foreach loop on a multi-dimensional array, and found out that it didn't exactly worked out the way that I expected. Is there a foreach loop for multi-dimensional arrays, or another way to do this?
$array[0][0] = "a";
$array[0][1] = "b";
$array[0][2] = "c";
foreach($array as $a) {
echo $a."<br>";
}
Result:
Nothing
Needed Result:
a
b
c
You could also try this:
foreach($array[0] as $key => $value){
echo $value . "<br>":
}
$array in this code you're accessing the key of 0,0,0 so it will not print it.
$array[0] in this code you're both accessing key 0,1,2 and the values a,b and c
You need two loops. One to loop the first array, and one to loop the inner one.
foreach($array as $key) {
foreach($key as $val) {
echo $val;
}
}
Try nesting another foreach...
$array[0][0] = "a";
$array[0][1] = "b";
$array[0][2] = "c";
foreach($array as $a) {
foreach($a as $val){
echo $val."<br>";
}
}
It is because $a is still an array. If you use print_r() you will see this:
foreach($array as $a) {
print_r($a);
}
Result:
Array
(
[0] => a
[1] => b
[2] => c
)
To combat the nested array you have to run a second foreach() loop to get the values:
foreach($array as $a) {
foreach($a as $value){ // loop through second array
echo $value . "</ br>";
}
}
well since no one else has mentioned it:
<?php
$array[0][0] = "a";
$array[0][1] = "b";
$array[0][2] = "c";
echo implode('<br>',$array[0]);
http://codepad.viper-7.com/SC9PLI

Get keys of an array where values is not NULL in PHP

I have an associative array in PHP
$a = array("d1" => "data", "d2" => NULL, "d3" => "data")
I want to get all keys and all values which are not NULL, in order to implode them:
// e.g.:
$sub_key = array_keys($a, keys != NULL);
$sub_values = array_values($a, values != NULL);
echo "`".implode("`,`", $sub_key)."`";
echo "'".implode("','", $sub_key)."'";
Are there functions like array_keys() and array_values() that allow to take only vales that do not match the pattern?
Use array_filter before using array_keys and filter the array like this
$newArray = array_filter($a);
Then do
$sub_key = array_keys($newArray);
$sub_values = array_values($newArray);
You could use array_filter($a), but as one of the comments above pointed out, this would also filter out values like FALSE, empty strings, etc. So I'd use a foreach loop.
$new_array = array();
foreach ($a as $key => $value) {
if (is_null($value) === false) {
$new_array[$key] = $value;
}
}
Please try this:
// Loop to find empty elements and
// unset the empty elements
foreach($array as $key => $value)
if(empty($value))
unset($array[$key]);
// Display the array elements
foreach($array as $key => $value)
echo ($array[$key] . "<br>");
In you case you will replace $array with $a. This will work for null/empty key values.
$a = array("d1" => "data1", "d2" => NULL, "d3" => "data3");
$b = array_filter($a); // Not Null Values Array
$sub_key = array_keys(array_filter($a));
$sub_values = array_values(array_filter($a));
echo "`".implode("`,`", $sub_key)."` <br/>";
echo "'".implode("','", $sub_values)."'";
$sub_key = array();
$sub_values = array();
foreach ($a as $key => $value) {
if (!is_null($key) && !is_null($value)) { // you can also do is_empty() in stead of is_null() if you also wan't to avoid empty string
$sub_key[] = $key;
$sub_values[] = $value; // or use mysql_real_escape_string($value) if you are going to create a query with this! Otherwise you will create an SQL injection vulnerability here.
}
}
// you can add if(count($sub_key)) here to only do the echoes, if there was at least 1 item in the array. Otherwise you will echo an empty ``
echo "`".implode("`,`", $sub_key)."`";
echo "'".implode("','", $sub_key)."'"; // don't you mean $sub_values here?

How to get key of array when i know this value in php [duplicate]

This question already has answers here:
Retrieve array key passed on value PHP
(5 answers)
Closed 9 years ago.
I have an array like this:
$array = array('name'=>'Van Pham','age'=>'23','sex'=>'male');
How can I get key 'age' with value '23'?
you are looking for array_keys http://www.php.net/manual/en/function.array-keys.php
print_r(array_keys($array, "23")); // age
OR by HamZa DzCyberDeV
echo array_keys($array, "23")[0];
http://codepad.viper-7.com/bZErGT
try this
$my_arr = array('name'=>'Van Pham','age'=>'23','sex'=>'male');
echo get_array_by_value($my_arr, '23');
function get_array_by_value($my_arr = '', $arr_value = '') {
$new_arr = array_flip($my_arr);
if (isset($new_arr[$arr_value])) {
return $new_arr[$arr_value];
}
}
output
age
use array_search()
$key= array_search($value, $array);
example:
$array = array('name'=>'Van Pham','age'=>'23','sex'=>'male');
echo array_search("23", $array); //age
try this
<?php
$arr = array('name'=>'Van Pham','age'=>'23','sex'=>'male');
foreach ($arr as $key => $value) {
echo "Key: $key; Value: $value<br />\n";
}
?>
I think you want to find the keys, for values 23, do it like this:
$array = array('name'=>'Van Pham','age'=>'23','sex'=>'male');
while ($value = current($array)) {
if ($value == '23') {
echo key($array)."\n";
}
next($array);
}
Try with
foreach($array as $key => $value)
{
if($key == 'age' && $value == '23')
echo $key.'-'.$value;
}
<?php
$array = array('name'=>'Van Pham','age'=>'23','sex'=>'male');
echo array_keys($array, "23") .":". $array['age'];
?>
Output
age : 23
It can be done easily using array_search function.This Function Searches the array for a given value and returns the corresponding key if successful.
<?php
$array = array('name'=>'Van Pham','age'=>'23','sex'=>'male');
$key1 = array_search('23', $array);
echo '<p>Key 1='.$key1.'</p>';
$key2 = array_search('Van Pham', $array);
echo '<p>Key 2='.$key2.'</p>';
$key3 = array_search('male', $array);
echo '<p>Key 3='.$key3.'</p>';
$key4 = array_search('female', $array);
echo '<p>Key 4='.$key4.'</p>';
?>
Output:
Key 1=age
Key 2=name
Key 3=sex
Key 4=
DEMO

Categories