How can I put strings in array starting from 0 index - php

hello I have a string like this
"*HQ,6170929875,V1,185905,A,3127.3354,N,07307.6954,E,000.09,000,060718,FFFFB9FF,410,04,03104,32566#*HQ,6170929875,V1,185915,A,3127.3365,N,07307.6951,E,002.30,018,060718,FFFFB9FF,410,04,03104,32566#*HQ,6170929875,V1,185925,A,3127.3372,N,07307.6952,E,000.76,000,060718,FFFFB9FF,410,04,03104,32566#*HQ,6170929875,V1,185935,A,3127.3369,N,07307.6947,E,000.27,000,060718,FFFFB9FF,410,04,03104,32566#*HQ,6170929875,V1,185945,A,3127.3371,N,07307.6951,E,000.27,000,060718,FFFFB9FF,410,04,03104,32566#*HQ,6170929875,V1,185955,A,3127.3377,N,07307.6952,E,000.21,000,060718,FFFFB9FF,410,04,03104,32566#*HQ,6170929875,V1,190005,A,3127.3376,N,07307.6950,E,000.17,000,060718,FFFFB9FF,410,04,03104,32566#*HQ,6170929875,V1,190015,A,3127.3376,N,07307.6953,E,000.07,000,060718,FFFFB9FF,410,04,03104,32566#*HQ,6170929875,V1,190025,A,3127.3375,N,07307.6955,E,000.59,000,060718,FFFFB9FF,410,04,03104,32566#*HQ,6170929875,V1,190035,A,3127.3373,N,07307.6944,E,000.35,000,060718,FFFFB9FF,410,04,03104,32566#*HQ,6170929875,V1,190045,A,3127.3378,N,07307.6950,E,000.23,000,060718,FFFFB9FF,410,04,03104,32566#*HQ,6170929875,V1,190055,A,3127.3381,N,07307.6955,E,000.32,000,060718,FFFFB9FF,410,04,03104,32566#"
I want to put all these values in an array. so I did this
$array = explode("*",$str);
Now when i printed the array the data is
Array
(
[0] =>
[1] => HQ,6170929875,V1,185905,A,3127.3354,N,07307.6954,E,000.09,000,060718,FFFFB9FF,410,04,03104,32566#
[2] => HQ,6170929875,V1,185915,A,3127.3365,N,07307.6951,E,002.30,018,060718,FFFFB9FF,410,04,03104,32566#
[3] => HQ,6170929875,V1,185925,A,3127.3372,N,07307.6952,E,000.76,000,060718,FFFFB9FF,410,04,03104,32566#
[4] => HQ,6170929875,V1,185935,A,3127.3369,N,07307.6947,E,000.27,000,060718,FFFFB9FF,410,04,03104,32566#
[5] => HQ,6170929875,V1,185945,A,3127.3371,N,07307.6951,E,000.27,000,060718,FFFFB9FF,410,04,03104,32566#
[6] => HQ,6170929875,V1,185955,A,3127.3377,N,07307.6952,E,000.21,000,060718,FFFFB9FF,410,04,03104,32566#
[7] => HQ,6170929875,V1,190005,A,3127.3376,N,07307.6950,E,000.17,000,060718,FFFFB9FF,410,04,03104,32566#
[8] => HQ,6170929875,V1,190015,A,3127.3376,N,07307.6953,E,000.07,000,060718,FFFFB9FF,410,04,03104,32566#
[9] => HQ,6170929875,V1,190025,A,3127.3375,N,07307.6955,E,000.59,000,060718,FFFFB9FF,410,04,03104,32566#
[10] => HQ,6170929875,V1,190035,A,3127.3373,N,07307.6944,E,000.35,000,060718,FFFFB9FF,410,04,03104,32566#
[11] => HQ,6170929875,V1,190045,A,3127.3378,N,07307.6950,E,000.23,000,060718,FFFFB9FF,410,04,03104,32566#
[12] => HQ,6170929875,V1,190055,A,3127.3381,N,07307.6955,E,000.32,000,060718,FFFFB9FF,410,04,03104,32566#
)
0 index is empty. I want to start the array from 0 index. Please help. what I am doing wrong here

