Goal here is to take text from a log file, plain text delimited by returns with each line delimited by a varying number of spaces.
Getting it into a multi-dimensional is easy enough, getting rid of blank spaces is not. I suppose there are a lot of messy ways to go about this but isn't it one of the reasons for array_filter()?
$alarms = array(
"1530 1545 Place_4 Fault_1",
"1617 1622 Place_1 Fault_2",
"1634 1640 Place_2 Fault_1"
);
foreach ($alarms as $data) {
$subArr = explode(" ", $data);
array_filter($subArr);
print_r($subArr);
echo "<br /><br />";
}
Output:
Array ( [0] => 1530 [1] => [2] => [3] => [4] => [5] => 1545 [6] => [7] => [8] => [9] => [10] => Place_4 [11] => [12] => [13] => [14] => [15] => Fault_1 )
Array ( [0] => 1617 [1] => [2] => [3] => [4] => [5] => 1622 [6] => [7] => [8] => [9] => [10] => Place_1 [11] => [12] => [13] => [14] => [15] => Fault_2 )
Array ( [0] => 1634 [1] => [2] => [3] => [4] => [5] => 1640 [6] => [7] => [8] => [9] => [10] => Place_2 [11] => [12] => [13] => [14] => [15] => Fault_1 )
Want it to be:
Array ( [0] => 1530 [1] => 1545 [2] => Place_4 [3] => Fault_1 )
Array ( [0] => 1617 [1] => 1622 [2] => Place_1 [3] => Fault_2 )
Array ( [0] => 1634 [1] => 1640 [2] => Place_2 [3] => Fault_1 )
Not sure what's wrong...
Cheers.
From the manual, array_filter:
Return values:
Returns the filtered array.
Emphasis on returns. Currently you ignore the return value.
Rather than explode(" ",$something), try this:
$parts = preg_split("/ +/",$something);
This will split on variable numbers of spaces.
updated code
$alarms = array(
"1530 1545 Place_4 Fault_1",
"1617 1622 Place_1 Fault_2",
"1634 1640 Place_2 Fault_1"
);
foreach ($alarms as $data) {
$subArr = explode(" ", $data);
$subArr = array_filter($subArr);
print_r($subArr);
echo "<br /><br />";
}
Related
I have doubt in php i want to set an associative array for which i have the keys ,as well as values.
i have an array $headers and a mutidimentional array $data as follows:
$headers=(
[0] => Testcase Name
[1] => Cell Name
[2] => Customer
[3] => Flops
[4] => Title
[5] => Status
[6] => Mfix CCR(open/close)
[7] => Scenerio-Brief Description
[8] => Expected Results
[9] => CCR Status
[10] => CCR No.
[11] => Remarks
[12] => Testcase Path
)
$data=(
[0] => Array
(
[0] => /a/b/c
[1] =>
[2] =>
[3] =>
[4] =>
[5] => Done
[6] => close
[7] => 2D Elastic with scanformat=parallel
[8] => No miscompares for both scan and logic tests
[9] =>
[10] => 1716280
[11] =>
[12] =>
)
[1] => Array
(
[0] => /x/y/z
[1] =>
[2] =>
[3] =>
[4] =>
[5] => Done
[6] => close
[7] => 2D Elastic with scanformat=parallel & explicitshifts
[8] => No miscompares for both scan and logic tests
[9] =>
[10] => 1717028
[11] =>
[12] =>
)
[2] => Array
(
[0] => /a/p/q
[1] =>
[2] =>
[3] =>
[4] =>
[5] => Done
[6] =>
[7] => Error if explicitshifts greater than scan length
[8] => No miscompares for both scan and logic tests
[9] =>
[10] =>
[11] =>
[12] =>
)
[3] => Array
(
[0] => /s/m/p
[1] =>
[2] =>
[3] =>
[4] =>
[5] => Done
[6] =>
[7] => 2D Elastic + wide 1 Masking with scanformat=parallel
[8] => No miscompares for both scan and logic tests
[9] =>
[10] =>
[11] =>
[12] =>
)
)
I want to set the numeric keys [0]....[12] as the values of $headers array.
Means i want to replace [0]....[12] with $header[0]....$headers[12].
Please provide a solution.
Use array_combine:
$dataWithKeys = [];
foreach ($data as $row) {
$dataWithKeys[] = array_combine($headers, $row);
}
$result = array();
foreach($data as $key => $val){
$temp = array();
foreach($val as $k => $v){
$temp[$header[$k]] = $v;
}
$result[] = $temp;
}
I am sending a big array of 130+ indices from ajax to php. But while going to php, if i print, it became a 2D array with indices 63, 63, 6 respectively.
Below is snip
Array
(
[0] => Array
(
[0] => 943900200
[1] => 1297017000
[2] => 1299436200
[3] => 1302114600
[4] => 1304879400
[5] => 1307385000
................
[60] => 1452105000
[61] => 1454869800
[62] => 1457375400
)
[1] => Array
(
[0] => 943900200
[1] => 1297017000
[2] => 1299436200
[3] => 1302114600
[4] => 1304879400
[5] => 1307385000
......
[61] => 1454869800
[62] => 1457289000
)
[2] => Array
(
[0] => 1440441000
[1] => 1443033000
[2] => 1445970600
[3] => 1445970600
[4] => 1447007400
[5] => 1448908200
)
)
But i want them in a single D array...[0]--[127] together. I tried to copy them using foreach aswell. It copies the first 63 indices and stops. Any one please help. Thanks
You must use foreach twice:
INTPUT (example):
Array
(
[0] => Array
(
[0] => 5031750
[1] => 3972258
[2] => 1673731
[3] => 721866
[4] => 4031885
[5] => 1454990
)
[1] => Array
(
[0] => 1115002
[1] => 27608
[2] => 3531620
[3] => 4412066
[4] => 4032217
[5] => 2681734
)
[2] => Array
(
[0] => 3360879
[1] => 5190034
[2] => 3452229
[3] => 5112636
[4] => 628357
[5] => 4299124
)
)
PHP Code:
$output = array();
foreach($input as $key=>$sub){
foreach($sub as $k => $v){
$output[] = $v;
}
}
print_r($output);
Output:
Array
(
[0] => 5031750
[1] => 3972258
[2] => 1673731
[3] => 721866
[4] => 4031885
[5] => 1454990
[6] => 1115002
[7] => 27608
[8] => 3531620
[9] => 4412066
[10] => 4032217
[11] => 2681734
[12] => 3360879
[13] => 5190034
[14] => 3452229
[15] => 5112636
[16] => 628357
[17] => 4299124
)
How to explode this array value:
$row = array(
[0] = 1,1,Carlyle,Rogers,1,"Carlyle, Rogers",0000-00-00,,carlyle.rogers#stafford-trust.com,,non premium)
I tried this code
$values = explode(',', $row[0]);
and give me this wrong output:
Array (
[0] => 1
[1] => 1
[2] => Carlyle
[3] => Rogers
[4] => 1
[5] => "Carlyle
[6] => Rogers"
[7] => 0000-00-00
[8] =>
[9] => carlyle.rogers#stafford-trust.com
[10] =>
[11] => non premium
)
What I want is to Output must be like this one:
Array (
[0] => 1
[1] => 1
[2] => Carlyle
[3] => Rogers
[4] => 1
[5] => "Carlyle, Rogers"
[6] => 0000-00-00
[7] =>
[8] => carlyle.rogers#stafford-trust.com
[9] =>
[10] => non premium
)
You can't use explode like that because your input seems to be CSV-formatted and explode knows nothing about that "format". Use str_getcsv instead:
$values = str_getcsv($row[0]);
I have php array like this:
Array (
[0] => Array ([electronics] => TV [condition] => GOOD [1] => 100 [2] => [3] => [4] => [5] => [6] => [7] => [8] => [9] => [10] => )
[1] => Array ([electronics] => TV [condition] => NOTGOOD [1] => 50 [2] => [3] => [4] => [5] => [6] => [7] => [8] => [9] => [10] => )
[2] => Array ([electronics] => AC [condition] => GOOD [1] => 200 [2] => [3] => [4] => [5] => [6] => [7] => [8] => [9] => [10] => )
[3] => Array ([electronics] => TV [condition] => GOOD [1] => 50 [2] => 30 [3] => [4] => [5] => [6] => [7] => [8] => [9] => [10] => )
[4] => Array ([electronics] => AC [condition] => GOOD [1] => 50 [2] => 30 [3] => [4] => [5] => [6] => [7] => [8] => [9] => [10] => )
)
Then I want to group and calculation by [electronics] and [condition].
(calculated when found same [electronics] and [condition] in that array).
And the result I want like this:
Array (
[0] => Array ([electronics] => TV [condition] => GOOD [1] => 150 [2] => 30 [3] => [4] => [5] => [6] => [7] => [8] => [9] => [10] => )
[1] => Array ([electronics] => TV [condition] => NOTGOOD [1] => 50 [2] => [3] => [4] => [5] => [6] => [7] => [8] => [9] => [10] => )
[2] => Array ([electronics] => AC [condition] => GOOD [1] => 250 [2] => 30 [3] => [4] => [5] => [6] => [7] => [8] => [9] => [10] => )
)
This should be one way to do it:
$result = array();
foreach ($arrays as $array) {
$key = $array['electronics'] . $array['condition'];
foreach ($array as $k => $v) {
if (is_numeric($k)) {
$result[$key][$k] += $v;
} else {
$result[$key][$k] = $v;
}
}
}
$result = array_values($result);
This is my array:
[abominado] => Array
(
[0] => réprobo
[1] => réprobo
[2] => abominado
[3] => banido
[4] => condenado
[5] => detestado
[6] => odiado
[7] => precito
[8] => renegado
[9] => repudiado
)
[abominar] => Array
(
[0] => repelir
[1] => repelir
[2] => abominar
[3] => afastar
[4] => afugentar
[5] => arredar
[6] => desalojar
[7] => desviar
[8] => detestar
[9] => empuxar
[10] => escorraçar
[11] => espinafrar
[12] => execrar
[13] => exercer
[14] => expulsar
[15] => grimpar
[16] => impugnar
[17] => odiar
[18] => rebater
[19] => rechaçar
[20] => recusar
[21] => rejeitar
[22] => relegar
[23] => repudiar
)
how can I insert it into sql ?
foreach ($abominado as $key=>$str)
{
$string .= "$key:$str\n";
}
mysql_query("INSERT INTO strings VALUES ('".mysql_real_escape_string($string)."')");
You can use serialize to cast it to string and unserialize when you get it
First, these arrays happen to be sequential.
Second, in a SQL table, any column can be used as a search criteria, so just insert the data directly and set up an index on any searchable column.