Add variable in php file with inside array using fwrite()? - php

I have problems to add variable in php file with inside array using fwrite.
My php file has an array and is like this:
<?php
$array48= array (
0 =>
array (
0 => '2019-04-24-1248326-1313',
1 => 1556110847000,
2 => 30.6647,
3 => 71.387200000000007,
4 => 3.999999999999999,
5 => 51,
6 => 'O',
7 => 13,
8 => 0,
)
)
i want to add variable at first of file:
$fp_due_min_fa = fopen('../file.php', 'r+');
fwrite($fp_due_min_fa, '<?php ' . "\n" . '$id = ' . $id_terr . ';' . "\n");
fclose($fp_due_min_fa);
but after this code the php file is like this:
<?php
$id = 123456;
556110847000,
2 => 30.6647,
3 => 71.387200000000007,
4 => 3.999999999999999,
5 => 51,
6 => 'O',
7 => 13,
8 => 0,
)
)
the array is break...
i would to have this output:
<?php
$id = 123456;
$array48= array (
0 =>
array (
0 => '2019-04-24-1248326-1313',
1 => 1556110847000,
2 => 30.6647,
3 => 71.387200000000007,
4 => 3.999999999999999,
5 => 51,
6 => 'O',
7 => 13,
8 => 0,
)
)
why i have this mistake?
I hope you can help me.
Thanks a lot and sorry for my english

The overall design looks bad, but in general just read, concatenate and write to pre-pend to the beginning. So as not to have to strip out the opening PHP tag, just open and close the tags:
$output = "<?php \$id = $id_terr; ?>\n";
$file = file_get_contents('../file.php');
file_put_contents('../file.php', $output.$file);
Or to append (assuming there is no closing PHP tag as shown), simply:
file_put_contents('../file.php', "\n\$id = $id_terr;\n", FILE_APPEND);

Related

fgetcsv() not respecting $enclosure

I'm working on a script talking to an SMTP server via Sockets, and I'm trying to implement DIGEST-MD5 authentication, but I'm having trouble parsing the string that gets sent back after the AUTH command.
After a base64_decode() it looks like:
realm="smtp.domain.net",nonce="AJRUc5Jx0UQbv5SJ9FoyUnaZpqZIHDhLTU+Awn/K0Uw=",qop="auth,auth-int",charset=utf-8,algorithm=md5-sess
I wanted to use str_getcsv(), but the server is still on PHP 5.2, so I got the following code from the comments on PHP.net, and it looks just fine:
<?php
if (!function_exists('str_getcsv')) {
function str_getcsv($input, $delimiter=',', $enclosure='"', $escape=null, $eol=null) {
$temp=fopen("php://memory", "rw");
fwrite($temp, $input);
fseek($temp, 0);
$r = array();
while (($data = fgetcsv($temp, 4096, $delimiter, $enclosure)) !== false) {
$r[] = $data;
}
fclose($temp);
return $r;
}
}
But it returns the following:
array (
0 =>
array (
0 => 'realm="smtp.domain.net"',
1 => 'nonce="2PuESkmrNzGu/5b8N6eIYQoW7mSlScnYAB/PSYebkYo="',
2 => 'qop="auth',
3 => 'auth-int"',
4 => 'charset=utf-8',
5 => 'algorithm=md5-sess',
),
)
Note that indexes 2 and 3 should be a single qop="auth,auth-int".
On writing this I realized that maybe fgetcsv() expects that the $enclosure character contain the entire field and not just part of it, but in that case I have to idea how to properly parse this string.
In my googlings for 'PHP DIGEST-MD5' I've come across a patch for another project that deals with the same format string with the folowing line:
preg_match_all('/(\w+)=(?:"([^"]*)|([^,]*))/', $challenge, $matches);
Which gives me:
array (
0 =>
array (
0 => 'realm="smtp.domain.net',
1 => 'nonce="AJRUc5Jx0UQbv5SJ9FoyUnaZpqZIHDhLTU+Awn/K0Uw=',
2 => 'qop="auth,auth-int',
3 => 'charset=utf-8',
4 => 'algorithm=md5-sess',
),
1 =>
array (
0 => 'realm',
1 => 'nonce',
2 => 'qop',
3 => 'charset',
4 => 'algorithm',
),
2 =>
array (
0 => 'smtp.domain.net',
1 => 'AJRUc5Jx0UQbv5SJ9FoyUnaZpqZIHDhLTU+Awn/K0Uw=',
2 => 'auth,auth-int',
3 => '',
4 => '',
),
3 =>
array (
0 => '',
1 => '',
2 => '',
3 => 'utf-8',
4 => 'md5-sess',
),
)
Then I can fill out a useful array with this loop:
$authvars = array();
foreach( $auth_matches[1] as $key => $val ) {
if( !empty($auth_matches[2][$key]) ) {
$authvars[$val] = $auth_matches[2][$key];
} else {
$authvars[$val] = $auth_matches[3][$key];
}
}
Which gives me:
array (
'realm' => 'ns103.zabco.net',
'nonce' => 'xITX1qgqlCDmYX6IrctN0WZRx7+Q4W7jjaXCCeUZnU8=',
'qop' => 'auth,auth-int',
'charset' => 'utf-8',
'algorithm' => 'md5-sess',
)
It's not exactly pretty, but it gets the job done.
$decodedString = 'realm="smtp.domain.net",nonce="AJRUc5Jx0UQbv5SJ9FoyUnaZpqZIHDhLTU+Awn/K0Uw=",qop="auth,auth-int",charset=utf-8,algorithm=md5-sess';
parse_str(preg_replace('/(?:(")(.*?)("))?,(?:(")(.*?)("))?/','$1$2$3&$4$5$6',$decodedString), $values);
var_dump($values);
If you want to get rid of the quotes around the resultant array values as well, use
$values = array_map(
function ($value) {
return trim($value,'"');
},
$values
);
var_dump($values);

