kat35601 Posted September 25, 2017 Share Posted September 25, 2017 I need to add a second value to my associative array. $lines[substr($line, 69, 5)] = substr($line, 117, 2); I need to add substr($line, 91, 10) Link to comment Share on other sites More sharing options...
Barand Posted September 25, 2017 Share Posted September 25, 2017 Sorry, but I have no idea what you want to do. Is the thing you want to add a value or a key? If a value, do you want to add it without a key or add it as a second value to the existing key? Perhaps you could show an example of what line contains and how you want the resulting array to appear. Link to comment Share on other sites More sharing options...
kat35601 Posted September 25, 2017 Author Share Posted September 25, 2017 as a second value to the existing key? $lines = [];$fp = fopen('order_test1.txt', 'r'); while (!feof($fp)) { $line = fgets($fp); $lines[substr($line, 69, 5)] = substr($line, 117, 2); //I would like to add second array value substr($line, 91, 10) print_r($lines); } fclose($fp); $order=array_filter($lines,function($AA){ return $AA=='AA' | $AA=='AC';} ); $key_string = implode(',', array_keys($order)); Link to comment Share on other sites More sharing options...
cyberRobot Posted September 25, 2017 Share Posted September 25, 2017 You could use an array. $lines[substr($line, 69, 5)] = array( substr($line, 117, 2), substr($line, 91, 10) ); Link to comment Share on other sites More sharing options...
Barand Posted September 25, 2017 Share Posted September 25, 2017 Each key in an array must be unique, so the values (if more than one for a key) must be in a subarray. while (!feof($fp)) { $line = fgets($fp); $lines[substr($line, 69, 5)] = [ substr($line, 117, 2) , substr($line, 91, 10) ]; ... } Link to comment Share on other sites More sharing options...
kat35601 Posted September 25, 2017 Author Share Posted September 25, 2017 When I use the subarray it kills my filter and implode. So i will need to tell the filter and implode which array value I want to use 0 or 1 correct. What should that look like? $key_string = implode(',', array_keys($order)); print_r($key_string); $sql_update="select ompsalesorderid from m1_kf.dbo.SalesOrders where ompsalesorderid in ($key_string)"; $connect = odbc_connect("kforbe", "sa", "H25rlz95!"); $result = odbc_exec($connect, $sql_update); Link to comment Share on other sites More sharing options...
Barand Posted September 25, 2017 Share Posted September 25, 2017 Alter the filter function to check the first element of each subarray, or check the values before you store them in the array so there is no need to filter it. Link to comment Share on other sites More sharing options...
kat35601 Posted September 26, 2017 Author Share Posted September 26, 2017 That went over my head quickly. so let me restart to make sure I do it right. I download a file from an ftp site then I look through that file and pull out 3 items (order number, assignment number, status) I want to filter by status and I may need to filter more than one time. Different status causes different updates. For now, I am just worried about AA and AC my output so far looks like this when I do a print_r Array ( [48578] => Array ( [item1] => 172686046 [item2] => AA ) ) Array ( [48578] => Array ( [item1] => 172686046 [item2] => AA ) [48577] => Array ( [item1] => 172686047 [item2] => AA ) ) Array ( [48578] => Array ( [item1] => 172686046 [item2] => AA ) [48577] => Array ( [item1] => 172686047 [item2] => AA ) [ ] => Array ( [item1] => [item2] => ) ) Array ( [48578] => Array ( [item1] => 172686046 [item2] => AA ) [48577] => Array ( [item1] => 172686047 [item2] => AA ) [ ] => Array ( [item1] => [item2] => ) [0] => Array ( [item1] => [item2] => ) ) now I need to filter this for only AAs because in real time there will be a lot of different statuses. then update my MSSQL update statement. so how do fix my filter and implode or is there a better way to do this? Then how should I update my sql statement? Thanks $curl = curl_init();$fh = fopen("order_test1.txt", 'w'); curl_setopt($curl, CURLOPT_URL, 'ftp://server.com/Outbox/' . $server_file); curl_setopt($curl, CURLOPT_USERPWD, "removed"); curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1); $result = curl_exec($curl); fwrite($fh, $result); fclose($fh); curl_close($curl); } $lines = []; $fp = fopen('order_test1.txt', 'r'); while (!feof($fp)) { $line = fgets($fp); $index = substr($line, 69, 5); $lines[$index] = ['item1' => substr($line, 91, 10), 'item2' => substr($line, 117, 2)]; } fclose($fp); $order=array_filter($lines,function($AA){ return $AA=='AA' | $AA=='AC';} ); //print_r($order); $key_string = implode(',', array_keys($order)); $sql_update="update uag.dbo.SalesOrders set order_credit status=-1, assignment_num= "orders[value2]" where ompsalesorderid in ($key_string)"; $connect = odbc_connect("removed"); $result = odbc_exec($connect, $sql_update); Link to comment Share on other sites More sharing options...
Barand Posted September 26, 2017 Share Posted September 26, 2017 Does this pseudocode help? while not EOF if it is a line with 'AA' store the key and items in the array endif endwhileNow your array contains only the items you wanted so there is no need to filter them out. Sorry I can't be any clearer. Link to comment Share on other sites More sharing options...
kat35601 Posted September 26, 2017 Author Share Posted September 26, 2017 I do like the " if " before the array but a line may have AA in a name so it would mess me up. while (!feof($fp)) { $line = fgets($fp); if(substr($line, 117, 2)=='AA' | substr($line, 117, 2)=='AC'){ $lines[substr($line, 69, 5)] =substr($line, 91, 10); print_r($lines); } } fclose($fp); my output looks like Array ( [45925] => 171665579 ) Array ( [45925] => 171665579 [46409] => 171662760 ) I get 45925 twice but it's only in the file once. Link to comment Share on other sites More sharing options...
Barand Posted September 26, 2017 Share Posted September 26, 2017 Do the print_r AFTER the loop when you have finished building the array. You should be using "||". You are using the bitwise OR opertor (ie "|"). Link to comment Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.