Jump to content

add a second value to an associative array using substr($line, 91, 10);


Go to solution Solved by Barand,

Recommended Posts

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.

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));
Edited by kat35601

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)  ];
    ...
}

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);

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.

Edited by Barand

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);
  • Solution

Does this pseudocode help?

 

while not EOF
    if it is a line with 'AA'
        store the key and items in the array
    endif
endwhile
Now your array contains only the items you wanted so there is no need to filter them out. Sorry I can't be any clearer.

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.

 

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.