Jump to content

nephesh

Members
  • Content Count

    22
  • Joined

  • Last visited

Community Reputation

0 Neutral

About nephesh

  • Rank
    Member

Profile Information

  • Gender
    Not Telling
  1. Yes, I tested my code. I am sorry, I had just added in the else statement for this post, as I have it working correctly as this: function array_search_partial($arr, $keyword) { foreach ($arr as $index => $string) { $userBase = basename($string); $test = substr_count($userBase, '-'); if($test === 2) { if(preg_split("/[-]+/", $userBase)[2] == $keyword) { return $index; } } } } It only needs to find one item, as the $keyword is a unique identifier. But you are right I should return false to the caller. Which is : $index = array_search_partial($profilePaths, $userName); if($index){ $profileLink = "/".$profilePaths[$index].".html"; }
  2. One question though, why would I need to return "false" if nothing is found? It will always find something. And when I've tried adding it as below it doesn't return the correct results. (Also, to make this clear the file paths in the array don't contain the extension.) function array_search_partial($arr, $keyword) { foreach ($arr as $index => $string) { $userBase = basename($string); $test = substr_count($userBase, '-'); if($test === 2) { if(preg_split("/[-]+/", $userBase)[2] == $keyword) { return $index; } else { return false; } } }
  3. Thanks for everyone's help with this! I've got it working perfectly!
  4. I believe I've got it! Does this seem correct to you? function array_search_partial($arr, $keyword) { foreach ($arr as $index => $string) { $userBase = basename($string); $test = substr_count($userBase, '-'); if($test === 2) { if(preg_split("/[-]+/", $userBase)[2] == $keyword) { return $index; } } } }
  5. That might have worked if I needed the whole filename. I need from the end to the first "-" (hyphen) to compare to the $keyword. (eg. lastname-firstname-username) Need to isolate username.
  6. I have a array of file path strings and trying to search the array for a string that would be the end of the path sting and return the index of the file path that contains the string. I've tried using something like this, but it finds the keyword in anyplace in the file path. I've also tried strripos() as well with the same results. Any suggestions? function array_search_partial($arr, $keyword) { foreach ($arr as $index => $string) { if(strpos($string, $keyword) !== FALSE) { return $index; } } }
  7. Thanks Barand, I had figured that out myself while waiting for a response. I think everything is working the way it's supposed to now.
  8. Thanks for the suggestion! One more question, I've decided that I want to work with the $users array before converting it to json, but the array is outputting like: Array ( [username] => Array ( [iD] => 09342095 [Address] => 458 Apt 12)) [username] => Array([iD] => 72372054 [Address] => 232 Center St)) ...and so on I am trying to get the usernames to output from a loop using the code below where the username is the key, but nothing is echoed. What am I missing for ($i=0; $i < count($users); $i++) { $user = $users[$i]; echo "Username: ". $user; }
  9. Got rid of the undefined index errors. Final working code: $file="C:\\wamp\\www\\faculty-staff-dir.lst"; $h = fopen($file, "rt"); $line = ""; $lineArray = array(); $users = array(); while (!feof($h)) { $line = rtrim(fgets($h), "\n"); $lineArray = explode("|", $line); $lineArray = array_map('trim', $lineArray); if(!isset($users[$lineArray[0]])){ $users[$lineArray[0]] = array(); } if(isset($lineArray[0][0]) && isset($users[$lineArray[0]])){ $users[$lineArray[0]][0][$lineArray[1]] = $lineArray[2]; } } $json = json_encode($users); print_r($json); fclose($h); Look okay? Now I just need to write the json to a file.
  10. Okay, I think I've got it by changing the attribute assignment line, but it still gives me the undefined index on that line: $users[$lineArray[0]][0][$lineArray[1]] = $lineArray[2]; Whole code: $file="C:\\wamp\\www\\faculty-staff-dir.lst"; $h = fopen($file, "rt"); $line = ""; $lineArray = array(); $users = array(); while (!feof($h)) { $line = rtrim(fgets($h), "\n"); $lineArray = explode("|", $line); $lineArray = array_map('trim', $lineArray); if(!isset($users[$lineArray[0]])){ $users[$lineArray[0]] = array(); } $users[$lineArray[0]][0][$lineArray[1]] = $lineArray[2]; } $json = json_encode($users); print_r($json); fclose($h);
  11. Acts the same way even with the new array created before the attributes are added and still gives me the undefined index on this line: $users[$lineArray[0]][$lineArray[1]] = $lineArray[2]; Here is the code in it's entirety: $file="C:\\wamp\\www\\faculty-staff-dir.lst";$h = fopen($file, "rt"); $line = ""; $lineArray = array(); $users = array(); while (!feof($h)) { $line = rtrim(fgets($h), "\n"); $lineArray = explode("|", $line); $lineArray = array_map('trim', $lineArray); if(!isset($users[$lineArray[0]])){ $users[$lineArray[0]] = array(); } $users[$lineArray[0]][$lineArray[1]] = $lineArray[2]; } $json = json_encode($users); print_r($json); fclose($h);
  12. I'ts still being used, I didn't show it, because I thought you were saying I was leaving out the attribute on the new array. I guess I am confused about what you said was wrong. if(isset($users[$lineArray[0]])){ $users[$lineArray[0]][$lineArray[1]] = $lineArray[2]; } else { $users[$lineArray[0]] = array($lineArray[1] => $lineArray[2]); }
  13. I am sorry, I created an empty array as you suggested "start each new user with an empty set of attributes. That's an empty array." , but when I add the attributes to the new array like so, } else { $users[$lineArray[0]] = array($lineArray[1] => $lineArray[2]); } I get two "Undefined offset: 1 in C:\wamp\www\csv-to-json.php on line 17" errors even though the code is outputting this format: {"username":{"userID":"1234", "attribute2":"value2"...
  14. I've got it working except it is skipping over a attribute that has a value of a number. Is there a way to rectify this? $file="C:\\wamp\\www\\faculty-staff-dir.lst"; $h = fopen($file, "rt"); $line = ""; $lineArray = array(); $users = array(); while (!feof($h)) { $line = rtrim(fgets($h), "\n"); $lineArray = explode("|", $line); $lineArray = array_map('trim', $lineArray); if(isset($users[$lineArray[0]])){ $users[$lineArray[0]][$lineArray[1]] = $lineArray[2]; } else { $users[$lineArray[0]] = array(); } } $json = json_encode($users); print_r($json); fclose($h);
  15. While I was waiting, I kinda figured part of it out, but I don't know how to output the username as part of the final $users array. The code I have below outputs like this: [{"attribute1":"value1"}] [{"attribute2":"value2"}] ... and so on. Would like something : [{ "user": {"attribute1":"value1"}] [{"attribute2":"value2"}] }] or something close so I could do something like $users['user']['attribute1'] to get the value. Once again thanks for all your help and for being patient with me! $file="C:\\wamp\\www\\faculty-staff-dir.lst"; $h = fopen($file, "rt"); $line = ""; $lineArray = array(); $users = array(); while (!feof($h)) { $line = rtrim(fgets($h), "\n"); $lineArray = explode("|", $line); if(isset($lineArray[0])){ $users[0] = array($lineArray[1] => $lineArray[2]); } $json = json_encode($users); print_r($json); } fclose($h);
×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.