I have an array of sample user string inputs which may or may not have smarty variables in them which id like to escape with {literal}{/literal} tags.
Array
(
[0] => {$PLEASE}
[1] => {PLEASE}
[2] => {{PLEASE}}
[3] => {{{PLEASE}}}
[4] => {a{PLEASE}}
[5] => {a{$PLEASE}}
[6] => {{$PLEASE}a}
[7] => {{PLEASE}a}
[8] => {{{$PLEASE}}}
[9] => {{{{PLEASE}}}}
)
Here is what I hope to achieve.
Array
(
[0] => {$PLEASE}
[1] => {literal}{PLEASE}{/literal}
[2] => {literal}{{PLEASE}}{/literal}
[3] => {literal}{{{PLEASE}}}{/literal}
[4] => {literal}{a{PLEASE}{/literal}
[5] => {literal}{a{/literal}{$PLEASE}{literal}}{/literal}
[6] => {literal}{{/literal}{$PLEASE}{literal}a}{/literal}
[7] => {literal}{PLEASE}a}{/literal}
[8] => {literal}{{{/literal}{$PLEASE}{literal}}}{/literal}
[9] => {literal}{{{{PLEASE}}}}{/literal}
)
Right now I have this
$data = preg_replace('/{+([^\$])([a-z0-9]*)}+/si', '{literal}{\1\2}{/literal}', $data);
Which gives me
Array
(
[0] => {$PLEASE}
[1] => {literal}{PLEASE}{/literal}
[2] => {literal}{PLEASE}{/literal}
[3] => {literal}{PLEASE}{/literal}
[4] => {a{literal}{PLEASE}{/literal}
[5] => {a{$PLEASE}}
[6] => {{$PLEASE}a}
[7] => {literal}{PLEASE}{/literal}a}
[8] => {{{$PLEASE}}}
[9] => {literal}{PLEASE}{/literal}
)
Been stuck for quite sometime now, was wondering if anyone could help me figure it out or if its even possible to do so.
ok, I'm sure there's a more elegant way, perhaps one-liner, but whatever, it works with the following:
//Step 1: Replace 'real' smarty variables with an intermediate string
$data1 = preg_replace('/{(\$\w+)}/', "!!!$1!!!", $arr);
//replace start and end curly braces with {literal}:
$data2 = preg_replace('/{(.*)}/', '{literal}{$1}{/literal}', $data);
//Replace all inner smarty variables with their original string:
$data3 = preg_replace('/.!!!(.*)!!!/', '{/literal}$1{literal}', $data2);
//Replace standalone variables with their original string:
$data4 = preg_replace('/^!!!(.*)!!!$/', '{$1}', $data3);
You can merge steps 3&4 in one command
Related
i want to create something using array. I have 1 array and i need to split it into 2 array. After that search specific items from both array and remove it then combine it 2 array into 1 array.
How do i do that?
I already try to use unset for array but confuse how to use it for specific key since my array data format like 16/2/1/1 and 16/2/1/5. I need to remove data which have 1.
My format array is like this
Array
(
[1] => Array
(
[0] => 16/2/1/1 --> remove this have 1 after 2
[1] => 16/2/0/2
[2] => 16/2/0/3
[3] => 16/2/0/4
[4] => 16/2/0/5
[5] => 16/2/0/6
[6] => 16/2/0/7
[7] => 16/2/0/8
[8] => 16/2/0/9
[9] => 16/2/0/10
[10] => 16/2/0/11
[11] => 16/2/0/12
[12] => 16/2/0/13
[13] => 16/2/0/14
[14] => 16/2/0/15
[15] => 16/2/0/16
)
[2] => Array
(
[0] => 16/2/0/1
[1] => 16/2/0/2
[2] => 16/2/0/3
[3] => 16/2/0/4
[4] => 16/2/1/5 --> and this have 1 after 2 before 5
[5] => 16/2/0/6
[6] => 16/2/0/7
[7] => 16/2/0/8
[8] => 16/2/0/9
[9] => 16/2/0/10
[10] => 16/2/0/11
[11] => 16/2/0/12
[12] => 16/2/0/13
[13] => 16/2/0/14
[14] => 16/2/0/15
[15] => 16/2/0/16
)
)
i expect the output something like (after combine)
Array
(
[0] => 16/2/0/2
[1] => 16/2/0/3
[2] => 16/2/0/4
[3] => 16/2/0/6
[4] => 16/2/0/7
[5] => 16/2/0/8
[6] => 16/2/0/9
[7] => 16/2/0/10
[8] => 16/2/0/11
[9] => 16/2/0/12
[10] => 16/2/0/13
[11] => 16/2/0/14
[12] => 16/2/0/15
[13] => 16/2/0/16
)
Thanks for time to help me.
Make the array unique and then extract items that are digits/digits/NOT 1/digits:
$array = preg_grep('#^\d+/\d+/[^1]/\d+#', array_unique($array));
I would use preg_grep which allows you to search an array using a Regular expression.
$array =[
'16/2/0/13',
'16/2/0/16',
'16/2/1/5'
];
$array = preg_grep('~^16/2/0/\d+$~', $array);
print_r($array);
Output
Array
(
[0] => 16/2/0/13
[1] => 16/2/0/16
)
Sandbox
The Regex
^ match start of string
16/2/0/ - match literally (at the start of string, see above)
\d+ any digit one or more
$ match end of string
So Regular expressions is a way to do pattern matching, in this case the pattern is 16/2/0/{n} where {n} is any number. So by doing this we can find only those items that match that pattern.
Then if you have duplicates, you can do array_unique() and easily remove those.
There are many ways to do this array_filter with a custom callback etc. But this is the most straightforward way (if you know Regex).
I have an arbitrary number of arrays all containing the same format of data. There are 2 separate for loops looping through two separate SQL query results and adding them to 2 separate arrays.
Once I have all the information in both arrays, I am walking through them and joining them together to make a longer array.
However, as I am writing this array to a csv file, The information needs to be in order in the array so it writes it in order to the csv file. How can I do this?
Array 1
[1] => Array
(
[0] => 2017-07-21 00:00:00
[1] => Foo
[2] => Bar
[3] => 32.63
[4] => 18.36
[5] => 98.46
)
[2] => Array
(
[0] => 2017-07-21 00:00:00
[1] => Foo
[2] => Bar
[3] => 29.74
[4] => 148.68
[5] => 178.42
)
//etc
Array 2
[1] => Array
(
[0] => RTGH707321222
[1] => THIS
[2] => IS
[3] => TEXT
)
[2] => Array
(
[0] => RTGH707321220
[1] => SOME
[2] => WORDS
[3] => HERE
)
//etc
Joining the arrays together
array_walk($array2, function($values, $key) use (&$array1) {
$array1[$key] = array_merge($array1[$key], $values);
} );
After The array Merge - print_r($array1)
[1] => Array
(
[0] => 2017-07-21 00:00:00
[1] => Foo
[2] => Bar
[3] => 32.63
[4] => 18.36
[5] => 98.46
[6] => RTGH707321222
[7] => THIS
[8] => IS
[9] => TEXT
)
[2] => Array
(
[0] => 2017-07-21 00:00:00
[1] => Foo
[2] => Bar
[3] => 29.74
[4] => 148.68
[5] => 178.42
[6] => RTGH707321220
[7] => SOME
[8] => WORDS
[9] => HERE
)
//etc
So this is working fine. However, I would like to move some of these indexes around so that they are in a different order. I have looked into array_splice() but I am not sure if this is the correct method to use.
What I want it to look like
[1] => Array
(
[0] => 2017-07-21 00:00:00
[1] => RTGH707321222
[2] => TEXT
[3] => THIS
[4] => 18.36
[5] => 98.46
[6] => Foo
[7] => 32.63
[8] => IS
[9] => Bar
)
//etc
As you can see, all the information is still the same. The values have just been moved to different indexes. How can I sort the array so that it looks like the above. Can anyone point me in the right direction? Thanks.
This is a simpler method using array_replace() and an ordering array.
No extra loop, no temporary swapping variables.
Code: (Demo)
$array1=[
1=>['2017-07-21 00:00:00','Foo','Bar',32.63,18.36,98.46],
2=>['2017-07-21 00:00:00','Foo','Bar',29.74,148.68,178.42]
];
$array2=[
1=>['RTGH707321222','THIS','IS','TEXT'],
2=>['RTGH707321220','SOME','WORDS','HERE']
];
$order=[0=>'',6=>'',9=>'',7=>'',4=>'',5=>'',1=>'',3=>'',8=>'',2=>''];
array_walk($array2, function($values, $key) use (&$array1,$order) {
$array1[$key] = array_replace($order,array_merge($array1[$key], $values));
});
var_export($array1);
we can use swap technice here like,
<?php
foreach ($arr as $key => $value) {
$swap = $value[1];
$arr[$key][1] = $value[6];
$arr[$key][6] = $swap;
$swap = $value[9];
$arr[$key][9] = $value[2];
$arr[$key][2] = $swap;
$swap = $value[7];
$arr[$key][7] = $value[3];
$arr[$key][3] = $swap;
}
print_r($arr);
?>
$arr is your array.
I need to parse some data from a text and so i copy/pasted it into an array and used it like this:
$array = array("_alpha","_beta","_gama","_delta","_epsilon");
foreach ($array as $key => $value) {
{use the value in my script bellow....}
}
The data was coming from a script that had private declarations on the top of the file looked like this, so it was easy to just copy/paste it.
$private = ["_alpha","_beta","_gama","_delta","_epsilon"];
Now i had to parse many files so i tried to gather that $array data as best as i could into one array, so i can parse it using the same function...Well the best i could come out with is this:
Array
(
[0] => "_alpha","_delta","_beta","_epsilon","_delta","_kapa",
[1] => "_beta","_alpha","_delta","_kapa","_lamda","_epsilon","_array","_alpha"
[2] => "_epsilon","_array","_alpha","_theta","_omega"
[3] => "_alpha"
[4] => "_kapa","_lamda","_epsilon"
[5] => "_beta","_epsilon","_delta","_kapa","_lamda"
[6] => "_omega","_omega","_delta"
....
....
}
Each line represents the header from a file. Note the quotes around the values...
How can i make the above look like this:
Array
(
[0] => _alpha
[1] => _beta
[2] => _gama
[3] => _delta
[4] => _epsilon
...
...
..
)
Each value on its own, and without the quote or commas etc....just a plain value.
I can also extract the data in this format..but i think thats harder to do:
Array
(
[0] => Array
(
[0] => "_alpha"
[1] => "_beta"
[2] => "_gama"
[3] => "_delta"
[4] => "_epsilon"
...
...
..
)
[1] => Array
(
[0] => "_alpha"
[1] => "_beta"
[2] => "_gama"
[3] => "_delta"
[4] => "_epsilon"
[5] => "_epsilon"
...
..
)
...... etc etc ......
[10] => Array
(
[0] => "_alpha"
[1] => "_delta"
[2] => "_omega"
)
}
_Thanks
$private = ["_alpha","_beta","_gama","_delta","_epsilon"];
print_r($private);
Array
(
[0] => _alpha
[1] => _beta
[2] => _gama
[3] => _delta
[4] => _epsilon
)
i dont know whats the problem
but when i use your codes everything works fine
This is my $part_array:
Array (
[0] => 1015789
[1] => 1029402
[2] => 1031345
[3] => 1036476
[4] => 1061512
[5] => 1065031
[6] => 1069892
[7] => 1070721
[8] => 1073222
[9] => 1074811
)
$next_index = $next_index + 1;
$next_part = ($part_array[$next_index]);
Retrieve part# information using MySQL
Display part# and related information on page. Click on button to display next part# and related information and repeat until end, or user exits.
Each time I increment the $next_index and get the $next_part, the $part_array has the next part # added to the end of the array, so if I have read 3 records, my array now looks like this:
Array (
[0] => 1015789
[1] => 1029402
[2] => 1031345
[3] => 1036476
[4] => 1061512
[5] => 1065031
[6] => 1069892
[7] => 1070721
[8] => 1073222
[9] => 1074811
[10] => 1015789
[11] => 1029402
[12] => 1031345
)
How else could I do this without adding to the array? Does anyone have any suggestions? I am new to PHP and would greatly appreciate any advice.
I'm using preg_split to an string, but I'm not getting desired output. For example
$string = 'Tachycardia limit_from:1900-01-01 limit_to:2027-08-29 numresults:10 sort:publication-date direction:descending facet-on-toc-section-id:Case Reports';
$vals = preg_split("/(\w*\d?):/", $string, NULL, PREG_SPLIT_DELIM_CAPTURE);
is generating output
Array
(
[0] => Tachycardia
[1] => limit_from
[2] => 1900-01-01
[3] => limit_to
[4] => 2027-08-29
[5] => numresults
[6] => 10
[7] => sort
[8] => publication-date
[9] => direction
[10] => descending facet-on-toc-section-
[11] => id
[12] => Case Reports
)
Which is wrong, desire output it
Array
(
[0] => Tachycardia
[1] => limit_from
[2] => 1900-01-01
[3] => limit_to
[4] => 2027-08-29
[5] => numresults
[6] => 10
[7] => sort
[8] => publication-date
[9] => direction
[10] => descending
[11] => facet-on-toc-section-id
[12] => Case Reports
)
There something wrong with regex, but I'm not able to fix it.
I would use
$vals = preg_split("/(\S+):/", $string, NULL, PREG_SPLIT_DELIM_CAPTURE);
Output is exactly like you want
It's because the \w class does not include the character -, so i would expand the \w with that too:
/((?:\w|-)*\d?):/
Try this regex instead to include '-' or other characters in your splitting pattern: http://regexr.com?32qgs
((?:[\w\-])*\d?):