Explode String using PHP - php

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

Related

Php array remove duplicate and put toghether in another array

I have this php array:
Array
(
[0] => Array
(
[0] => 2505
[1] => Lima
[2] => Daniels
[3] => 0996995904
[4] =>
[5] => 755971
[6] => 1454284800
[7] => Cat. A (Moto)
[8] => 0
[9] => 1
[10] => 1
)
[1] => Array
(
[0] => 2505
[1] => Lima
[2] => Daniels
[3] => 0996995904
[4] =>
[5] => 755971
[6] => 1454284800
[7] => Cat. A (Moto)
[8] => 1
[9] => 0
[10] => 0
)
[2] => Array
(
[0] => 2505
[1] => Lima
[2] => Daniels
[3] => 0996995904
[4] =>
[5] => 755971
[6] => 1454284800
[7] => Cat. A (Moto)
[8] => 0
[9] => 0
[10] => 0
)
[3] => Array
(
[0] => 2525
[1] => Lomarca
[2] => Miro
[3] => 0099778877
[4] =>
[5] => 768131
[6] => 1454976000
[7] => Cat. A (Moto)
[8] => 1
[9] => 0
[10] => 0
)
[4] => Array
(
[0] => 2525
[1] => Lomarca
[2] => Miro
[3] => 0099778877
[4] =>
[5] => 768131
[6] => 1454976000
[7] => Cat. A (Moto)
[8] => 0
[9] => 1
[10] => 0
)
)
I want i new array:
1) if element [0] exists in the array I want to control elements 8, 9 and 10 and if one of this is 1 I want to have 1 in the final array, but I don't want to have the same array 2 times (the index [0] is the key).
My final array shound be:
Array
(
[0] => Array
(
[0] => 2505
[1] => Lima
[2] => Daniels
[3] => 0996995904
[4] =>
[5] => 755971
[6] => 1454284800
[7] => Cat. A (Moto)
[8] => 1
[9] => 1
[10] => 1
)
[3] => Array
(
[0] => 2525
[1] => Lomarca
[2] => Miro
[3] => 0099778877
[4] =>
[5] => 768131
[6] => 1454976000
[7] => Cat. A (Moto)
[8] => 1
[9] => 1
[10] => 0
)
)
I have only one time the index 2505 and 2505 and index 8 and 9 and 10 are 1 for the 2505 and the index 8 and 9 are 1 for the 2525.
this does the job: (assuming that your first array is $arrays and your desired result is $result)
$result = array();
foreach($arrays as $array)
{
if(!isset($result[$array[0]]))
{
$result[$array[0]] = $array;
}
else
{
$result[$array[0]][8] = $array[8] == 1 ? 1 : $result[$array[0]][8];
$result[$array[0]][9] = $array[9] == 1 ? 1 : $result[$array[0]][9];
$result[$array[0]][10] = $array[10] == 1 ? 1 : $result[$array[0]][10];
}
}
but wherever you get your data from, e.g. a database, I would use element[0] as the key, so you don't get multiple entries in $arrays. Another option would be to create a class which holds your data...

How to set keys in a multidimentional array in php

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

array combine is not working in php?

my code is:
array set 1:
Array
(
[0] => 15-3
[1] => 16-3
[2] => 15-4
[3] => 16-4
[4] => 15-3
[5] => 16-3
[6] => 15-4
[7] => 16-4
[8] => 15-3
[9] => 16-3
[10] => 15-4
[11] => 16-4
)
My second array set is:
Array
(
[0] => 1
[1] => 1
[2] => 1
[3] => 1
[4] => 2
[5] => 2
[6] => 2
[7] => 2
[8] => 3
[9] => 3
[10] => 3
[11] => 3
)
i just combine the above both the two array into one like below
$data1=array_combine($store_attri_ids, $store_ids);
but its shows like that
Array
(
[15-3] => 3
[16-3] => 3
[15-4] => 3
[16-4] => 3
)
The remaining values are not combined, wt we do nw????
You can try this by creating a sub-array -
$data1 = array();
foreach($store_attri_ids as $key => $id) {
$data1[$id][] = $store_ids[$key];
}
The output would be like -
Array
(
[15-3] => array(1, 2, 3),
[16-3] => array(...),
[15-4] => array(...),
[16-4] => array(...)
)
If you use array_combine the result is totally correct, like putting here: http://php.net/manual/en/function.array-combine.php
You need use array_merge, take a look at the documentation :http://php.net/manual/en/function.array-merge.php
Use like this:
$data1=array_merge($store_attri_ids, $store_ids);
print_r($data1);