The first element of your array is blank because your string begins with *. To solve this simply do:
$str = "*HQ,6170929875,V1,185905,A,3127.3354,N,07307.6954,E,000.09,000,060718,FFFFB9FF,410,04,03104,32566#*HQ,6170929875,V1,185915,A,3127.3365,N,07307.6951,E,002.30,018,060718,FFFFB9FF,410,04,03104,32566#*HQ,6170929875,V1,185925,A,3127.3372,N,07307.6952,E,000.76,000,060718,FFFFB9FF,410,04,03104,32566#*HQ,6170929875,V1,185935,A,3127.3369,N,07307.6947,E,000.27,000,060718,FFFFB9FF,410,04,03104,32566#*HQ,6170929875,V1,185945,A,3127.3371,N,07307.6951,E,000.27,000,060718,FFFFB9FF,410,04,03104,32566#*HQ,6170929875,V1,185955,A,3127.3377,N,07307.6952,E,000.21,000,060718,FFFFB9FF,410,04,03104,32566#*HQ,6170929875,V1,190005,A,3127.3376,N,07307.6950,E,000.17,000,060718,FFFFB9FF,410,04,03104,32566#*HQ,6170929875,V1,190015,A,3127.3376,N,07307.6953,E,000.07,000,060718,FFFFB9FF,410,04,03104,32566#*HQ,6170929875,V1,190025,A,3127.3375,N,07307.6955,E,000.59,000,060718,FFFFB9FF,410,04,03104,32566#*HQ,6170929875,V1,190035,A,3127.3373,N,07307.6944,E,000.35,000,060718,FFFFB9FF,410,04,03104,32566#*HQ,6170929875,V1,190045,A,3127.3378,N,07307.6950,E,000.23,000,060718,FFFFB9FF,410,04,03104,32566#*HQ,6170929875,V1,190055,A,3127.3381,N,07307.6955,E,000.32,000,060718,FFFFB9FF,410,04,03104,32566#";
$arr = explode('*', $str);
array_shift($arr);
print_r($arr);
Which produces:
Array
(
[0] => HQ,6170929875,V1,185905,A,3127.3354,N,07307.6954,E,000.09,000,060718,FFFFB9FF,410,04,03104,32566#
[1] => HQ,6170929875,V1,185915,A,3127.3365,N,07307.6951,E,002.30,018,060718,FFFFB9FF,410,04,03104,32566#
[2] => HQ,6170929875,V1,185925,A,3127.3372,N,07307.6952,E,000.76,000,060718,FFFFB9FF,410,04,03104,32566#
[3] => HQ,6170929875,V1,185935,A,3127.3369,N,07307.6947,E,000.27,000,060718,FFFFB9FF,410,04,03104,32566#
[4] => HQ,6170929875,V1,185945,A,3127.3371,N,07307.6951,E,000.27,000,060718,FFFFB9FF,410,04,03104,32566#
[5] => HQ,6170929875,V1,185955,A,3127.3377,N,07307.6952,E,000.21,000,060718,FFFFB9FF,410,04,03104,32566#
[6] => HQ,6170929875,V1,190005,A,3127.3376,N,07307.6950,E,000.17,000,060718,FFFFB9FF,410,04,03104,32566#
[7] => HQ,6170929875,V1,190015,A,3127.3376,N,07307.6953,E,000.07,000,060718,FFFFB9FF,410,04,03104,32566#
[8] => HQ,6170929875,V1,190025,A,3127.3375,N,07307.6955,E,000.59,000,060718,FFFFB9FF,410,04,03104,32566#
[9] => HQ,6170929875,V1,190035,A,3127.3373,N,07307.6944,E,000.35,000,060718,FFFFB9FF,410,04,03104,32566#
[10] => HQ,6170929875,V1,190045,A,3127.3378,N,07307.6950,E,000.23,000,060718,FFFFB9FF,410,04,03104,32566#
[11] => HQ,6170929875,V1,190055,A,3127.3381,N,07307.6955,E,000.32,000,060718,FFFFB9FF,410,04,03104,32566#
)