How to get data from variable array length?

I have this code:
$ItemID = 'a62442e2-ca1f-4fd1-b80d-0d0dc511758e';
$GET_FreeTextFields = new \Picqer\Financials\Exact\ItemExtraField($connection);
$FreeTextFields = $GET_FreeTextFields->filter("ItemID eq guid'$ItemID'", '', '' );
$FreeTextFields01 = array();
$FreeTextFields02 = array();
foreach($FreeTextFields as $GET_FreeTextFields){
$FreeTextFields01[] = $GET_FreeTextFields->Value;
$FreeTextFields02[] = $GET_FreeTextFields->Number;
}
print_r($FreeTextFields01);
print_r($FreeTextFields02);
This outputs:
Value Array
(
[0] => 390
[1] => 804715
[2] => WW001
[3] => WHT/WHT/WHT
[4] => 39/42
[5] => 804715 WW00139/42
[6] => 3pk Quarter Socks
)
Numbers Array
(
[0] => 3
[1] => 4
[2] => 5
[3] => 6
[4] => 7
[5] => 8
[6] => 10
)
What this needs to output:
What i want if i use the first output so with 6 values in the array:
$FreeTextField01 = null
$FreeTextField02 = null
$FreeTextField03 = 390
$FreeTextField04 = 804715
$FreeTextField05 = WW001
$FreeTextField06 = WHT/WHT/WHT
$FreeTextField07 = 39/42
$FreeTextField08 = 804715 WW00139/42
$FreeTextField09 = null
$FreeTextField10 = 3pk Quarter Socks
But with other $ItemID, it can also output:
Value Array
(
[0] => 10100153
[1] => 2007
[2] => 350
[3] => 804082
[4] => WW006
[5] => WHT/NNY/OXGM
[6] => 35/38
[7] => 804082 WW00635/38
[8] => 0,00138857
[9] => Champion 3pk Quarter Socks
)
Numbers Array
(
[0] => 1
[1] => 2
[2] => 3
[3] => 4
[4] => 5
[5] => 6
[6] => 7
[7] => 8
[8] => 9
[9] => 10
)
What i want is that if a variable is not in the numbers list so 1-10, setting it to empty, and if the numbers is in the numbers array setting that number to the corresponding value variable. For example the number at [0] is 1 then set the variable $FreeTextField1 to $NumbersArray[0]->Value.
I keep making all kinds off loops but i just get stuck at the fact that the array length changes so that [6] can be the number 10 at one $itemID, but at another $ItemID, the number can be 6.
I tried researching this but I don't even know what I have to type in google to find this problem, that's why I'm describing it here.
edit i tried describing it a second time:
Yeah I'm having problems describing what i want, so let me try again. I get two arrays as output one with numbers that correspond with place it stands, as example you have FreeTextField0 thru FreeTextField10. What i tried to do is if (Numbers[0] == 0){ $FreeTextField0 = Value[0]}, but then I get the problem that Numbers[0] can be 3 or something else because if FreeTextField1 is empty i don't get a null value but nothing.
What i want if i use the first output so with 6 values in the array:
$FreeTextField01 = null
$FreeTextField02 = null
$FreeTextField03 = 390
$FreeTextField04 = 804715
$FreeTextField05 = WW001
$FreeTextField06 = WHT/WHT/WHT
$FreeTextField07 = 39/42
$FreeTextField08 = 804715 WW00139/42
$FreeTextField09 = null
$FreeTextField10 = 3pk Quarter Socks
I think this is what you're after but I have to say that I think you're barking up the wrong tree here. You really should not dynamically create variables in your script. To me, it is a serious code-smell and I think you should evaluate your design here.
http://sandbox.onlinephpfunctions.com/code/b245a0218ce174e68508139872f394def5409b05
<?php
// Test case
$values = [
'390',
'804715',
'WW001',
'WHT/WHT/WHT',
'39/42',
'804715 WW00139/42',
'3pk Quarter Socks'
];
$numbers = [3, 4, 5, 6, 7, 8, 10];
// Flip the $numbers array and then use the keys to find the corresponding values in the $values array
$intersection = array_unique(array_intersect_key($values, array_flip($numbers)));
// Fill in the missing keys and use `null` as the value
$output = $intersection + array_fill_keys(range(1,10), null);
// Sort the final output by the keys
ksort($output);
// Format the keys to match FreeTextField{00}
$output = array_combine(
array_map(function($k){ return 'FreeTextField'.str_pad($k, 2, '0', STR_PAD_LEFT); }, array_keys($output)),
$output
);
// Use the extract function to bring all those array keys + values into the symbol table.
// You can now use $FreeTextField01 - $FreeTextField10
extract($output);
var_dump($output);
UPDATE
http://sandbox.onlinephpfunctions.com/code/244c4f1ed45db398c48b8330c402b375eb358446
<?php
// Test case
$input = [
['Value' => '390', 'Number' => 3],
['Value' => '804715', 'Number' => 4],
['Value' => 'WW001', 'Number' => 5],
['Value' => 'WHT/WHT/WHT', 'Number' => 6],
['Value' => '39/42', 'Number' => 7],
['Value' => '804715 WW00139/42', 'Number' => 8],
['Value' => '3pk Quarter Socks', 'Number' => 10],
];
$intersection = [];
foreach ($input as $config) {
$value = $config['Value'];
$number = $config['Number'];
$intersection[$number] = $value;
}
// Fill in the missing keys and use `null` as the value
$output = $intersection + array_fill_keys(range(1,10), null);
// Sort the final output by the keys
ksort($output);
// Format the keys to match FreeTextField{00}
$output = array_combine(
array_map(function($k){ return 'FreeTextField'.str_pad($k, 2, '0', STR_PAD_LEFT); }, array_keys($output)),
$output
);
// Use the extract function to bring all those array keys + values into the symbol table.
// You can now use $FreeTextField01 - $FreeTextField10
extract($output);
var_dump($output);
Use ${$var}
If you want to see a doc about this type of var you can see it here:
http://php.net/manual/en/language.variables.variable.php

