I am trying to create a search function to search a name in a data contain in txt file.
I have a data like this:
dimitri, 1998, php
nikolai, 1998, php
yuri, 1998, php
alyosha, 1998, php
I came up with an idea that to change this data to an array like this:
Array
(
[0] => dimitri, 1998, php
[1] => nikolai, 1998, php
[2] => yuri, 1998, php
[3] => alyosha, 1998, php
)
Then divide more to become multidimensional
Array
(
[0] => dimitri
Array(
[0] => 1998
[1]=> php
[1] => nikolai
Array(
[0] => 1998
[1]=> php
[2] => yuri
Array(
[0] => 1998
[1]=> php
[3] => alyosha
Array(
[0] => 1998
[1]=> php
)
So that I can search name through key.
Now I don't know what to do anymore. I try to use foreach()
function to explode the value in an array, however it did not work, it created another problem, the array only display some characters.
Attempt
$array = array();
$split = explode('\n', file_get_contents($file));
foreach ($split as $content){
$array = array_filter(array_map("trim", explode("\n", $content)));
$array1 = array();
$split2 = explode(", ", $array);
foreach($array as $row){
$array1[$row[1]][$row[2]][]=$row[0];
}
}
HTML
<form action="search.php" method="POST">
<input name="search_function" type="text" placeholder="Search who you want">
<input type="submit" name="search" value="Search">
</form>
About my search, I am thinking to use post and if type correctly with data in search input, the data will display.