php - array_diff() removing partial values
I am developing a hashtag search system on Instagram. And I get a list of users with their posts in the following form:
username1-idposts123455665435, username2-idposts45634563456, username3-idposts276544234....
I set up an array to be able to work with the display on my page, with the code, where $getRelatorio
are the users above.
$base_array_split = preg_split("/,\s/", $getRelatorio);
$base_array_filtro = array_filter($base_array_split);
foreach($base_array_filtro as $explode_usuarios_array)
{
$explode_usuarios = explode("-", $explode_usuarios_array);
$array1[] = array(
$explode_usuarios[0], //get username
$explode_usuarios[1] // ger post id
);
}
Everything works fine. But I want to implement a Black List system, to block the display of users that the customer does not want to see.
I store it in the database in the same way, separated by a comm and to work, I also assemble an array.
$base_array_lista = preg_split("/,\s/", $listanegra);
$base_array_filtro_lista = array_filter($base_array_lista);
foreach($base_array_filtro_lista as $explode_lista_negra)
{
$is_array[] = array(
$explode_lista_negra //get username
);
}
$array2 = isset($is_array) && is_array($is_array) ? $is_array : []; //if empty
Now, I am using the following to exclude the users that the customer has blacklisted:
$arrayfinal = array();
foreach($array1 as $arr)
{
$arrayfinal[] = array_diff($arr, array_column($array2, 0));
}
$array = array_filter($arrayfinal);
The difference between the two arrays is that one has the post id.
The result: I am able to remove the username, but the post ID is still displayed in the array.
If i remove array_column($array2, 0)
it doesn't work.
If i use array_diff_assoc
it doesn't work either
var_dump:
[0] => Array
(
[0] => username1
[1] => 2577195852001190087
)
[1] => Array
(
[0] => username2
[1] => 2577195809822230254
)
[2] => Array
(
[0] => 2577195306530472731 //username removed, bud post id is not - need to remove that too
)
var_dump expected:
[0] => Array
(
[0] => username1
[1] => 2577195852001190087
)
[1] => Array
(
[0] => username2
[1] => 2577195809822230254
)
Answer
Solution:
You can index on username
or postid
and do it like this (indexed on postid):
foreach(explode(', ', $string) as $values) {
$parts = explode('-idposts', $values);
$result[$parts[1]] = $parts[0];
}
$blacklist = ['username3'];
$result = array_diff($result, $blacklist);
Or using the format you have:
foreach(explode(', ', $string) as $values) {
$parts = explode('-idposts', $values);
$result[$parts[0]] = $parts;
}
$blacklist = ['username3'];
$result = array_diff_key($result, array_flip($blacklist));
Answer
Solution:
Here's another way.. flatten the arrays using json_encode, comparing then reinflating. View a demo here: https://www.tehplayground.com/IcpKbUyFltbXhNQp
<?php
$array1 = array(
array("username1", "2577195852001190087"),
array("username2", "1577195852001190087"),
array("username3", "3577195852001190087")
);
$array2 = array(
array("username2", "1577195852001190087"),
array("username3", "3577195852001190087")
);
$a=array_map(function($v) { return json_encode($v); }, $array1);
$b=array_map(function($v) { return json_encode($v); }, $array2);
$arrayfinal = array_map(function($v) { return json_decode($v); }, array_diff($a, $b) );
$array = array_filter($arrayfinal);
print_r($array);
Source