PHP compare multiple string

I have a lot of links:
http://exmpale.com/abc/exmpale.html
http://exmpale.com/abc/exmpale1.html
http://exmpale.com/abc/exmpale2.html
http://exmpale.com/abc/exmpale/exmpale1.html
http://exmpale.com/abc/exmpale/exmpale2.html
http://exmpale.com/abc/exmpale/exmpale3.html
http://exmpale.com/abcd/exmpale1.html
http://exmpale.com/abcd/exmpale2.html
http://exmpale.com/abcd/exmpale2.html
http://exmpale.com/abcd/exmpale4.html
http://exmpale.com/abc/abc/exmpale1.html
http://exmpale.com/abc/abc/exmpale2.html
http://exmpale.com/abc/abc/exmpale3.html
http://exmpale.com/abc/abc/exmpale4.html
http://exmpale.com/abc/abc/exmpale5.html
I use php, i want to get all links like http://exmpale.com/abc/(*).html and list it in array.
I'm sorry, I forgot code. code of me
$a = array(
0 => 'http://exmpale.com/abc/exmpale.html',
1 => 'http://exmpale.com/abc/exmpale1.html',
2 => 'http://exmpale.com/abc/exmpale2.html',
3 => 'http://exmpale.com/abc/exmpale/exmpale1.html',
4 => 'http://exmpale.com/abc/exmpale/exmpale2.html',
5 => 'http://exmpale.com/abc/exmpale/exmpale3.html',
6 => 'http://exmpale.com/abcd/exmpale1.html',
7 => 'http://exmpale.com/abcd/exmpale2.html',
8 => 'http://exmpale.com/abcd/exmpale2.html',
9 => 'http://exmpale.com/abcd/exmpale4.html',
10 => 'http://exmpale.com/abc/abc/exmpale1.html',
11 => 'http://exmpale.com/abc/abc/exmpale2.html',
12 => 'http://exmpale.com/abc/abc/exmpale3.html',
13 => 'http://exmpale.com/abc/abc/exmpale4.html',
14 => 'http://exmpale.com/abc/abc/exmpale5.html',
);
foreach($a as $k => $v ){
$abc = preg_match_all('/http\:\/\/exmpale\.com\/abc\/(.*?)\.html/',$v,$b);
print_r($b[1]);
}
and I want to get the result.
$result = array(
'http://exmpale.com/abc/exmpale.html',
'http://exmpale.com/abc/exmpale1.html',
'http://exmpale.com/abc/exmpale2.html');
but it took all result, please help me
thanks you.
Simply use the php function preg_match_all and the matched string will get loaded in an array that you supply to the above function as an argument. Please read the PHP manual on preg_match_all and create a regex for your needs. You can test your regex here
Since you dont provide what kind of data structure the urls are stored in, I assume it's a raw string:
$data_raw = "http://exmpale.com/abc/exmpale.html
http://exmpale.com/abc/exmpale1.html
http://exmpale.com/abc/exmpale2.html
http://exmpale.com/abc/exmpale/exmpale1.html
http://exmpale.com/abc/exmpale/exmpale2.html
http://exmpale.com/abc/exmpale/exmpale3.html
http://exmpale.com/abcd/exmpale1.html
http://exmpale.com/abcd/exmpale2.html
http://exmpale.com/abcd/exmpale2.html
http://exmpale.com/abcd/exmpale4.html
http://exmpale.com/abc/abc/exmpale1.html
http://exmpale.com/abc/abc/exmpale2.html
http://exmpale.com/abc/abc/exmpale3.html
http://exmpale.com/abc/abc/exmpale4.html
http://exmpale.com/abc/abc/exmpale5.html";
$urls = explode("\n", $data_raw);
$accept_host = "exmpale.com";
$accept_path = "/abc/";
$filtered_urls = array_filter($urls, function($url) use($accept_host, $accept_path) {
$parts = parse_url($url);
return $parts['host'] == $accept_host
&& strpos($parts['path'], $accept_path) === 0
&& strpos($parts['path'], '/', strlen($accept_path)) === false;
});
printf("<pre>%s</pre>", print_r($filtered_urls, true));
Yields:
Array
(
[0] => http://exmpale.com/abc/exmpale.html
[1] => http://exmpale.com/abc/exmpale1.html
[2] => http://exmpale.com/abc/exmpale2.html
)

