get repeated value from parent array and not repeated value from parent array
get repeated value from parent array and not repeated value from parent array
one is parent array and another is filtered array.
From there i just want to set two seperate array
common array
unique array
$a = array([0]=> Array
(
[id] => 22429
[user_id] => 0
[shop_id] => 78
[start_date] =>
[fn_point] => 0
),[1]=> Array
(
[id] => 22430
[user_id] => 0
[shop_id] => 78
[start_date] =>
[fn_point] => 0
),[2]=> => Array
(
[id] => 22431
[user_id] => 0
[shop_id] => 78
[start_date] =>
[fn_point] => 0
),[3] => Array
(
[id] => 22432
[user_id] => 0
[shop_id] => 78
[start_date] =>
[fn_point] => 0
));
$b = array("0"=> Array
(
[id] => 22428
[user_id] => 0
[shop_id] => 78
[start_date] =>
[fn_point] => 0
),"1" => Array
(
[id] => 22430
[user_id] => 0
[shop_id] => 78
[start_date] =>
[fn_point] => 0
),[2]=> Array
(
[id] => 22431
[user_id] => 0
[shop_id] => 78
[start_date] =>
[fn_point] => 0
),[3] => Array
(
[id] => 22432
[user_id] => 0
[shop_id] => 78
[start_date] =>
[fn_point] => 0
));;
output for unique array will be
$c = array(
[0]=> Array
(
[id] => 22429
[user_id] => 0
[shop_id] => 78
[start_date] =>
[fn_point] => 0
)
);
output for common array will be
$u = array(
[0]=> Array
(
[id] => 22430
[user_id] => 0
[shop_id] => 78
[start_date] =>
[fn_point] => 0
),[1]=> Array
(
[id] => 22431
[user_id] => 0
[shop_id] => 78
[start_date] =>
[fn_point] => 0
),[2]=> Array
(
[id] => 22432
[user_id] => 0
[shop_id] => 78
[start_date] =>
[fn_point] => 0
)
);
array_diff
array_intersect
Why are these numbers strings, and not integers?
– Nils Werner
Sep 10 '18 at 7:14
@NilsWerner why does it matter? It could be a Json file that is decoded.
– Andreas
Sep 10 '18 at 7:17
When you ask a question make sure what you ask is relevant to the actual code and variables. Single dimensional and multi dimensional arrays are handles completely different
– Andreas
Sep 10 '18 at 8:22
3 Answers
3
To get common from both array,use array_intersect()
$c = array_values(array_intersect($a,$b)); //array_values() used for re-indexing final array
print_r($c);
Output:- https://eval.in/1055829
Use array_diff()
$c = array_values(array_diff($a,$b));//array_values() used for re-indexing final array
print_r($c);
Output:- https://eval.in/1055830
Reference:-
array_values()
For your edited question apply foreach()
along with in_array()
and array_column()
foreach()
in_array()
array_column()
$final_array = ;
foreach($a as $key=>$val)
if(in_array($val['id'],array_column($b,'id')))
$final_array = $val;
print_r($final_array); // common values array
Output:- https://eval.in/1055838
And
$final_array = ;
foreach($a as $key=>$val)
if(!in_array($val['id'],array_column($b,'id')))
$final_array = $val;
print_r($final_array); // non common value array
Output:-https://eval.in/1055839
For your common array:
array_intersect($a, $b);
For your unique array:
array_unique(array_merge($a, $b), SORT_REGULAR);
I have updated my question. can u pls check it. array_unique not supporting.
– Lemon Kazi
Sep 10 '18 at 7:22
Array_intersect and array_diff
$a = array('1','2','3','4');
$b = array('4','5','6','7');
$c = array_intersect($a, $b);
var_dump($c); //4
$d = array_diff($a,$b);
var_dump($d); //1,2,3
https://3v4l.org/Ut8JW
I have updated my question. can u pls check it. PhpErrorException: Array to string conversion
– Lemon Kazi
Sep 10 '18 at 7:22
Thanks for contributing an answer to Stack Overflow!
But avoid …
To learn more, see our tips on writing great answers.
Required, but never shown
Required, but never shown
By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.
array_diff
,array_intersect
.– u_mulder
Sep 10 '18 at 7:12