I am parsing an XML file and creating two arrays: one of the XML tags ($tags), and the other as the values for the tags ($values). As it parses, it adds the tags and values as it goes, when it's done, I implode the arrays and put them into a MySQL statement:
$sql = "INSERT INTO everything ($tags) VALUE ($values)";
This works fine until I have repeating tags, and then the SQL statement doesn't work....
Is there a way to find the first repeated word in the $tags array and split it at that word (Keeping the tags that follow it) and also split the $values array at the same index that $tags was split, so that the information stays in the same order?
So ultimately converting something like this:
INSERT INTO everything (AmazonOrderID,MerchantOrderID,ShipmentID,MerchantFulfillmentID,PostedDate,AmazonOrderItemCode,SKU,Quantity,Principal,Commission,AmazonOrderItemCode,SKU,Quantity,Principal,Commission,AmazonOrderItemCode,SKU,Quantity,Principal,Commission,FBA) VALUE ('1','1','D','A','2015','64','OX','1','18','-2','64','WA','1','23','-2','29','WAG','1','49','77','97');
Into something like:
INSERT INTO everything (AmazonOrderID,MerchantOrderID,ShipmentID,MerchantFulfillmentID,PostedDate,AmazonOrderItemCode,SKU,Quantity,Principal,Commission) VALUES ('1','1','D','A','2015','64','OX','1','18','-2');
INSERT INTO everything (AmazonOrderItemCode,SKU,Quantity,Principal,Commission) VALUES ('64','WA','1','23','-2');
INSERT INTO everything (AmazonOrderItemCode,SKU,Quantity,Principal,Commission,FBA) VALUES ('29','WAG','1','49','77','97');
Thanks in advance!...
I just base from your "something like".. :)
$fields = ['AmazonOrderID', 'MerchantOrderID', 'ShipmentID', 'MerchantFulfillmentID', 'PostedDate', 'AmazonOrderItemCode', 'SKU', 'Quantity', 'Principal', 'Commission', 'AmazonOrderItemCode', 'SKU', 'Quantity', 'Principal', 'Commission', 'AmazonOrderItemCode', 'SKU', 'Quantity', 'Principal', 'Commission', 'FBA'];
$values = ['1','1','D','A','2015','64','OX','1','18','-2','64','WA','1','23','-2','29','WAG','1','49','77','97'];
// i just added this to avoid error produced by: `Undefined offset` error warning
error_reporting(0);
$fields_dup = array();
$values_dup = array();
for ($i = 0, $j = 0; $i < count($fields); $i++)
{
if (in_array($fields[$i], $fields_dup[$j]))
$j++;
$fields_dup[$j][] = $fields[$i];
$values_dup[$j][] = $values[$i];
// or maybe you want to add ` and ' make your statement look like:
// INSERT INTO table (`field1`, `field2`) VALUES ('value1', 'value2')
//
// $fields_dup[$j][] = "`".$fields[$i]."`";
// $values_dup[$j][] = "'".$values[$i]."'";
}
error_reporting(E_ALL);
// just to show what is produced
var_dump($fields_dup);
var_dump($values_dup);
// while you can also construct your statement in a loop like
for ($i = 0; $i < count($fields_dup); $i++)
{
$sql_fields = implode(',', $fields_dup[$i]);
$sql_values = implode(',', $values_dup[$i]);
echo "INSERT INTO everything ($sql_fields) VALUES ($sql_values) <br>";
}
Output would be:
//var_dump($fields_dup);
array (size=3)
0 =>
array (size=10)
0 => string 'AmazonOrderID' (length=13)
1 => string 'MerchantOrderID' (length=15)
2 => string 'ShipmentID' (length=10)
3 => string 'MerchantFulfillmentID' (length=21)
4 => string 'PostedDate' (length=10)
5 => string 'AmazonOrderItemCode' (length=19)
6 => string 'SKU' (length=3)
7 => string 'Quantity' (length=8)
8 => string 'Principal' (length=9)
9 => string 'Commission' (length=10)
1 =>
array (size=5)
0 => string 'AmazonOrderItemCode' (length=19)
1 => string 'SKU' (length=3)
2 => string 'Quantity' (length=8)
3 => string 'Principal' (length=9)
4 => string 'Commission' (length=10)
2 =>
array (size=6)
0 => string 'AmazonOrderItemCode' (length=19)
1 => string 'SKU' (length=3)
2 => string 'Quantity' (length=8)
3 => string 'Principal' (length=9)
4 => string 'Commission' (length=10)
5 => string 'FBA' (length=3)
// var_dump($values_dup);
array (size=3)
0 =>
array (size=10)
0 => string '1' (length=1)
1 => string '1' (length=1)
2 => string 'D' (length=1)
3 => string 'A' (length=1)
4 => string '2015' (length=4)
5 => string '64' (length=2)
6 => string 'OX' (length=2)
7 => string '1' (length=1)
8 => string '18' (length=2)
9 => string '-2' (length=2)
1 =>
array (size=5)
0 => string '64' (length=2)
1 => string 'WA' (length=2)
2 => string '1' (length=1)
3 => string '23' (length=2)
4 => string '-2' (length=2)
2 =>
array (size=6)
0 => string '29' (length=2)
1 => string 'WAG' (length=3)
2 => string '1' (length=1)
3 => string '49' (length=2)
4 => string '77' (length=2)
5 => string '97' (length=2)
// for the last for-statement
INSERT INTO everything (AmazonOrderID,MerchantOrderID,ShipmentID,MerchantFulfillmentID,PostedDate,AmazonOrderItemCode,SKU,Quantity,Principal,Commission) VALUES (1,1,D,A,2015,64,OX,1,18,-2)
INSERT INTO everything (AmazonOrderItemCode,SKU,Quantity,Principal,Commission) VALUES (64,WA,1,23,-2)
INSERT INTO everything (AmazonOrderItemCode,SKU,Quantity,Principal,Commission,FBA) VALUES (29,WAG,1,49,77,97)
Is that what you are trying to do?
Hope this is helpful, Cheers! ;)
Related
I have an array that has this structure:
array (size=6)
0 =>
array (size=9)
0 => string 'Dorado' (length=6)
1 => string '32GB' (length=4)
2 => string 'Plastico' (length=8)
'vlr' => string '40000' (length=5)
'pcost' => string '0' (length=1)
'pcomp' => string '0' (length=1)
'sede' =>
array (size=1)
9 => string '0' (length=1)
'ptc' =>
array (size=2)
12 => string '0' (length=1)
11 => string '0' (length=1)
's' => string '' (length=0)
1 =>
array (size=9)
0 => string 'Dorado' (length=6)
1 => string '32GB' (length=4)
2 => string 'Madera' (length=6)
'vlr' => string '40000' (length=5)
'pcost' => string '0' (length=1)
'pcomp' => string '0' (length=1)
'sede' =>
array (size=1)
9 => string '0' (length=1)
'ptc' =>
array (size=2)
12 => string '0' (length=1)
11 => string '0' (length=1)
's' => string '' (length=0)
I have values from a selection that is Dorado->32GB->Madera and I need find this value in the array; if its true that it exists, then take the other values like a vlr, pcost, etc.
What is the best method to find that, remembering that the values with number are dynamic, sometimes with 1 or 5 example:
0 => string 'Dorado' (length=6)
1 => string '32GB' (length=4)
2 => string 'Plastico' (length=8)
0 => string 'Azul' (length=6)
1 => string '32GB' (length=4)
Can somebody explain how I could do this?
I suppose, this is a code to start with:
$searchValue1 = 'Dorado';
$searchValue2 = '32GB';
$searchValue3 = 'Plastico';
foreach ($yourArray as $item) {
if ($item[0] === $searchValue1 && $item[1] === $searchValue2 && $item[2] === $searchValue3) {
print_r($item);
}
}
A bit sophisticated version with checking whether $searchValue_ is not empty is:
$searchValue1 = 'Dorado';
$searchValue2 = null;
$searchValue3 = 'Plastico';
foreach ($yourArray as $item) {
if (
(!empty($searchValue1) && $item[0] === $searchValue1)
&&
(!empty($searchValue2) && $item[1] === $searchValue2)
&&
(!empty($searchValue3) && $item[2] === $searchValue3)
) {
print_r($item);
}
}
update: with unknown number of search parameters you can:
$searchValues = ['Dorado', 'Plastico'];
foreach ($yourArray as $item) {
// find intersection of two arrays
$intersect = array_intersect($item, $searchValues);
// if size of intersection is same as size of `$searchValues`
// then you can be sure that all elements of `$searchValues` are in `$item`
if (count($intersect) === count($searchValues)) {
print_r($item);
}
}
I have list of departments, against each department there is a button which is UP and another button DOWN
When you click on UP Button, It calls the function up..
Public function actionUP($id, $sort_order){
$allDeps = Department::model()->findAll($criteria);
$depArr = array();
foreach ($allDeps as $department){
$temp = array();
$temp['id'] = $department->id;
$temp['sort_order'] = $department->sort_order;
$depArr[] = $temp;
}
Now if i var dump this array depArr i am getting an array..
array (size=17)
0 =>
array (size=2)
'id' => string '142' (length=3)
'sort_order' => string '1' (length=1)
1 =>
array (size=2)
'id' => string '141' (length=3)
'sort_order' => string '2' (length=1)
2 =>
array (size=2)
'id' => string '144' (length=3)
'sort_order' => string '5' (length=1)
3 =>
array (size=2)
'id' => string '140' (length=3)
'sort_order' => string '6' (length=1)
4 =>
array (size=2)
'id' => string '139' (length=3)
'sort_order' => string '3' (length=1)
5 =>
array (size=2)
'id' => string '143' (length=3)
'sort_order' => string '4' (length=1)
I have list of departments, position is actually sort_order and there are up and down button against department, up buttons calls for actionUP which sends the up?id=143&sort_order=4
If a user clicks on up button against position 4 its position should changed to 3, and the department at 3 should go to 4 and ALSO UPDATE INTO DATABASE..
I am using Yii 1.1 PHP framework
Flow:
Search using
array_search('whateverID', array_column($depArr, 'id'));
Now you have the index and the sort order, you just need to search for sort order - 1 now.
array_search($depArr[x]['sort_order']-1, array_column($depArr,
'sort_order'));
You now have both element's index, swap the sort order or unset and set again. Hope this helps.
I am reading data from a .csv (which originally comes from an external database) file with fgetcsv. The first line of the .csv file contains the column names, the following rows contain the data.
What I got so far is an array with the column names, and a second array with the rows of of the .csv file (each row as an array).
How can I convert the second array to an associative array with keys being the column names from the first array?
// this is for Mac OS X
ini_set("auto_detect_line_endings", true);
// the name of the file to read
$fileName = 'WebArtikel_V1-modified.csv';
// open file (get handle to file)
$file = fopen($fileName, 'r');
// the array to store the csv's data
$rawData = array();
// the first line of the csv
$header = array();
$i = 0;
while (($line = fgetcsv($file, 4096, ";")) !== FALSE) {
//$line is an array of the csv elements
//print_r($line);
if($i == 0){
$header[] = $line;
} else {
$rawData[] = $line;
}
$i++;
}
fclose($file);
Here is the var_dump($header):
array (size=1)
0 =>
array (size=50)
0 => string 'KHK_EAN' (length=7)
1 => string 'StyleNumber' (length=11)
2 => string 'StyleName' (length=9)
3 => string 'Setname' (length=7)
4 => string 'ColorNumber' (length=11)
5 => string 'ColorName' (length=9)
6 => string 'StyleSize' (length=9)
7 => string 'KHK_Artikel' (length=11)
8 => string 'PriceNew' (length=8)
9 => string 'PriceSale' (length=9)
Note: there is a toplevel array with 1 element which contains another array with the column names.
The $rawData array:
array (size=3604)
0 =>
array (size=50)
0 => string '4055765008989' (length=13)
1 => string '201001001' (length=9)
2 => string ' ' (length=1)
3 => string 'ACCESS 3' (length=8)
4 => string '2942' (length=4)
5 => string 'Blau/Marine' (length=11)
6 => string '1' (length=1)
7 => string '201001001-2942-1' (length=16)
8 => string '199,9' (length=5)
9 => string '199,9' (length=5)
1 =>
array (size=50)
0 => string '4055765008996' (length=13)
1 => string '201001001' (length=9)
2 => string ' ' (length=1)
3 => string 'ACCESS 3' (length=8)
4 => string '3924' (length=4)
5 => string 'Beige/Braun' (length=11)
6 => string '1' (length=1)
7 => string '201001001-3924-1' (length=16)
8 => string '199,9' (length=5)
9 => string '199,9' (length=5)
2 =>
array (size=50)
0 => string '4055765009047' (length=13)
1 => string '201002001' (length=9)
2 => string ' ' (length=1)
3 => string 'ACCESS 3' (length=8)
4 => string '2942' (length=4)
5 => string 'Blau/Marine' (length=11)
6 => string '1' (length=1)
7 => string '201002001-2942-1' (length=16)
8 => string '179,9' (length=5)
9 => string '179,9' (length=5)
Note: same as above, nested arrays, so I can't use array_combine().
Any suggestions?
Is this the same question as here? Copying it below for convenience.
how do i parse a csv file to grab the column names first then the rows that relate to it?
For reading it all at once you can use:
$csv = array_map("str_getcsv", file("file1.csv",FILE_SKIP_EMPTY_LINES));
$keys = array_shift($csv);
To turn all the rows into a nice associative array you could then apply:
foreach ($csv as $i=>$row) {
$csv[$i] = array_combine($keys, $row);
}
I want to display array 1 by 1.. 1st array value display thnn 2nd array value
I want to display array data using loop and display data separatelylike 1st array data display then 2nd array data will display. and i want to display data only entrydate n description. with loop count
$svs= $user_posts->getvalue('service', $dsplylimit );
var_dump($svs);
My array is like this
array (size=2)
0 =>
array (size=46)
0 => string '74' (length=2)
'id' => string '74' (length=2)
1 => string '0' (length=1)
'author' => string '0' (length=1)
2 => string '2016-02-08 07:32:08' (length=19)
'entrydate' => string '2016-02-08 07:32:08' (length=19)
3 => string '2016-02-08 01:32:08' (length=19)
'date_gmt' => string '2016-02-08 01:32:08' (length=19)
4 => string 'nice email' (length=10)
'description' => string 'nice email' (length=10)
1 =>
array (size=46)
0 => string '75' (length=2)
'id' => string '75' (length=2)
1 => string '0' (length=1)
'author' => string '0' (length=1)
2 => string '2016-02-08 11:15:40' (length=19)
'entrydate' => string '2016-02-08 11:15:40' (length=19)
3 => string '2016-02-08 05:15:40' (length=19)
'date_gmt' => string '2016-02-08 05:15:40' (length=19)
4 => string 'hiiiiiii' (length=8)
'description' => string 'hiiiiiii' (length=8)
It isn't very clear what you want to do, but you can probably display the data with a foreach loop. Just loop through each array, and then print out the values from each iteration that you want.
For example for the following array
$data = [
['entrydate' => '2016-01-01', 'description' => 'Some description here'],
['entrydate' => '2016-01-02', 'description' => 'Another description here']
];
You could print out the data like so:
$html = '';
foreach ($data as $array) {
$html .= '<h1>' . htmlspecialchars($array['entrydate']) . '</h1>';
$html .= '<p>' . htmlspecialchars($array['description']) . '</p>';
}
echo $html;
For the following output:
2016-01-01
Some description here
2016-01-02
Another description here
There's a chance I'm not understanding your requirements, if so just let me know, but I'm not 100% sure what it is you're after.
I have an array in loop while like this when I var_dump it.
array (size=73)
0 => string '1' (length=1)
'address' => string '1' (length=1)
1 => string '2' (length=2)
'street_no' => string '2' (length=1)
array (size=73)
0 => string 'vbfgh' (length=5)
'address' => string 'vbfgh' (length=5)
1 => string 'fgfd' (length=4)
'street_no' => string 'fgfd' (length=4)
array (size=73)
0 => string 'vbfgh' (length=5)
'address' => string 'vbfgh' (length=5)
1 => string 'fgfd' (length=4)
'street_no' => string 'fgfd' (length=4)
array (size=73)
0 => string 'vbfgh' (length=5)
'address' => string 'vbfgh' (length=5)
1 => string 'fgfd' (length=4)
'street_no' => string 'fgfd' (length=4)
I want disply data on screen for first data is
`AddressMain: 1
streetMain: 1
.....
AddressLeft: vbfgh
street1Left: fgfd
.....
AddressRight: vbfgh
streetRight: fgfd
.....
AddressCenter: vbfgh
streetCenter: fgfd
.....`
How I can change my label like this if this code in loop while ?
And this is my code
while( $row = pg_fetch_array($result)){
echo "AddressMain:".$row['address'];
echo "streetMain:".$row['street_no'];
echo "<br/>";
echo ".......";
}
Plz help me how I can change my label in this loop ?
thanks
Use an array to define labels, then loop around it.
$labels = array('Main','Left','Right','Center');
$i = 0;
while( $row = pg_fetch_array($result)){
echo "Address".$labels[$i].":".$row['address'];
echo "street".$labels[$i].":".$row['street'];
echo "<br/>";
$i++;
}