You could pass it to array_filter to remove first child (index 0). It happen because you have * on first part of string. Exploding by * delimiter mean zero string on first value.
Something like:
$array = explode("*",$str);
$array = array_filter($array);

This is occurring simply because you have a "*" character at the start of the string
*HQ,6170929875,V1,185905,A,3127.3354,N,07307.6954,E,000.09,000,060718,FFFFB9FF,410,04,03104,32566#*....
If you always have that character at the start of the string, you will always have an empty index 0 in your resultant array - you need to either remove the first character before exploding the string or the first item of the array.

You might use a combination of array_filter to remove the empty values and array_values to reindex the array to start from 0:
$str = "*HQ,6170929875,V1,185905,A,3127.3354,N,07307.6954,E,000.09,000,060718,FFFFB9FF,410,04,03104,32566#*HQ,6170929875,V1,185915,A,3127.3365,N,07307.6951,E,002.30,018,060718,FFFFB9FF,410,04,03104,32566#*HQ,6170929875,V1,185925,A,3127.3372,N,07307.6952,E,000.76,000,060718,FFFFB9FF,410,04,03104,32566#*HQ,6170929875,V1,185935,A,3127.3369,N,07307.6947,E,000.27,000,060718,FFFFB9FF,410,04,03104,32566#*HQ,6170929875,V1,185945,A,3127.3371,N,07307.6951,E,000.27,000,060718,FFFFB9FF,410,04,03104,32566#*HQ,6170929875,V1,185955,A,3127.3377,N,07307.6952,E,000.21,000,060718,FFFFB9FF,410,04,03104,32566#*HQ,6170929875,V1,190005,A,3127.3376,N,07307.6950,E,000.17,000,060718,FFFFB9FF,410,04,03104,32566#*HQ,6170929875,V1,190015,A,3127.3376,N,07307.6953,E,000.07,000,060718,FFFFB9FF,410,04,03104,32566#*HQ,6170929875,V1,190025,A,3127.3375,N,07307.6955,E,000.59,000,060718,FFFFB9FF,410,04,03104,32566#*HQ,6170929875,V1,190035,A,3127.3373,N,07307.6944,E,000.35,000,060718,FFFFB9FF,410,04,03104,32566#*HQ,6170929875,V1,190045,A,3127.3378,N,07307.6950,E,000.23,000,060718,FFFFB9FF,410,04,03104,32566#*HQ,6170929875,V1,190055,A,3127.3381,N,07307.6955,E,000.32,000,060718,FFFFB9FF,410,04,03104,32566#";
print_r(array_values(array_filter(explode("*", $str))));
Result:
Array
(
[0] => HQ,6170929875,V1,185905,A,3127.3354,N,07307.6954,E,000.09,000,060718,FFFFB9FF,410,04,03104,32566#
[1] => HQ,6170929875,V1,185915,A,3127.3365,N,07307.6951,E,002.30,018,060718,FFFFB9FF,410,04,03104,32566#
[2] => HQ,6170929875,V1,185925,A,3127.3372,N,07307.6952,E,000.76,000,060718,FFFFB9FF,410,04,03104,32566#
[3] => HQ,6170929875,V1,185935,A,3127.3369,N,07307.6947,E,000.27,000,060718,FFFFB9FF,410,04,03104,32566#
[4] => HQ,6170929875,V1,185945,A,3127.3371,N,07307.6951,E,000.27,000,060718,FFFFB9FF,410,04,03104,32566#
[5] => HQ,6170929875,V1,185955,A,3127.3377,N,07307.6952,E,000.21,000,060718,FFFFB9FF,410,04,03104,32566#
[6] => HQ,6170929875,V1,190005,A,3127.3376,N,07307.6950,E,000.17,000,060718,FFFFB9FF,410,04,03104,32566#
[7] => HQ,6170929875,V1,190015,A,3127.3376,N,07307.6953,E,000.07,000,060718,FFFFB9FF,410,04,03104,32566#
[8] => HQ,6170929875,V1,190025,A,3127.3375,N,07307.6955,E,000.59,000,060718,FFFFB9FF,410,04,03104,32566#
[9] => HQ,6170929875,V1,190035,A,3127.3373,N,07307.6944,E,000.35,000,060718,FFFFB9FF,410,04,03104,32566#
[10] => HQ,6170929875,V1,190045,A,3127.3378,N,07307.6950,E,000.23,000,060718,FFFFB9FF,410,04,03104,32566#
[11] => HQ,6170929875,V1,190055,A,3127.3381,N,07307.6955,E,000.32,000,060718,FFFFB9FF,410,04,03104,32566#
)

