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 removearray_column($array2, 0)
it doesn't work.
If i usearray_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
)
You can index onusername
orpostid
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));
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);
Our community is visited by hundreds of web development professionals every day. Ask your question and get a quick answer for free.
Find the answer in similar questions on our website.
Do you know the answer to this question? Write a quick response to it. With your help, we will make our community stronger.
PHP (from the English Hypertext Preprocessor - hypertext preprocessor) is a scripting programming language for developing web applications. Supported by most hosting providers, it is one of the most popular tools for creating dynamic websites.
The PHP scripting language has gained wide popularity due to its processing speed, simplicity, cross-platform, functionality and distribution of source codes under its own license.
https://www.php.net/
Welcome to the Q&A site for web developers. Here you can ask a question about the problem you are facing and get answers from other experts. We have created a user-friendly interface so that you can quickly and free of charge ask a question about a web programming problem. We also invite other experts to join our community and help other members who ask questions. In addition, you can use our search for questions with a solution.
Ask about the real problem you are facing. Describe in detail what you are doing and what you want to achieve.
Our goal is to create a strong community in which everyone will support each other. If you find a question and know the answer to it, help others with your knowledge.