PHP array_filter() not working

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 />";
}

array manipulation

I have a two column array (array1), for each row of that array I need to compare the value in the 2nd column with a column value in each row of another array(array1) , when they equal I want to append another column value (from array2) to the first array.
in english:
if array1[x][1] = array2[y][0]
then array1[x][2] = array2[y][2]
screen dumps of both arrays
array1 (
[1] => Array (
[0] => 1 [1] => 2
)
[2] => Array (
[0] => 2 [1] => 3
)
[3] => Array (
[0] => 3 [1] =>
) [7] => Array (
[0] => 7 [1] => 1
)
[8] => Array (
[0] => 8 [1] => 1
)
[9] => Array (
[0] => 9 [1] => 10
)
[10] => Array (
[0] => 10 [1] => 2
)
)
array2 (
[0] => Array (
[0] => 1
[1] => 2
[2] => 2
[3] => Jane
[4] => Smith
[5] => jsmith#internet.com
[6] => jsmith
[7] => 12345
[8] => 1
[9] => no
)
[1] => Array (
[0] => 2
[1] => 2
[2] => 3
[3] => James
[4] => Beard
[5] => jasb#bellsouth.net
[6] => jbeard03
[7] => keeper
[8] => 1
[9] => no
)
[2] => Array (
[0] => 3
[1] => 2
[2] =>
[3] => Peter
[4] => Allen
[5] => pallen#rfgg.com
[6] => pallen
[7] => pallen
[8] => 1
[9] => no
)
[3] => Array (
[0] => 7
[1] => 2
[2] => 1
[3] => Joe
[4] => Blow
[5] => jasb#bellsouth.net
[6] => jblow
[7] => blow123
[8] => 5
[9] => yes
)
[4] => Array (
[0] => 8
[1] => 2
[2] => 1
[3] => John
[4] => Smith
[5] => logtest#bellsouth.net
[6] => jnsmith
[7] => jsmith123
[8] => 4
[9] => yes
)
[5] => Array (
[0] => 9
[1] => 2
[2] => 10
[3] => Frank
[4] => Smith
[5] => pallen#test.com
[6] => fsmith
[7] => fsmith123
[8] => 4
[9] => yes
)
[6] => Array (
[0] => 10
[1] => 2
[2] => 2
[3] => Loretta
[4] => Beard
[5] => lbeard#me.net
[6] => lbeard
[7] => lbeard123
[8] => 1
[9] => no
)
)
Does this work? I'm not sure I'm entirely clear on what you're looking for.
foreach($array1 as $x => $xarray) {
foreach($array2 as $y => $yarray) {
if($xarray[1] == $yarray[0]) {
$array1[$x][2] = $array[$y][2];
}
}
}
foreach ($array1 as &$a1) {
foreach ($array2 as $a2) {
if ($a1[1] == $a2[0]) {
$a1[] = $a2[2];
continue 2;
}
}
}
BTW, you should try to use explicit keys that actually mean something, e.g. $a1['id'] instead of $a1[1]. You'll thank yourself when you look at the code again two months down the road.
And because I threatened to do so:
btwyoushouldtrytouseexplicitkeysthatactuallymeansomethingeg$a1['id']insteadof$a1[1]youllthankyourselfwhenyoulookatthecodeagaintwomonthsdowntheroad ;-P

Categories