Related

How to split 1 array into 2 arrays, remove certain items, and combine them again into 1 array in PHP?

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

PHP Exploding first part of a string into array elements and the second part into one element

I have a string which gets exploded into an array using the space as a delimiter. Is it possible to , for example explode the first 4 words into the array and the rest into ONE array element?
as of now the code is like this
$string = 'This is a string that needs to be split into elements';
$splitarray = explode(' ',$string);
This gives an array
Array
(
[0] => This
[1] => is
[2] => a
[3] => string
[4] => that
[5] => needs
[6] => to
[7] => be
[8] => split
[9] => into
[10] => elements
)
What i need is for the array to look like this
Array
(
[0] => This
[1] => is
[2] => a
[3] => string
[4] => that
[5] => needs
[6] => to be split into elements
)
Is something like this possible?
Use limit parameter here.
From explode() documentation:
If limit is set and positive, the returned array will contain a maximum of limit elements with the last element containing the rest of string.
Code:
$string = 'This is a string that needs to be split into elements';
$splitarray = explode(' ',$string, 7);
print_r($splitarray);
Output:
Array
(
[0] => This
[1] => is
[2] => a
[3] => string
[4] => that
[5] => needs
[6] => to be split into elements
)

remove spaces in a array php

for this array,
Array (
[0] => 'HOST:'
[1] => 'killbill'
[2] =>
[3] =>
[4] =>
[5] =>
[6] =>
[7] =>
[8] =>
[9] =>
[10] =>
[11] => 'Loss%'
[12] =>
[13] =>
[14] => 'Snt'
[15] =>
[16] =>
[17] => 'Last'
[18] =>
[19] =>
[20] =>'id'
)
it has empty values.by using this code it gives
foreach($array as $key => $subarray) {
$array[$key] = preg_grep('/^$/', $subarray, PREG_GREP_INVERT);
}
array (
[0] => HOST:
[1] => killbill
[11] => Loss%
[14] => Snt
[17] => Last
[20] =>id
)
that means it removes all the spaces. but it has the original key values .(compair above one and bellow one. then can get a clear idea what i'm saying).but i want to have it like this.
array (
[0] => 'HOST:'
[1] => 'killbill'
[2] => 'Loss%'
[3] => 'Snt'
[4] => 'Last'
[5] => 'id'
)
key values as 1,2,3,4.... so how could i get that.
simply use this instead of Foreach
array_values(array_filter($array));
that will remove the space and reorder your array.
look: http://codepad.org/howl3Opj
Just use array_filter().
$array = array_filter($array);
That will remove all the empty values -- ie blank, null, false, zero.
If you only want to remove values that are empty strings and keep other empty values (eg zero), you can specify the second parameter for array_filter(), which allows you to define a function to specify which elements should be filtered.
$array = array_filter($array, function($val) {return $val!=='';});
Hope that helps.
try this function it will help you to sort out the issue
$arr = array_map('array_values', $arr);
Use array_diff function
<?php
$array_space = array(0,3,4,45,12,"",54,23);
$remove = array("");
print_r(array_diff($array_space,$remove));
?>
See Output here
at start you need to take the array i gave him a name $arrWords
$arrWords = array_filter($arrWords, 'trim');
after i clean all the empty spaces i will make new array keys
$arrWords = array_values($arrWords);

escaping user input while keeping smarty variables

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

preg_split with regex giving incorrect output

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?):

Categories