This question already has answers here:
Php match values between 2 arrays
(4 answers)
Closed 6 years ago.
I have two arrays as follows:-
$a = ["2","11","6"];
$b = ["6","7"];
$c = array_diff($a, $b);
$c = ["2","11"];
The result in $c is wrong. I want the result should be as $c = [6]
in other words i want the common elements in both array be returned! but it is giving wrong error. Kindly help me?
Use array_intersect()
$a = ["2","11","6"];
$b = ["6","7"];
$c = array_intersect($a, $b);
Demo: https://eval.in/682653
You can use array_intersect
$c = array_intersect($a, $b);
$a = ["2","11","6"];
$b = ["6","7"];
$c = array_intersect($a,$b);
Use array_intersect (http://php.net/manual/en/function.array-intersect.php)
<?php
$a = ["2","11","6"];
$b = ["6","7"];
$c = array_intersect($a, $b);
print_r($c)
?>
Related
I am in trouble while converting a link to tag . here is
I am trying
[audio mp3="https://abcd.com/wp-content/uploads/sites/2/2020/03/classical-demo.mp3"][/audio]
to convert
I am use str_replace, following are the code one by one.
$a = '[audio mp3="https://abcd.com/wp-content/uploads/sites/2/2020/03/classical-demo.mp3"][/audio]';
echo str_replace("[","<",$a);
echo str_replace("]",">",$a);
echo str_replace("audio","a",$a);
echo str_replace("mp3","href",$a);
exit();
But still getting no result.
Is any another way for solving this? any one having solution for this please help me to solve out this problem.
I'm not PHP developer, but I think you can use following code:
$a = '[audio mp3="https://abcd.com/wp-content/uploads/sites/2/2020/03/classical-demo.mp3"][/audio]';
$b = str_replace("[","<", $a);
$b = str_replace("]",">", $b);
$b = str_replace("audio","a", $b);
// you need to replace mp3=, not mp3, as you have 2 of it
$b = str_replace("mp3=","href=", $b);
// optional
$b = str_replace("><",">link text<", $b);
echo $b;
exit();
$b will be: link text
See it in action thanks to #Phil
The problem is that you are running replace on $a without updating it, so you function replaces < with [ and outputs, then it replaces > with ] but on the original variable, then outputs.
If you update the variable with the result of str_replace it works as intended.
$a = '[audio mp3="https://abcd.com/wp-content/uploads/sites/2/2020/03/classical-demo.mp3"][/audio]';
$a = str_replace("[","<",$a);
$a = str_replace("]",">",$a);
$a = str_replace("audio","a",$a);
$a = str_replace("mp3=","href=",$a);
echo $a;
Edit
Also as #Phil pointed out in a comment your last line will change your file extension, I adjusted the last replace to account for this.
As alternative, to str_replace, perhaps you could use regex?
$x = '[audio mp3="https://abcd.com/wp-content/uploads/sites/2/2020/03/classical-demo.mp3"][/audio]';
preg_match('/"(.*?)"/', $x, $matches);
print_r($matches);
output
Array
(
[0] => "https://abcd.com/wp-content/uploads/sites/2/2020/03/classical-demo.mp3"
[1] => https://abcd.com/wp-content/uploads/sites/2/2020/03/classical-demo.mp3
)
I have two arrays:
$a = array('a','b','c');
$b = array('b','c','d','e');
What must I do in order to get array ('a','d','e') via array $a and $b?
$a = array('a','b','c');
$b = array('b','c','d','e');
$output = array_merge(array_diff($a, $b), array_diff($b, $a));
I think that is right - is off top of my head.
Yup http://ideone.com/56V0d0 <- fiddle here - seems to work!
This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
php values of one array to key of another array
I have the sorted array
$A = array(
0=>EUR,
1=>GBP,
2=>USD
);
$B = array(
0=>'0.88',
1=>'0'
);
I want to map to be like this allways put 'EUR'=>'1':
$C = array(
'EUR'=>'1',
'GBP'=>'0.88',
'USD'=>'0'
);
Could anyone tell me please?
$C = array_combine($A, array_merge(array(1), $B));
$C = array();
foreach ($A as $key => $value)
{
$C[$value] = $B[$key];
}
I have 2 arrays.
I want to add the elements of $b to the end of $a. I checked google, nothing.
$a=array(1,2,3);
$b=array(4,5,6);
array_push($a,$b);
print_r($a);
My goal is to make $a=(1,2,3,4,5,6) but the above doesn't work correctly...any ideas?
You can use array_merge for that.
you can use array_merge
$c = array_merge($a, $b);
$c = array(1,2,3,4,5,6);
Use array_merge()
$a = array(1,2,3);
$b = array(4,5,6);
$c = array_merge($a, $b);
print_r($c);
You can't use array operator: + as xzyfer pointed out.
It will do a union and only works when the keys don't overlap.
Here's the situations:
I have 2 arrays, eg:
$a=array('a','b','c','d');
$b=array('1','b','c','e');
I want to produce 2 arrays with result:
$c=array('a','d');//only element appeared on $a
$d=array('1','e');//only element appeared on $b
Do you have a clever solution?
$c = array_diff($a, $b);
$d = array_diff($b, $a);
Sorry, my bad. It turn out a was giving the wrong array in my test.
simple array_diff solved the problem:
$c = array_diff($a, $b);
$d = array_diff($b, $a);
Try using the array_diff() function:
array_diff(array1,array2,array3...)
eg:
<?php
$a1=array(0=>"Cat",1=>"Dog",2=>"Horse");
$a2=array(3=>"Horse",4=>"Dog",5=>"Fish");
print_r(array_diff($a1,$a2));
?>
Output:
Array ( [0] => Cat )
Source: http://www.w3schools.com/PHP/func_array_diff.asp