Jump to content
#StayAtHome ×

nephesh

Members
  • Content Count

    22
  • Joined

  • Last visited

Everything posted by nephesh

  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);
  16. Requinix, the second part of your code that is to assign a value to an attribute in the $users array won't work, because I don't have a consistent name for the attribute, some users have different attributes than others, that is why I was trying the foreach loop to create an associate array. That is also why I said there is not an attribute "user", it is just the first value in the line.
  17. What am I missing then? With this code, I call explode on $line and assign it to the $lineArray, but my foreach outputs "0 =>". if I echo $line in the while loop and see the entire string of users, but I've also tested the supposed array $lineArray with count() and it returns a "1". $file='C:\\wamp\\www\\faculty-staff-dir.lst'; $h = fopen($file, "rt"); while (!feof($h)) { $line = rtrim(fgets($h), "\n"); } fclose($h); $lineArray = explode(" | ", $line); foreach($lineArray as $key => $value){ echo "{$key} => {$value}"; }
  18. I am very new to php, so this line you mention: "To merge the data keep an array keyed by username that contains all the attributes. Start with an empty array" I can't quite grasp how to accomplish this, as $line is the array of data and you don't push the $values from it anywhere the empty $users array. Please forgive my ignorance, I am more familiar with JavaScript.
  19. Thanks requinix! The "username1, username2", etc. values don't have an actual "attribute" for them, its just the users last name, so somehow I need to get that value set as my example output.
  20. I have a text file with attributes in this format: username1 | attribute1 | value1 username1 | attribute2 | value2 username1 | attribute3 | value2 username2 | attribute1 | value1 username2 | attribute2 | value2 username2 | attribute3 | value3 I would like to get them into a JSON array like: [ users: [ { username1: { attribute1: "value1", attribute2: "value2", attribute3: "value3" }, { username2: { attribute1: "value1", attribute2: "value2", attribute3: "value3" }, { username3: { attribute1: "value1", attribute2: "value2", attribute3: "value3" }, ] ] Here is what I have so far: $file='C:\wamp\www\faculty-staff-dir.lst'; $csv= file_get_contents($file); $array = preg_split("/[\n]+/", $csv); foreach($array as $key=>$value) { $array[$key]=array($value=$value); } $json = json_encode($array); print_r($json); it's outputting in this format now: [["username1 | attribute1 | value1"], ["username1 | attribute2 | value2"], ["username1 | attribute3 | value3"], etc.. Any help would be greatly appreciated!!
×
×
  • 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.