multiple array combine into other array with key using php

We have three array something like this but these are dynamic
Array
(
[04/07/2013] => 2
[05/02/2013] => 1
[06/02/2013] => 1
[08/07/2013] => 2
[08/08/2013] => 3
[09/07/2013] => 2
[11/07/2013] => 1
[16/03/2013] => 1
[17/07/2013] => 1
[18/04/2013] => 2
[18/07/2013] => 1
[21/05/2013] => 2
[24/05/2013] => 8
[25/04/2013] => 2
[26/04/2013] => 1
[26/06/2013] => 1
[30/05/2013] => 1
)
Array
(
[01/08/2013] => 42
[02/08/2013] => 2
[03/08/2013] => 3
[07/08/2013] => 29
[09/08/2013] => 4
[10/08/2013] => 4
[11/08/2013] => 31
[19/07/2013] => 4
[20/07/2013] => 4
[22/07/2013] => 13
[23/07/2013] => 69
[29/07/2013] => 4
[31/07/2013] => 5
)
Array
(
[13/02/2013] => 2
[26/04/2013] => 2
[04/06/2013] => 2
[20/06/2013] => 2
[04/07/2013] => 2
[09/07/2013] => 1
[01/08/2013] => 1
[07/08/2013] => 1
[08/08/2013] => 3
)
We want to combine into other array with keys(key must be remain same).If all three array keys has same put it into the same key other wise create a key for and put it value if array has key other wise put it with zero value.
Here we tried for this
$maximum = max($countVisi,$countClic,$countClai);
if($countClic==$maximum){
$maxim = $clickArray;
}elseif($countVisi>=$maximum){
$maxim = $visitArray;
}elseif($countClai>=$maximum){
$maxim = $claimsArray;
}else{
$maxim = $visitArray;
}
we count the maximum index array and foreach the loop like this
foreach($maxim as $key=>$values){
if($visitArray[$key]){
$vv[$key] = $visitArray[$key];
}else{
$vv[$key] = 0;
}
if($clickArray[$key]){
$cc[$key] = $clickArray[$key];
}else{
$cc[$key] = 0;
}
if($claimsArray[$key]){
$kk[$key] = $claimsArray[$key];
}else{
$kk[$key] = 0;
}
$combineArrayNext[$key][] = $vv[$key];
$combineArrayNext[$key][] = $cc[$key];
$combineArrayNext[$key][] = $kk[$key];
//$vvvvv = explode('/' , $key);
//$myKey[$key] = "'".date('d M Y' , strtotime($vvvvv[2]."/".$vvvvv[1]."/".$vvvvv[0]))."'";
}
The problem is that we are getting only according to max array key.It leave the key which are not exits in max array.
Sorry for the less explaining , I think you understand my problem.Please share some idea with us to solve my problem.
Thanks
Hope this code helps you:
$dates = array_merge(array_keys($arr1), array_keys($arr2), array_keys($arr3));
foreach($dates as $date) {
$new_arr[$date] = array('click' => isset($arr1[$date])? $arr1[$date] : 0,
'visit' => isset($arr2[$date])? $arr2[$date] : 0,
'claim' => isset($arr3[$date])? $arr3[$date] : 0 );
}

