This question already has answers here:
Merge two arrays as key value pairs in PHP
(3 answers)
Closed 9 months ago.
Is it possible that I can get the data from the other array based on the value from another?
If I input value from array it will return a value from other array.
Example:
$arr1 = ['A','B','C'];
$arr2 = [1,2,3,];
Input: 2
Result: B
Do they need to be separate?
You could use array_combine() to assign a key to the value and then input is just $array[$input] - e.g.
$arr1 = ['A', 'B', 'C'];
$arr2 = [1, 2, 3];
$arr = array_combine($arr2, $arr1);
echo $arr[$_POST['input']]; # will display 2
Since your arrays have not been given any specific keys, they are assigned numerically indexes from PHP, starting from zero.
You can then use array_search() to get the key of the $arr2 array, and use that to find the value in $arr1.
$key = array_search($input, $arr2);
$output = $arr1[$key];
If either array has defined indexes, you can use array_values() to just get the values and get the numeric indexes from PHP again.
Live demo at https://3v4l.org/mf688
This function combine 2 or more arrays
<?php
$array1 = array('A', 'B', 'C');
$array2 = array('1', '2', '3');
$result = array_merge($array1, $array2);
print_r($result);
?>
Working with array $result above (1 of 2 possibility)
<?php echo $result[1]; ?>
Or use
<?php
$q = '2'; $key = array_search($q, $result);
if(!$key)
echo "This search returned no key and value pair";
else
echo "This search for value: " . $q . " is present in key: " . $key;
?>
Related
This question already has an answer here:
Assign by reference bug
(1 answer)
Closed 6 years ago.
Consider the following PHP code segment.
$array1 = array(1,20);
$x = &$array1[1];
$array2 = $array1;
$array2[1] = 22;
print_r($array1[1]); // Output is 22
Here, $array2 is not referencing to $array1, but how to change value in $array2 by changing value of $array1?
If you want $array2 to be a reference of $array1 then you do the same thing as with $x.
$array2 = &$array1;
Now anything you change in either $array1 or $array2 is visible in both arrays since $array2 is just a reference to $array1.
Update
Thinking about it, what you may be looking at is a way to change a value, but still have a full copy of the arrays. This is doable with an object.
$obj = new stdClass();
$array1 = array(1, 20);
$array1[1] = $obj;
$array1[1]->color = 22;
$array2 = $array1;
$array2[1]->color = 33;
echo $array1[1]->color; // prints 33
This is because objects are always copied by reference, whereas numbers and strings are copied as is.
I have two array inputs like this :
$array1 = [1,2,3,4,6];
$array2 = [1,3];
$output = array_merge(array_diff($array1,$array2),array_diff($array2,$array1));
Now I want to check array1 with array 2 and eliminate 1 and 3 in $array1
and the output I am expecting is
$output = [2,4,6];
but in this method I get some bugs, when array2 have single value e.g.: $array2 = [1]; , $array1 = [1,2,3,4,6]; the output should be $output = [2,3,4,6];. But I am getting $array1 all values [1,2,3,4,6];
Simple :)
(Just un-complicate your code and you don't need anything new for that)
<?php
$array1 = array(1,2,3,4,6);
$array2 = array(1,3);
$result = array_diff($array1, $array2);
print_r($result);
?>
Demo
In Your style it can even be a one liner :P
<?php print_r(array_diff(array(1,2,3,4,6), array(1,3))); ?>
This question already has answers here:
php remove duplicates from array
(5 answers)
Closed 9 years ago.
How do I remove duplicates from an array?
Let's say that my I have two arrays named $array and $new_array. $array has contents while $new_array is empty, as seen below:
$array = array(5,1,2,1,5,7,10);
$new_array = array();
I want $new_array to store the unique values of $array. It kind of goes like this:
$array = array(5,1,2,1,5,7,10);
$new_array = array(5,1,2,7,10); // removing the 1 and 5 after 2 since those numbers are already a duplicate of the preceding numbers.
echo $new_array; // Output: 512710
You can do it through PHP's array_unique function.
This function traverses through your provided array and returns an array with unique values (repeating values will be removed).
Code to return desired string:
$array = array(5,1,2,1,5,7,10);
$new_array = array_unique($array);
echo implode('', $new_array);
Use array_unique() and implode():
$array = array(5,1,2,1,5,7,10);
$new_array = array_unique($array);
echo implode('', $new_array);
Output:
512710
$arr1 = array('potato'=>1,'tomato'=>2,'apple'=>5,'banana'=>10);
$arr2 = array('orange'=>20,'tomato'=>3,'apple'=>5,'banana'=>20);
I need function that would return array('tomato','banana'), consider that it omits keys that don't exist in one or the other array. Apple has the same value in both arrays, so it should be omitted - returned should be only keys whose values differ and are set
This should work (demo):
$arr1 = array('potato'=>1,'tomato'=>2,'apple'=>5,'banana'=>10);
$arr2 = array('orange'=>20,'tomato'=>3,'apple'=>5,'banana'=>20);
$result = array_keys(array_diff(array_intersect_key($arr1, $arr2), $arr2));
print_r($result);
Output:
Array
(
[0] => tomato
[1] => banana
)
Reference:
array_intersect_key — Computes the intersection of arrays using keys for comparison
array_diff — Computes the difference of arrays
array_keys — Return all the keys or a subset of the keys of an array
$array3 = array();
foreach(array_intersect_key($array1, $array2) as $key => $v){
if($array1[$key] != $array2[$key]) $array3[] = $key;
}
<?php
/**
* Returns an array which contains keys which are in both $array1
* and $array2, and which have different values.
*/
function getKeysWhichMatchAndHaveDifferentValues($array1, $array2)
{
$arrIntersected = array_intersect_key($array1, $array2);
foreach($arrIntersected as $key => $value)
{
if($array2[$key] == $value) {
unset($arrIntersected[$key]);
}
}
return array_keys($arrIntersected);
}
$arr1 = array('potato'=>1,'tomato'=>2,'apple'=>5,'banana'=>10);
$arr2 = array('orange'=>20,'tomato'=>3,'apple'=>5,'banana'=>20);
$final = getKeysWhichMatchAndHaveDifferentValues($arr1, $arr2);
echo '<pre>' . print_r($final) . '</pre>';
?>
I would do simple loop.
Of course if you will need to compare large arrays, the native PHP functions could help a lot. Still can't answer right now what would be the most optimal way to do this.
You could do this using array_intersect and array_keys.
$arr3 = array_intersect(array_keys($arr1), array_keys($arr2));
This question already has answers here:
Create an assoc array with equal keys and values from a regular array
(3 answers)
Closed 6 years ago.
I have this array:
$a = array('b', 'c', 'd');
Is there a simple method to convert the array to the following?
$a = array('b' => 'b', 'c' => 'c', 'd' => 'd');
$final_array = array_combine($a, $a);
Reference: http://php.net/array-combine
P.S. Be careful with source array containing duplicated keys like the following:
$a = ['one','two','one'];
Note the duplicated one element.
Be careful, the solution proposed with $a = array_combine($a, $a); will not work for numeric values.
I for example wanted to have a memory array(128,256,512,1024,2048,4096,8192,16384) to be the keys as well as the values however PHP manual states:
If the input arrays have the same string keys, then the later value for that key will overwrite the previous one. If, however, the arrays contain numeric keys, the later value will not overwrite the original value, but will be appended.
So I solved it like this:
foreach($array as $key => $val) {
$new_array[$val]=$val;
}