Working with checkbox array values and dumping into MYSQL - php

From a checkbox submit(post) i have an array like this. So this value will be dynamic in form submit. The variable name is $my_values.
Output
Array
(
[0] => 1
[1] => 2
[2] => 2_6
[3] => 3
[4] => 3_7
[5] => 3_8
[6] => 4
[7] => 4_9
[8] => 4_10
[9] => 4_11
[10] => 4_12
[11] => 4_13
[12] => 4_13_14
[13] => 5
)
Expected Output
1,2,3,4,5,6,7,8,9,10,11,12,13,14
So I need to get the output as above in a single variable. How can I achieve that?
In other words :
$my_values is having the array as i have mentioned. i want one more variable $my_results which will convert the array values and give it as a single value with comma seperator (i.e 1,2,3,4,5,6,7,8,9,10,11,12,13,14)
Thanks
Kimz

if you need each id just once, you can do something like this:
$tmp = implode(',', $my_values);
$tmp = str_replace('_', ',', $tmp);
$idList = explode(',', $tmp);
$my_results = implode(',', array_unique($idList));
echo $my_results;

Related

How can I put strings in array starting from 0 index

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

Compare Array elements and add based on key and value

I have two arrays like this:
$array_1 = Array ( [0] => 4 [1] => 6 [2] => 2 [3] => 6 [4] => 4 [5] => 10 [6] => 4 [7] => 6 [8] => 2 [9] => 2 [10] => 4 [11] => 4 [12] => 2 [13] => 2 );
$array_2 = Array ( [0] => DK [1] => GA [2] => DK [3] => GA [4] => DK [5] => GA [6] => WE [7] => VE [8] => WE [9] => VE [10] => PLA [11] => PRA [12] => PLA [13] => PRA ) ;
Now I want result like this:
$dk=4+2+4=10;
$ga=6+6+10=22;
$we=4+2=6;
$ve=6+2=8;
$pla=4+2=6;
$pra=4+2;
Explanation:
In $array_2, 'DK' exists 3 times and key values are = 0,2 and 4.
So, i have to add the values of $array_1 having key 0,2,4 and assign them to $dk. Here, $dk will be 4+2+4=10. This process will be same for all other variables.
How can i do this??
Instead separate variable name I suggest you to make array like this
<?php
$array_1 = [4,6,2,6];
$array_2 = [ 0=> "DK", 1=>"GA", 2=>"DK", 3=>"GA"];
$newArray = [];
foreach($array_2 as $key=>$value){
if(isset($newArray[$value])){
$newArray[$value] +=$array_1[$key];
}else{
$newArray[$value] =$array_1[$key];
}
}
print_r($newArray);
?>
Live Demo
Output :
Array
(
[DK] => 6
[GA] => 12
)
Another suggestion : Instead complex programming try to make good relation or binding to not get any inconsistency in records
This will loop array2 and build an array with the sum.
Then output it (just to see the result), then I use extract to pull out the variables as you want them.
But I would rather keep them in the array
Foreach($array_2 as $key => $val){
If(!isset($new[$val])) $new[$val] =0;
$new[$val] += $array_1[$key];
}
Var_dump($new);
Extract($new);
https://3v4l.org/jOR7Z

Reorganise array, move indexes to specific locations php

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.

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

Categories