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"));
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...);
I have following array. How I can delete those values which doesn't have value in [1]? So if there's not "x", it will be deleted.
Before:
Array
(
[0] => Array
(
[0] => 1
[1] => x
[2] => name1
[3] => company1
[4] => 709
)
[1] => Array
(
[0] => 2
[1] => x
[2] => name2
[3] => company2
[4] => 500
)
.
.
.
[978] => Array
(
[0] => 946
[1] =>
[2] => name946
[3] => company946
[4] => 0
)
[979] => Array
(
[0] => 946
[1] => x
[2] => name946
[3] => company946
[4] => 0
)
[980] => Array
(
[0] => 946
[1] =>
[2] => name946
[3] => company946
[4] => 0
)
)
After:
Array
(
[0] => Array
(
[0] => 1
[1] => x
[2] => name1
[3] => company1
[4] => 709
)
[1] => Array
(
[0] => 2
[1] => x
[2] => name2
[3] => company2
[4] => 500
)
.
.
.
[979] => Array
(
[0] => 946
[1] => x
[2] => name946
[3] => company946
[4] => 0
)
)
Just loop on your table :
foreach($lines AS $k => $row) {
if($row[1] !== 'x') {
unset($lines[$k]);
}
}
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;
}
}
Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Closed 8 years ago.
This question appears to be off-topic because it lacks sufficient information to diagnose the problem. Describe your problem in more detail or include a minimal example in the question itself.
Questions asking for code must demonstrate a minimal understanding of the problem being solved. Include attempted solutions, why they didn't work, and the expected results. See also: Stack Overflow question checklist
Improve this question
I have been working on a project of mine for a few days now, and essentially what I'm trying to do in this project is a comparison of csv file that a user normally does in excel, to do it in php automatically every night. I got the info from the CSV's intro arrays, but I'm having trouble combining them to get the right info for every book, hence the following example and question.
I have the following array (array1) from a csv file, in witch the [5] key will always be 0 :
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] => description1
[2] => category1
[3] => code1
[4] => editor1
[5] => 9
[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] => 12
[6] => curr3
[7] => in stoc
)
[3] =>
)
and array3 from yet another csv:
Array
(
[0] => Array
(
[0] => book1
[1] => description1
[2] => category1
[3] => code1
[4] => editor1
[5] => 10
[6] => eur
[7] => in 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] =>
)
I would like to know how to return an additional array (array4) with the minimum values for [5] key, except 0 because the [5] key from array1 will always be 0, for each of the items in the array
Ex:
Array
(
[0] => Array
(
[0] => book1
[1] => description1
[2] => category1
[3] => code1
[4] => editor1
[5] => 10 // 9 < 10, but 9 is not in stock so the next value > 0 is 10
[6] => eur
[7] => in stoc
)
[1] => Array
(
[0] => book2
[1] => description2
[2] => category2
[3] => code2
[4] => editor2
[5] => 0 // because this book has 0 price in all arrays, hence the price
[6] => curr2 // is not valid
[7] => out of stoc
)
[2] => Array
(
[0] => book3
[1] => description3
[2] => category3
[3] => code3
[4] => editor3
[5] => 12 // because the only array that contains a valid price is array2
[6] => curr3
[7] => out of stoc
)
[3] =>
)
Try this:
$merged = $array1;
foreach($merged as $key => &$value) {
$prices = array($array1[$key][5], $array2[$key][5], $array3[$key][5]);
//if all prices are 0, the final price is 0
if(array_sum($prices) == 0) {
$value[5] = 0;
}
//else find the lowest price in $prices that is > 0
else {
$minPrice = PHP_INT_MAX;
foreach($prices as $price) {
if($price > 0 && $price < $minPrice) {
$minPrice = $price;
}
}
$value[5] = $minPrice;
}
}
unset($value);
print_r($merged);
If I understand correctly you want the smallest value per array for key #5, correct?
If that is the case this will help:
$min = array();
foreach ($array_x as $key => $value) {
if ($value[5] > 0) {
if (!isset($min[$key])) $min[$key] = $value;
if ($min[$key] < $value[5]) $min[$key] = $value;
} //end if
} //end foreach
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