Merging 2 arrays using array_merge_recursive not working properly

My database has 2 columns. the person's ID is repeated 5 times in 1 column and each its paired in another column with a question answer
Something like this:
ID Answer
1 A1
1 A4
1 A2
1 A9
1 A3
12 A1
12 A11
12 A12
12 A17
12 A2
What i want to try to do is to merge all the answers into 1 array with its ID
something like
array (
[1] => array ( 0 => 'A1', 1 => 'A4', 2 => 'A2', 3 => 'A9', 4 => 'A3'),
[12] => array ( 0 => 'A1', 1 => 'A11', 2 => 'A12', 3 => 'A17', 4 => 'A2')
)
My code is as follows:
foreach ($quiz_answers as $aq => $aa)
{
$array_loop = array( $aa['response_id'] => array( $aa['answer'] ) );
$ss = array_merge_recursive($array_loop, $array_loop);
}
My Problem is that somehow the loop doesnt merge in the desired way and i only get 2 outputs. I am not very good at manipulating arrays and probably i need another function but i am not quite sure what i am missing. I've tried using another variable in array_merge_recursive($anotherVariable, $array_loop); but this doesnt work either.
Simply change your foreach loop to construct the resulting array how you desire.
foreach ($quiz_answers as $aa) {
$ss[$aa['response_id']][] = $aa['answer'];
}
This gives an $ss array as you want:
array(
1 => array('A1', 'A4', 'A2', 'A9', 'A3'),
12 => array('A1', 'A11', 'A12', 'A17', 'A2'),
)
What you need to do is to have an array that holds the user ids, each of which is an associative array that holds an array of answers.
array (
[1] => array ( 0 => 'A1', 0 => 'A4', 0 => 'A2', 0 => 'A9', 0 => 'A3'),
[12] => array ( 0 => 'A1', 0 => 'A11', 0 => 'A12', 0 => 'A17', 0 => 'A2')
)
so something like this will work (skeleton code below)
$answers = array();
foreach( $quiz_answers as $id => $ans ) {
// check if $answers[$id] exists, otherwise create it here....
if(exists(....)) ...
else $answers[$id] = array();
// Then add the current answer to it, as below
array_push($answers[$id], $ans );
}
echo "<pre />";
for($i=0;$i<count($array);$i++)
{
$output[$array[$i]['ID']][]=$array[$i]['Answer'];
}
print_r($output);
$quiz_answers = // get all the data from database.
$res = array();
foreach($quiz_answers as $key=>$val){
$res[$val['response_id']][] = $val['answer'];
}
print_r($res);

Resources