been woking on a project of mine for a few days now, and essentialy what i'm triing to do in this project is a comparison of csv file that a user normaly does in excel, to do it in php automaticly every night.
I got the info from the CSV's intro arrays, but i'm having trouble combinig them to get the right info for every book, hence the folowing exemple and question.
I have the folowing array (array1) from a csv file :
Array
(
[0] => Array
(
[0] => book1
[1] => description1
[2] => category1
[3] => code1
[4] => editor1
[5] => 0
[6] => eur
[7] => out of stoc
)
[1] => Array
(
[0] => book2
[1] => description2
[2] => category2
[3] => code2
[4] => editor2
[5] => 0
[6] => curr2
[7] => out of stoc
)
[2] => Array
(
[0] => book3
[1] => description3
[2] => category3
[3] => code3
[4] => editor3
[5] => 0
[6] => curr3
[7] => out of stoc
)
[3] =>
)
and another array (array2) from a second csv file :
Array
(
[0] => Array
(
[0] => book1
[1] => description_from_array2
[2] => category_from_array2
[3] => code_from_array2
[4] => editor_from_array2
[5] => 12
[6] => eur
[7] => in stoc
)
[1] => Array
(
[0] => book2
[1] => description_from_array2
[2] => category_from_array2
[3] => code_from_array2
[4] => editor_from_array2
[5] => 13
[6] => eur
[7] => in stoc
)
[2] => Array
(
[0] => book4
[1] => description_from_array2
[2] => category_from_array2
[3] => code_from_array2
[4] => editor_from_array2
[5] => 14
[6] => usd
[7] => in stoc
)
[3] => Array
(
[0] => book5
[1] => description_from_array2
[2] => category_from_array2
[3] => code_from_array2
[4] => editor_from_array2
[5] => 16
[6] => usd
[7] => in stoc
)
)
I would like to know how to get the values form array2 intro array1 for the books of array1 found in array2.
Ex:
Array
(
[0] => Array
(
[0] => book1
[1] => description2_from_array2
[2] => category2_from_array2
[3] => code2_from_array2
[4] => editor2_from_array2
[5] => 12
[6] => eur
[7] => in stoc
)
[1] => Array
(
[0] => book2
[1] => description_from_array2
[2] => category_from_array2
[3] => code_from_array2
[4] => editor_from_array2
[5] => 13
[6] => curr_from_array2
[7] => in stoc
)
[2] => Array
(
[0] => book3
[1] => description3
[2] => category3
[3] => code3
[4] => editor3
[5] => 0
[6] => curr3
[7] => out of stoc //because book3 is not found in array2
)
[3] =>
)
Any help for this question would be greatly appreciated, belive me!
//Add keys to array 2 to aid lookup
$array2Tmp = array();
foreach($array2 as $item){
$array2Tmp[$item[0]] = $item;
}
$array3 = array();
foreach($array1 as $item){
//Use item from second array if it exists
if(array_key_exists($item[0],$array2Tmp)){
$array3[] = $array2Tmp[$item[0]];
}else{
$array3[] = $item;
}
}
Related
Hello I have the following scenario:
The following array is to be changed to a two-dimensional array.
Array
(
[0] => Array
(
[0] => Name1
[1] => Name2
[2] => Name3
[3] => Name4
[4] => Name5
[5] => Name6
[6] => Name7
[7] => Name8
)
[1] => Array
(
[0] => Company1
[1] => Company2
[2] => Company3
[3] => Company4
[4] => Company5
[5] => Company6
[6] => Company7
[7] => Company8
)
[2] => Array
(
[0] => Street1
[1] => Street2
[2] => Street3
[3] => Street4
[4] => Street5
[5] => Street6
[6] => Street7
[7] => Street8
)
[3] => Array
(
[0] => Date1
[1] => Date2
[2] => Date3
[3] => Date4
[4] => Date5
[5] => Date6
[6] => Date7
[7] => Date8
)
[4] => Array
(
[0] => Date_2_1
[1] => Date_2_2
[2] => Date_2_3
[3] => Date_2_4
[4] => Date_2_5
[5] => Date_2_6
[6] => Date_2_7
[7] => Date_2_8
)
[5] => Array
(
[0] => place1
[1] => place2
[2] => place3
[3] => place4
[4] => place5
[5] => place6
[6] => place7
[7] => place8
)
[6] => Array
(
[0] => break1
[1] => break2
[2] => break3
[3] => break4
[4] => break5
[5] => break6
[6] => break7
[7] => break8
)
[7] => Array
(
[0] => postcode1
[1] => postcode2
[2] => postcode3
[3] => postcode4
[4] => postcode5
[5] => postcode6
[6] => postcode7
[7] => postcode8
)
)
How the final array should look like
Array
(
[0] => Array
(
[0] => Name1
[1] => Company1
[2] => Street1
[3] => Date1
[4] => Date_2_1
[5] => place1
[6] => break1
[7] => postcode1
)
[1] => Array
(
[0] => Name2
[1] => Company2
[2] => Street2
[3] => Date2
[4] => Date_2_2
[5] => place2
[6] => break2
[7] => postcode2
)
[2] => Array
(
[0] => Name3
[1] => Company3
[2] => Street3
[3] => Date3
[4] => Date_2_3
[5] => place3
[6] => break3
[7] => postcode3
)
[3] => Array
(
[0] => Name4
[1] => Company4
[2] => Street4
[3] => Date4
[4] => Date_2_4
[5] => place4
[6] => break4
[7] => postcode4
)
[4] => Array
(
[0] => Name5
[1] => Company5
[2] => Street5
[3] => Date5
[4] => Date_2_5
[5] => place5
[6] => break5
[7] => postcode5
)
[5] => Array
(
[0] => Name6
[1] => Company6
[2] => Street6
[3] => Date6
[4] => Date_2_6
[5] => place6
[6] => break6
[7] => postcode6
)
[6] => Array
(
[0] => Name7
[1] => Company7
[2] => Street7
[3] => Date7
[4] => Date_2_7
[5] => place7
[6] => break7
[7] => postcode7
)
[7] => Array
(
[0] => Name8
[1] => Company8
[2] => Street8
[3] => Date8
[4] => Date_2_8
[5] => place8
[6] => break8
[7] => postcode8
)
)
function test($post_employee_nr){
require_once $_SERVER['DOCUMENT_ROOT'].'/module/dienstplan/_config.php';
$employee_query = $dbh->query("SELECT FT.*,M.*,O.*,E.*,K.* FROM
finish_time FT
LEFT JOIN
m_schicht M ON FT.m_schichtid = M.ID
LEFT JOIN
objekte O ON O.ID = M.objid
LEFT JOIN
mitarbeiter E ON E.ID = M.mitarbeiterid
LEFT JOIN
kunde K ON K.ID = M.kdid where FT.mitarbeiterid=$post_employee_nr")->fetchall();
foreach ($employee_query as $row) {
$employee_ID[] = $row['FT.ID'];
$customer[] = $row['kundenname'];
$street[] = $row['straße'];
$postcode[] = $row['plz'];
$place[] = $row['ort'];
$begin[] = $row['b_time'];
$end[] = $row['e_time'];
$break[] = $row['pause'];
$output = array($employee_ID, $customer,$street,$postcode,$place,$begin,$end,$break);
}
$html = $output;
$response = $html;
echo json_encode($response);
}
I hope I could make it obvious enough
EDIT
This is my solution:
$result = array();
foreach($employee_query as $employee_query) {
$result[] = array(
$days[date('l', strtotime($employee_query['b_time']))],
date("d.m.Y", strtotime($employee_query['b_time'])),
$employee_query['kundenname'],
$employee_query['strasse'],
$employee_query['plz'].' '.$employee_query['ort'],
date("H:i", strtotime($employee_query['b_time'])),
date("H:i", strtotime($employee_query['e_time'])),
'<i class="fas fa-plus-circle" style="color:green;"></i>'
);
}
echo json_encode($result);
exit();
}
If you want to change it to a 2 layer array just create a variable that holds the first element of the 3 layer array:
somthing like:
var array2D = array3D[0];
Also this bit of code seems like its not necesarry
$html = array($output);
$response = $html;
echo json_encode($response);
unless you need the array to be 3 layers when encoding it to Json. Otherwise just change it to:
echo json_encode($output);
Hopefully I understood your question and was able to help a little.
Edit
The way your foreach loop is currently running you're only creating a new array for each element then adding the same elements to their respective array and finally storing every array inside a new one (1 array with ALL id's, and one with ALL companynames etc...)
to fix it is very simple.
inside your foreach loop the $row variable looks like this:
$row => [idvalue, companyvalue, streetvalue etc....]
it's already an array containing the current $row's data, now all you need to do is directly add it to your $output array.
You're new foreach loop should look something like this:
foreach($employee_query as $row) {
$output[] = $row; // when using [] after a variable you add to that array
}
echo json_encode($output);
if you don't want to use all the data that your query collected you can specify which attributes you want to use like so:
$output[] = array($row['FT.ID'], $row['kundenname'], $row['straße'], etc...);
Array
(
[csv_data] => Array
(
[0] => Array
(
[0] => CAM
[1] => Partner
[2] => Division
[3] => Domain
[4] => Year
[5] => Quarter
[6] => Tactic
[7] => Impressions
[8] => Responders
)
[1] => Array
(
[0] => CAM
[1] => Acme and Brick
[2] => Belgium
[3] => www.partnerA.com
[4] => 2016
[5] => Q2
[6] => Single Email Campaign
[7] => 8000
[8] => 6000
)
[2] => Array
(
[0] =>
[1] => Acme and Brick
[2] => Belgium
[3] => www.partnerA.com
[4] => 2016
[5] => Q2
[6] => Multi-Touch Email Campaign
[7] => 350
[8] => 200
)
[3] => Array
(
[0] => TestR
[1] => Partner R2
[2] => India
[3] => www.partnerA.com
[4] => 2016
[5] => Q1
[6] => Single Email Campaign
[7] => 9000
[8] => 4000
)
[4] => Array
(
[0] =>
[1] => Partner R2
[2] => India
[3] => www.partnerA.com
[4] => 2016
[5] => Q2
[6] => Linkedin(Groups)
[7] => 350
[8] => 200
)
)
)
Hello, i am new in PHP.
i just want that in this array i want to add the particular array key values and identify that it sholud be has the same partner and divison after identify the values of all particular should be add in new array.
ANSWER should be like this:
Array
(
[csv_data] => Array
(
[0] => Array
(
[0] => CAM
[1] => Partner
[2] => Division
[3] => Domain
[4] => Year
[5] => Quarter
[6] => Tactic
[7] => Impressions
[8] => Responders
)
[1] => Array
(
[0] => CAM
[1] => Acme and Brick
[2] => Belgium
[3] => www.partnerA.com
[4] => 2016
[5] => Q2
[6] => Single Email Campaign
[7] => 8350
[8] => 6200
)
[2] => Array
(
[0] => TestR
[1] => Partner R2
[2] => India
[3] => www.partnerA.com
[4] => 2016
[5] => Q1
[6] => Single Email Campaign
[7] => 9350
[8] => 4200
)
)
)
Try this code, this will definitely work for you..
$removeKeys = array();
foreach($data['csv_data'] as $key => $val)///loop through array..
{
foreach($data['csv_data'] as $k => $v)
{
if($val[1] == $v[1] && $key != $k)////check if key 1 matches
{
if(!in_array($key,$removeKeys)) ////check if item is already added or not
{
$removeKeys[] = $k; ///push into removed keys because this is added into matched item
$data['csv_data'][$key][7]+=$data['csv_data'][$k][7];
$data['csv_data'][$key][8]+=$data['csv_data'][$k][8];
}
}
}
}
foreach($removeKeys as $rk)
{
unset($data['csv_data'][$rk]); ////remove all the keys in removeKeys
}
print_r($data['csv_data']);///your desired output...
This will give you :
Array
(
[0] => Array
(
[0] => CAM
[1] => Partner
[2] => Division
[3] => Domain
[4] => Year
[5] => Quarter
[6] => Tactic
[7] => Impressions
[8] => Responders
)
[1] => Array
(
[0] => CAM
[1] => Acme and Brick
[2] => Belgium
[3] => www.partnerA.com
[4] => 2016
[5] => Q2
[6] => Single Email Campaign
[7] => 8350
[8] => 6200
)
[3] => Array
(
[0] => TestR
[1] => Partner R2
[2] => India
[3] => www.partnerA.com
[4] => 2016
[5] => Q1
[6] => Single Email Campaign
[7] => 9350
[8] => 4200
)
)
LIVE EXAMPLE : CLICK HERE
I am using MultipleIterator() to iterate through two different arrays and get each element.
My code
$d = new MultipleIterator ();
$d->attachIterator ( new ArrayIterator ( $tbl_one_data ) );
$d->attachIterator ( new ArrayIterator ( $tbl_two_data ) );
foreach ( $d as $data ) {
print_r($data);
}
Which generates the following :
My question is how do I loop through the array and return each element? For example I would like to return 2014-11-06 11:31:58.781018. Tried using $data[0][0] but this returns all elements in the first index but I only want one element.
EDIT
print_r($d);
MultipleIterator Object ( [storage:SplObjectStorage:private] => Array ( [00000000583bd67b000000000ac6c449] => Array ( [obj] => ArrayIterator Object ( [storage:ArrayIterator:private] => Array ( [0] => Array ( [0] => 1 [1] => 2014-11-06 11:31:58.781018 [2] => NONE [3] => NONE ) [1] => Array ( [0] => 2 [1] => 2014-11-06 11:31:58.799436 [2] => MANAGER [3] => 500 ) [2] => Array ( [0] => 3 [1] => 2014-11-06 11:31:58.841035 [2] => MANAGER [3] => 501 ) [3] => Array ( [0] => 4 [1] => 2014-11-06 11:33:00.741873 [2] => MANAGER [3] => 500 ) [4] => Array ( [0] => 5 [1] => 2014-11-06 11:33:00.802389 [2] => MANAGER [3] => 501 ) [5] => Array ( [0] => 6 [1] => 2014-11-06 13:15:49.457646 [2] => MANAGER [3] => 500 ) [6] => Array ( [0] => 7 [1] => 2014-11-06 13:37:16.259128 [2] => NONE [3] => NONE ) [7] => Array ( [0] => 8 [1] => 2014-11-06 13:37:16.275201 [2] => NONE [3] => 500 ) [8] => Array ( [0] => 9 [1] => 2014-11-06 13:37:27.682873 [2] => NONE [3] => NONE ) [9] => Array ( [0] => 10 [1] => 2014-11-06 13:37:27.690863 [2] => NONE [3] => 500 ) [10] => Array ( [0] => 11 [1] => 2014-11-06 13:52:21.108003 [2] => MANAGER [3] => 500 ) [11] => Array ( [0] => 12 [1] => 2014-11-06 14:17:01.266769 [2] => NONE [3] => NONE ) [12] => Array ( [0] => 13 [1] => 2014-11-06 14:17:01.279507 [2] => node1-1415283420.0 [3] => 500 ) [13] => Array ( [0] => 14 [1] => 2014-11-06 14:17:02.527183 [2] => node1-1415283420.0 [3] => 500 ) [14] => Array ( [0] => 15 [1] => 2014-11-06 14:17:23.775279 [2] => node1-1415283442.1 [3] => 500 ) ) ) [inf] => ) [00000000583bd67a000000000ac6c449] => Array ( [obj] => ArrayIterator Object ( [storage:ArrayIterator:private] => Array ( [0] => Array ( [0] => NONE [1] => QUEUESTART [2] => [3] => [4] => [5] => [6] => ) [1] => Array ( [0] => Local/120#disc-agents/n [1] => ADDMEMBER [2] => [3] => [4] => [5] => [6] => ) [2] => Array ( [0] => Local/120#disc-agents/n [1] => ADDMEMBER [2] => [3] => [4] => [5] => [6] => ) [3] => Array ( [0] => Local/120#disc-agents/n [1] => REMOVEMEMBER [2] => [3] => [4] => [5] => [6] => ) [4] => Array ( [0] => Local/120#disc-agents/n [1] => REMOVEMEMBER [2] => [3] => [4] => [5] => [6] => ) [5] => Array ( [0] => Local/120#disc-agents/n [1] => ADDMEMBER [2] => [3] => [4] => [5] => [6] => ) [6] => Array ( [0] => Local/120#disc-agents/n [1] => PAUSEALL [2] => [3] => [4] => [5] => [6] => ) [7] => Array ( [0] => Dunc Test [1] => PAUSE [2] => [3] => [4] => [5] => [6] => ) [8] => Array ( [0] => Local/120#disc-agents/n [1] => UNPAUSEALL [2] => [3] => [4] => [5] => [6] => ) [9] => Array ( [0] => Dunc Test [1] => UNPAUSE [2] => [3] => [4] => [5] => [6] => ) [10] => Array ( [0] => Local/120#disc-agents/n [1] => REMOVEMEMBER [2] => [3] => [4] => [5] => [6] => ) [11] => Array ( [0] => NONE [1] => QUEUESTART [2] => [3] => [4] => [5] => [6] => ) [12] => Array ( [0] => NONE [1] => ENTERQUEUE [2] => [3] => 363 [4] => 1 [5] => [6] => ) [13] => Array ( [0] => NONE [1] => ABANDON [2] => 1 [3] => 1 [4] => 1 [5] => [6] => ) [14] => Array ( [0] => NONE [1] => ENTERQUEUE [2] => [3] => 363 [4] => 1 [5] => [6] => ) ) ) [inf] => ) ) )
if this is really output by print_r then $data[0][1] must be '2014-11-06 11:31:58.781018' . I test it by code:
$data = array(array('1','2014-11-06 11:31:58.781018'));
echo '<pre>';
print_r($data); echo '<br>';
echo 'what we want: '.$data[0][1].'<br>';
otput:
Array
(
[0] => Array
(
[0] => 1
[1] => 2014-11-06 11:31:58.781018
)
)
what we want: 2014-11-06 11:31:58.781018
If you have the $data as an array you can access the value: 2014-11-06 11:31:58.781018 by using,
$date = $data[0][1];
i have the folowing array read from a csv file:
Array
(
[0] => Array
(
[0] => book1
[1] => description1
[2] => category1
[3] => code1
[4] => editor1
[5] => 0
[6] => eur
[7] => out of stoc
)
[1] => Array
(
[0] => book2
[1] => description2
[2] => category2
[3] => code2
[4] => editor2
[5] => 0
[6] => curr2
[7] => out of stoc
)
[2] => Array
(
[0] => book3
[1] => description3
[2] => category3
[3] => code3
[4] => editor3
[5] => 0
[6] => curr3
[7] => out of stoc
)
)
Code :
function read_CSV_raza($rzs_csv) {
$file_handle = fopen($rzs_csv, 'r');
while (!feof($file_handle)) {
$line_of_text[] = fgetcsv($file_handle, 1024, ';');
}
fclose($file_handle);
return $line_of_text;
}
$rzs_csv = 'rzs.csv';
$csv = read_CSV_raza($rzs_csv);
I want to add another array (headers) in order to write this intro another csv file.
EX :
Array
(
[0] => Array
(
[0] => h1
[1] => h2
[2] => h3
[3] => h4
[4] => h5
[5] => h6
[6] => h7
[7] => h8
)
[1] => Array
(
[0] => book1
[1] => description1
[2] => category1
[3] => code1
[4] => editor1
[5] => 0
[6] => eur
[7] => out of stoc
)
[2] => Array
(
[0] => book2
[1] => description2
[2] => category2
[3] => code2
[4] => editor2
[5] => 0
[6] => curr2
[7] => out of stoc
)
[3] => Array
(
[0] => book3
[1] => description3
[2] => category3
[3] => code3
[4] => editor3
[5] => 0
[6] => curr3
[7] => out of stoc
)
)
I tried with array_merge(); array_unshift(); but to no avail, i do not get the results i need.
The cod for writing the array in the new CSV is :
$fp = fopen('rezultate.csv', 'w') or die("Can't open rezultate.csv");
foreach ($csv as $items) {
fputcsv($fp, $items, ';', '"');
}
fclose($fp) or die("Can't close rezultate.csv");
As i am not a programmer any help with this issue would be greatly appreciated.
array_unshift worked for me:
$add = array (
0 => "h1",
1 => "h2",
2 => "h3",
3 => "h4",
4 => "h5",
5 => "h6",
6 => "h7",
7 => "h8"
);
array_unshift($myarray, $add);
And also
array_unshift($myarray, array(0 => "h1", 1 => "h2", 2 => "h3", 3 => "h4", 4 => "h5", 5 => "h6", 6 => "h7", 7 => "h8"));
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