Reputation: 323
I'm using a text file as database and i'm trying to get the index value. Text file:
0982|Chiara|chiaramella|543254
7134|Paolo|pablo752|675474
9564|Andrea|andry8239|39377874
3122|Luca|luka7|26887543
4456|Riccardo|riccard904|6832787645
9721|Fabio|fab78|38307696
3284|Francesco|frafra54|9325454
9555|Luigi|lulu14|0055468
1485|Matteo|matty990|897103464
0986|Laura|lau3245|324891000
3714|Claudio|cla235|36464820
9986|Giovanni|giojo982|0005405
8244|Stefano|stefy734|45367
7731|Marco|markkkk998|355647689
2123|Roberto|robn88|809678741
and this piece of code to get the index:
$db_friends = file("db_friends.txt");
$db_friends = array_map('trim', $db_friends);
$key = array_search('7134', $db_friends);
echo $key;
The echo result, should be give me "1", but maybe there is an error because is always empty and i'm not figured out why? Any help? Please! Thanks a lot!
Upvotes: 0
Views: 693
Reputation: 350831
array_search
only matches array elements that match completely, not those that contain the given string, but have more than that.
You could first extract the numerical value of each string, and only then continue with array_search
, like this:
$key = array_search('7134', array_map('intval', $db_friends));
Upvotes: 0
Reputation: 48387
array_search() is working fine - it is your expectations which are wrong. The array_search() function looks for an element in the array containing only the value passed as a parameter, not a value containing, beginning with or ending in the search value.
If the first value in the dataset is unique, then you could use that as an array index:
$data=array();
$f=fopen('yourfile.txt', 'r');
While (($r=fgetcsv($f, 0, '|') && !feof($f)) {
$i=array_shift($r);
$data[$i]=$r;
}
print_r $data['7134'];
Or simply...
preg_match('/\n7134\|(.*)/', file_get_contents('yourfile.txt'), $matches);
print $matches[0];
However using files for maintaining data not scale well - there are problems with capacity, concurrency and performance. Thats why we use DBMS like mongodb or mysql.
Upvotes: 0
Reputation: 1123
The array_search
try to find a exactly value, for instance:
$array = ['value one'];
var_dump(array_search('one', $array)); //= false
var_dump(array_search('value one', $array)); //= 0
The $db_friends
is something like a ["2123|Roberto|robn88|809678741", "0982|Chiara..."]
this not have any values with exactly "2123", for this reason the array_search
not work.
To fix that you can use explode()
to create another array and after find it using array_search
:
$file = file('db_friends.txt');
foreach($file as $line){
$db_friends[] = explode('|', trim($line));
}
$key = array_search('7134', array_column($db_friends, '0'));
echo $key; //= 1
In this situation the we create a new array inside another (because of explode()
) and we try to find 7134
inside the first column of this new array.
Upvotes: 1
Reputation: 782107
You should convert the array to an associative array.
$db_friends_assoc = array();
foreach ($db_friends as $friend) {
list($id, $lastname, $username, $code) = explode('|', $friend);
$db_friends_assoc[$id] = array('lastname' => $lastname, 'username' => $username, 'code' => $code);
}
Then you can just do:
$key = $db_friends_assoc['7134'];
Upvotes: 0
Reputation: 1381
You can iterate over each row, checking if the row starts with that id.
Complete Code:
$db_friends = file("db_friends.txt");
$rows = array_map('trim', $db_friends);
$id = '7134';
$index = -1;
foreach ($rows as $i => $row) {
if (substr($row, 0, strlen($id)) === $id) {
$index = $i;
}
}
// $index now contains the index of the row
// As a function
function getIndexOfIdInFile($file, $id) {
$file = file("db_friends.txt");
$file = array_map('trim', $file);
foreach ($file as $i => $row) {
if (substr($row, 0, strlen($id)) === $id) {
// Return the index of the id if we have found it
return $i;
}
}
// Return -1 if the id cannot be found
return -1;
}
Upvotes: 0