Cant interact with PHP array - php

I'm turning grey headed over here..
Trying to interact with a array made from a csv file. When i echo parts of the array, it can be perfectly found. But when i try to use an array element in an IF sentence, it seems empty (its not empty).
Here is my code:
<?php
$filnavn="uploads/".$_COOKIE["f1l3nam3"];
$file = fopen($filnavn,"r");
$tael=0; //Used as a counter
while(! feof($file))
{
$resultat=fgetcsv($file,0,";");
if($resultat[19]!="DKK"){
$tael++;
}
}
echo $tael;
fclose($file);
?>
The result of this page is 0, it should be around 1320
var_dump($resultat[19]); gives me
string(7) "DKK"
string(7) "DKK"
string(7) "EUR"
string(7) "GBP"
string(7) "GBP"
string(7) "GBP"
string(7) "GBP"
string(7) "DKK"
string(7) "GBP"
string(7) "GBP"
string(7) "GBP"
string(7) "GBP"
string(7) "DKK"
string(7) "DKK"
string(7) "GBP"
string(7) "GBP"
string(7) "GBP"
string(7) "GBP"
string(7) "DKK"
string(7) "GBP"
string(7) "GBP"
string(7) "GBP"
string(7) "DKK"
string(7)
var_dump($resultat); gives me
array(31) {
[0]=> string(19) "246338756"
[1]=> string(41) "ContextType_Supplier"
[2]=> string(1) ""
[3]=> string(15) "0,00000"
[4]=> string(15) "0,00000"
[5]=> string(3) "0"
[6]=> string(3) "0"
[7]=> string(15) "0,00000"
[8]=> string(15) "0,00000"
[9]=> string(3) "0"
[10]=> string(3) "0"
[11]=> string(15) "0,00000"
[12]=> string(13) "311921"
[13]=> string(11) "17659"
[14]=> string(21) "08-05-2018"
[15]=> string(1) ""
[16]=> string(19) "246156901"
[17]=> string(17) "43534453"
[18]=> string(61) "Secret"
[19]=> string(7) "DKK"
}
Does anyone have any ideas?

Change your condition like below:-
if(strpos(trim($resultat[19]), "DKK") === false){
$tael++;
}
Or use
if(htmlentities($resultat[19]) != "DKK"){
$tael++;
}

Related

Parse JSON object with PHP [duplicate]

This question already has an answer here:
How to extract and access data from JSON with PHP?
(1 answer)
Closed 5 years ago.
I've read every single question on StackOverflow on how to parse JSON with PHP but none of them are working with the JSON I have.
array(2) { ["status"]=> bool(true) ["currency"]=> array(30) { [0]=>
array(10) { ["currency"]=> string(7) "EUR/AUD" ["value"]=> string(6)
"1.4925" ["change"]=> string(7) "-0.0076" ["change_percent"]=>
string(8) "-0.50663" ["ask"]=> string(6) "1.4926" ["bid"]=> string(6)
"1.4923" ["daily_lowest"]=> string(6) "1.4873" ["daily_highest"]=>
string(6) "1.5016" ["date"]=> string(19) "2017-08-16 09:46:11"
["type"]=> string(8) "original" } [1]=> array(10) { ["currency"]=>
string(7) "EUR/CAD" ["value"]=> string(5) "1.494" ["change"]=>
string(7) "-0.0032" ["change_percent"]=> string(8) "-0.21373"
["ask"]=> string(5) "1.494" ["bid"]=> string(6) "1.4939"
["daily_lowest"]=> string(5) "1.489" ["daily_highest"]=> string(6)
"1.4986" ["date"]=> string(19) "2017-08-16 09:46:11" ["type"]=>
string(8) "original" } [2]=> array(10) { ["currency"]=> string(7)
"EUR/CHF" ["value"]=> string(6) "1.1434" ["change"]=> string(6)
"0.0024" ["change_percent"]=> string(7) "0.21034" ["ask"]=> string(6)
"1.1435" ["bid"]=> string(6) "1.1434" ["daily_lowest"]=> string(6)
"1.1402" ["daily_highest"]=> string(6) "1.1444" ["date"]=> string(19)
"2017-08-16 09:46:11" ["type"]=> string(8) "original" } [3]=>
array(10) { ["currency"]=> string(7) "EUR/GBP" ["value"]=> string(6)
"0.9099" ["change"]=> string(7) "-0.0019" ["change_percent"]=>
string(8) "-0.20838" ["ask"]=> string(4) "0.91" ["bid"]=> string(6)
"0.9099" ["daily_lowest"]=> string(6) "0.9083" ["daily_highest"]=>
string(6) "0.9144" ["date"]=> string(19) "2017-08-16 09:46:11"
["type"]=> string(8) "original" } [4]=> array(10) { ["currency"]=>
string(7) "EUR/ILS" ["type"]=> string(9) "converted" ["date"]=>
string(19) "2017-08-16 09:46:11" ["value"]=> string(7) "4.24323"
["change"]=> string(3) "0.0" ["change_percent"]=> string(3) "0.0"
["ask"]=> string(3) "0.0" ["bid"]=> string(3) "0.0" ["daily_lowest"]=>
string(3) "0.0" ["daily_highest"]=> string(3) "0.0" } [5]=> array(10)
{ ["currency"]=> string(7) "EUR/JPY" ["value"]=> string(8) "130.1145"
["change"]=> string(6) "0.2425" ["change_percent"]=> string(7)
"0.18672" ["ask"]=> string(7) "130.122" ["bid"]=> string(7) "130.107"
["daily_lowest"]=> string(7) "129.613" ["daily_highest"]=> string(7)
"130.405" ["date"]=> string(19) "2017-08-16 09:46:11" ["type"]=>
string(8) "original" } [6]=> array(10) { ["currency"]=> string(7)
"EUR/NZD" ["value"]=> string(6) "1.6182" ["change"]=> string(7)
"-0.0036" ["change_percent"]=> string(8) "-0.22198" ["ask"]=>
string(6) "1.6186" ["bid"]=> string(6) "1.6178" ["daily_lowest"]=>
string(6) "1.6138" ["daily_highest"]=> string(6) "1.6247" ["date"]=>
string(19) "2017-08-16 09:46:11" ["type"]=> string(8) "original" }
[7]=> array(10).............
I'm trying to output the value of each ["currency"] ["bid"] with this code:
foreach($result['currency'][0]['bid'] as $item) {
print $item['currency'][0]['bid'];
}
And tried 100 other variations, still getting errors like:
Warning: Illegal string offset 'currency' in getData.php on line 27
Warning: Illegal string offset 'value' in getData.php on line 27
Warning: Invalid argument supplied for foreach() in getData.php on line 27
Maybe there are online parser websites that can help with that? How do I solve this?
It seems to me like it should be.
foreach($result['currency']as $item) {
print $item['value'];
}
Because each currency is 0,1,2 and so on.
And in the item 0,1,2 there is the "value".

csv header row as array key

I'm able to get the file into an array, but I'd like to get the key's to be the first row. Here's is the curl command:
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, "https://url.com/download");
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "GET");
$headers = array();
$headers[] = "Content-Type: application/json";
$headers[] = "X-App-Key: xxxxx";
$headers[] = "X-User-Key: yyyy";
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
$result = curl_exec($ch);
if (curl_errno($ch)) {
echo 'Error:' . curl_error($ch);
}
curl_close ($ch);
$data = array_map("str_getcsv", preg_split('/\r*\n+|\r+/', $result));
return $data;
Here is the csv that is returned:
"Date,Campaign,Campaign Id,Impressions,Clicks,CTR,Platform Cost,Search Cost,Demographic Cost,Contextual Cost,Behavioral Cost,Geographic Cost,Segment Cost,Media Cost,CPM,Total Spend,ECPC,Click Sales,Click Leads,Click Signups,Click Other,View Sales,View Leads,View Signups,View Other,CPA,Weighted Actions 2017-05-16,Company,326606,,,,,,,,,,,,,,,0,0,0,0,0,0,0,2,,2.00 2017-05-18,Company,326606,,,,,,,,,,,,,,,0,0,0,0,0,0,0,2,,2.00 2017-05-19,Company,326606,,,,,,,,,,,,,,,0,0,0,0,0,0,0,2,,2.00 2017-05-20,Company,326606,,,,,,,,,,,,,,,0,0,0,0,0,0,0,2,,2.00 2017-05-28,Company,326606,,,,,,,,,,,,,,,0,0,0,0,0,0,0,1,,1.00 2017-06-02,Company,326606,,,,,,,,,,,,,,,0,0,0,0,0,0,0,1,,1.00 2017-05-10,Company,326606,16877,41,0.002429,0.770373,0.000000,0.0,0.000000,0.0,0.770373,0.000000,1.540785,3.081682,52.009548,1.268526,,,,,,,,,, 2017-05-11,Company,326606,17001,50,0.002941,0.764775,0.000000,0.0,0.000000,0.0,0.764775,0.000000,1.529592,3.059288,52.010956,1.040219,,,,,,,,,, 2017-05-12,Company,326606,16814,44,0.002617,0.773364,0.000000,0.0,0.000000,0.0,0.773364,0.000000,1.546763,3.093651,52.016652,1.182197,,,,,,,,,, 2017-05-13,Company,326606,16880,32,0.001896,0.770245,0.000000,0.0,0.000000,0.0,0.770245,0.000000,1.540520,3.081195,52.010570,1.625330,,,,,,,,,, 2017-05-14,Company,326606,17045,32,0.001877,0.762742,0.000000,0.0,0.000000,0.0,0.762742,0.000000,1.525543,3.051212,52.007907,1.625247,0,0,0,0,0,0,0,1,52.0079,1.00 2017-05-15,Company,326606,7294,10,0.001371,0.781126,0.000000,0.0,0.000000,0.0,0.781126,0.000000,1.562252,3.124683,22.791441,2.279144,,,,,,,,,, 2017-06-03,Company,559435,30186,42,0.001391,0.690422,0.690422,0.0,0.000000,0.0,0.000000,0.000000,1.380910,2.761773,83.366884,1.984926,0,0,0,1,0,0,0,1,41.6834,2.00 2017-06-04,Company,559435,29952,25,0.000835,0.695962,0.695962,0.0,0.000000,0.0,0.000000,0.000000,1.391983,2.783917,83.383894,3.335356,0,0,0,0,0,0,0,12,6.9487,12.00 2017-06-05,Company,559435,29863,39,0.001306,0.697854,0.697854,0.0,0.000000,0.0,0.000000,0.000000,1.395772,2.791494,83.362371,2.137497,0,0,0,4,2,1,0,15,3.7892,22.00 2017-06-06,Company,559435,29859,40,0.001340,0.698055,0.698055,0.0,0.000000,0.0,0.000000,0.000000,1.396160,2.792283,83.374777,2.084369,0,1,0,4,2,1,0,21,2.8750,29.00 2017-06-07,Company,559435,29968,22,0.000734,0.695653,0.695653,0.0,0.000000,0.0,0.000000,0.000000,1.391359,2.782683,83.391458,3.790521,0,0,0,1,0,0,0,25,3.2074,26.00 2017-06-08,Company,559435,29967,41,0.001368,0.695284,0.695284,0.0,0.000000,0.0,0.000000,0.000000,1.390634,2.781195,83.344075,2.032782,0,0,0,2,1,0,0,23,3.2055,26.00 2017-06-09,Company,559435,14689,8,0.000545,0.686755,0.686755,0.0,0.000000,0.0,0.000000,0.000000,1.373565,2.747041,40.351291,5.043911,0,0,0,0,0,2,0,21,1.7544,23.00 ,,,286395,426,0.001487,0.718754,0.471867,0.0,0.000000,0.0,0.246886,0.000000,1.437560,2.875126,823.421824,1.932915,0,1,0,12,5,4,0,129,5.4531,151.00
And the returned array is but I'd like "date" "clicks" to be used as the keys so I can simply call "$data[Click Lead]":
array(22) {
[0]=>
array(27) {
[0]=>
string(4) "Date"
[1]=>
string(8) "Campaign"
[2]=>
string(11) "Campaign Id"
[3]=>
string(11) "Impressions"
[4]=>
string(6) "Clicks"
[5]=>
string(3) "CTR"
[6]=>
string(13) "Platform Cost"
[7]=>
string(11) "Search Cost"
[8]=>
string(16) "Demographic Cost"
[9]=>
string(15) "Contextual Cost"
[10]=>
string(15) "Behavioral Cost"
[11]=>
string(15) "Geographic Cost"
[12]=>
string(12) "Segment Cost"
[13]=>
string(10) "Media Cost"
[14]=>
string(3) "CPM"
[15]=>
string(11) "Total Spend"
[16]=>
string(4) "ECPC"
[17]=>
string(11) "Click Sales"
[18]=>
string(11) "Click Leads"
[19]=>
string(13) "Click Signups"
[20]=>
string(11) "Click Other"
[21]=>
string(10) "View Sales"
[22]=>
string(10) "View Leads"
[23]=>
string(12) "View Signups"
[24]=>
string(10) "View Other"
[25]=>
string(3) "CPA"
[26]=>
string(16) "Weighted Actions"
}
[1]=>
array(27) {
[0]=>
string(10) "2017-05-16"
[1]=>
string(25) "Company"
[2]=>
string(6) "326606"
[3]=>
string(0) ""
[4]=>
string(0) ""
[5]=>
string(0) ""
[6]=>
string(0) ""
[7]=>
string(0) ""
[8]=>
string(0) ""
[9]=>
string(0) ""
[10]=>
string(0) ""
[11]=>
string(0) ""
[12]=>
string(0) ""
[13]=>
string(0) ""
[14]=>
string(0) ""
[15]=>
string(0) ""
[16]=>
string(0) ""
[17]=>
string(1) "0"
[18]=>
string(1) "0"
[19]=>
string(1) "0"
[20]=>
string(1) "0"
[21]=>
string(1) "0"
[22]=>
string(1) "0"
[23]=>
string(1) "0"
[24]=>
string(1) "2"
[25]=>
string(0) ""
[26]=>
string(4) "2.00"
}
[2]=>
array(27) {
[0]=>
string(10) "2017-05-18"
[1]=>
string(25) "Company"
[2]=>
string(6) "326606"
[3]=>
string(0) ""
[4]=>
string(0) ""
[5]=>
string(0) ""
[6]=>
string(0) ""
[7]=>
string(0) ""
[8]=>
string(0) ""
[9]=>
string(0) ""
[10]=>
string(0) ""
[11]=>
string(0) ""
[12]=>
string(0) ""
[13]=>
string(0) ""
[14]=>
string(0) ""
[15]=>
string(0) ""
[16]=>
string(0) ""
[17]=>
string(1) "0"
[18]=>
string(1) "0"
[19]=>
string(1) "0"
[20]=>
string(1) "0"
[21]=>
string(1) "0"
[22]=>
string(1) "0"
[23]=>
string(1) "0"
[24]=>
string(1) "2"
[25]=>
string(0) ""
[26]=>
string(4) "2.00"
}
[3]=>
array(27) {
[0]=>
string(10) "2017-05-19"
[1]=>
string(25) "Company"
[2]=>
string(6) "326606"
[3]=>
string(0) ""
[4]=>
string(0) ""
[5]=>
string(0) ""
[6]=>
string(0) ""
[7]=>
string(0) ""
[8]=>
string(0) ""
[9]=>
string(0) ""
[10]=>
string(0) ""
[11]=>
string(0) ""
[12]=>
string(0) ""
[13]=>
string(0) ""
[14]=>
string(0) ""
[15]=>
string(0) ""
[16]=>
string(0) ""
[17]=>
string(1) "0"
[18]=>
string(1) "0"
[19]=>
string(1) "0"
[20]=>
string(1) "0"
[21]=>
string(1) "0"
[22]=>
string(1) "0"
[23]=>
string(1) "0"
[24]=>
string(1) "2"
[25]=>
string(0) ""
[26]=>
string(4) "2.00"
}
[4]=>
array(27) {
[0]=>
string(10) "2017-05-20"
[1]=>
string(25) "Company"
[2]=>
string(6) "326606"
[3]=>
string(0) ""
[4]=>
string(0) ""
[5]=>
string(0) ""
[6]=>
string(0) ""
[7]=>
string(0) ""
[8]=>
string(0) ""
[9]=>
string(0) ""
[10]=>
string(0) ""
[11]=>
string(0) ""
[12]=>
string(0) ""
[13]=>
string(0) ""
[14]=>
string(0) ""
[15]=>
string(0) ""
[16]=>
string(0) ""
[17]=>
string(1) "0"
[18]=>
string(1) "0"
[19]=>
string(1) "0"
[20]=>
string(1) "0"
[21]=>
string(1) "0"
[22]=>
string(1) "0"
[23]=>
string(1) "0"
[24]=>
string(1) "2"
[25]=>
string(0) ""
[26]=>
string(4) "2.00"
}
[5]=>
array(27) {
[0]=>
string(10) "2017-05-28"
[1]=>
string(25) "Company"
[2]=>
string(6) "326606"
[3]=>
string(0) ""
[4]=>
string(0) ""
[5]=>
string(0) ""
[6]=>
string(0) ""
[7]=>
string(0) ""
[8]=>
string(0) ""
[9]=>
string(0) ""
[10]=>
string(0) ""
[11]=>
string(0) ""
[12]=>
string(0) ""
[13]=>
string(0) ""
[14]=>
string(0) ""
[15]=>
string(0) ""
[16]=>
string(0) ""
[17]=>
string(1) "0"
[18]=>
string(1) "0"
[19]=>
string(1) "0"
[20]=>
string(1) "0"
[21]=>
string(1) "0"
[22]=>
string(1) "0"
[23]=>
string(1) "0"
[24]=>
string(1) "1"
[25]=>
string(0) ""
[26]=>
string(4) "1.00"
}
[6]=>
array(27) {
[0]=>
string(10) "2017-06-02"
[1]=>
string(25) "Company"
[2]=>
string(6) "326606"
[3]=>
string(0) ""
[4]=>
string(0) ""
[5]=>
string(0) ""
[6]=>
string(0) ""
[7]=>
string(0) ""
[8]=>
string(0) ""
[9]=>
string(0) ""
[10]=>
string(0) ""
[11]=>
string(0) ""
[12]=>
string(0) ""
[13]=>
string(0) ""
[14]=>
string(0) ""
[15]=>
string(0) ""
[16]=>
string(0) ""
[17]=>
string(1) "0"
[18]=>
string(1) "0"
[19]=>
string(1) "0"
[20]=>
string(1) "0"
[21]=>
string(1) "0"
[22]=>
string(1) "0"
[23]=>
string(1) "0"
[24]=>
string(1) "1"
[25]=>
string(0) ""
[26]=>
string(4) "1.00"
}
[7]=>
array(27) {
[0]=>
string(10) "2017-05-10"
[1]=>
string(25) "Company"
[2]=>
string(6) "326606"
[3]=>
string(5) "16877"
[4]=>
string(2) "41"
[5]=>
string(8) "0.002429"
[6]=>
string(8) "0.770373"
[7]=>
string(8) "0.000000"
[8]=>
string(3) "0.0"
[9]=>
string(8) "0.000000"
[10]=>
string(3) "0.0"
[11]=>
string(8) "0.770373"
[12]=>
string(8) "0.000000"
[13]=>
string(8) "1.540785"
[14]=>
string(8) "3.081682"
[15]=>
string(9) "52.009548"
[16]=>
string(8) "1.268526"
[17]=>
string(0) ""
[18]=>
string(0) ""
[19]=>
string(0) ""
[20]=>
string(0) ""
[21]=>
string(0) ""
[22]=>
string(0) ""
[23]=>
string(0) ""
[24]=>
string(0) ""
[25]=>
string(0) ""
[26]=>
string(0) ""
}
[8]=>
array(27) {
[0]=>
string(10) "2017-05-11"
[1]=>
string(25) "Company"
[2]=>
string(6) "326606"
[3]=>
string(5) "17001"
[4]=>
string(2) "50"
[5]=>
string(8) "0.002941"
[6]=>
string(8) "0.764775"
[7]=>
string(8) "0.000000"
[8]=>
string(3) "0.0"
[9]=>
string(8) "0.000000"
[10]=>
string(3) "0.0"
[11]=>
string(8) "0.764775"
[12]=>
string(8) "0.000000"
[13]=>
string(8) "1.529592"
[14]=>
string(8) "3.059288"
[15]=>
string(9) "52.010956"
[16]=>
string(8) "1.040219"
[17]=>
string(0) ""
[18]=>
string(0) ""
[19]=>
string(0) ""
[20]=>
string(0) ""
[21]=>
string(0) ""
[22]=>
string(0) ""
[23]=>
string(0) ""
[24]=>
string(0) ""
[25]=>
string(0) ""
[26]=>
string(0) ""
}
[9]=>
array(27) {
[0]=>
string(10) "2017-05-12"
[1]=>
string(25) "Company"
[2]=>
string(6) "326606"
[3]=>
string(5) "16814"
[4]=>
string(2) "44"
[5]=>
string(8) "0.002617"
[6]=>
string(8) "0.773364"
[7]=>
string(8) "0.000000"
[8]=>
string(3) "0.0"
[9]=>
string(8) "0.000000"
[10]=>
string(3) "0.0"
[11]=>
string(8) "0.773364"
[12]=>
string(8) "0.000000"
[13]=>
string(8) "1.546763"
[14]=>
string(8) "3.093651"
[15]=>
string(9) "52.016652"
[16]=>
string(8) "1.182197"
[17]=>
string(0) ""
[18]=>
string(0) ""
[19]=>
string(0) ""
[20]=>
string(0) ""
[21]=>
string(0) ""
[22]=>
string(0) ""
[23]=>
string(0) ""
[24]=>
string(0) ""
[25]=>
string(0) ""
[26]=>
string(0) ""
}
[10]=>
array(27) {
[0]=>
string(10) "2017-05-13"
[1]=>
string(25) "Company"
[2]=>
string(6) "326606"
[3]=>
string(5) "16880"
[4]=>
string(2) "32"
[5]=>
string(8) "0.001896"
[6]=>
string(8) "0.770245"
[7]=>
string(8) "0.000000"
[8]=>
string(3) "0.0"
[9]=>
string(8) "0.000000"
[10]=>
string(3) "0.0"
[11]=>
string(8) "0.770245"
[12]=>
string(8) "0.000000"
[13]=>
string(8) "1.540520"
[14]=>
string(8) "3.081195"
[15]=>
string(9) "52.010570"
[16]=>
string(8) "1.625330"
[17]=>
string(0) ""
[18]=>
string(0) ""
[19]=>
string(0) ""
[20]=>
string(0) ""
[21]=>
string(0) ""
[22]=>
string(0) ""
[23]=>
string(0) ""
[24]=>
string(0) ""
[25]=>
string(0) ""
[26]=>
string(0) ""
}
[11]=>
array(27) {
[0]=>
string(10) "2017-05-14"
[1]=>
string(25) "Company"
[2]=>
string(6) "326606"
[3]=>
string(5) "17045"
[4]=>
string(2) "32"
[5]=>
string(8) "0.001877"
[6]=>
string(8) "0.762742"
[7]=>
string(8) "0.000000"
[8]=>
string(3) "0.0"
[9]=>
string(8) "0.000000"
[10]=>
string(3) "0.0"
[11]=>
string(8) "0.762742"
[12]=>
string(8) "0.000000"
[13]=>
string(8) "1.525543"
[14]=>
string(8) "3.051212"
[15]=>
string(9) "52.007907"
[16]=>
string(8) "1.625247"
[17]=>
string(1) "0"
[18]=>
string(1) "0"
[19]=>
string(1) "0"
[20]=>
string(1) "0"
[21]=>
string(1) "0"
[22]=>
string(1) "0"
[23]=>
string(1) "0"
[24]=>
string(1) "1"
[25]=>
string(7) "52.0079"
[26]=>
string(4) "1.00"
}
[12]=>
array(27) {
[0]=>
string(10) "2017-05-15"
[1]=>
string(25) "Company"
[2]=>
string(6) "326606"
[3]=>
string(4) "7294"
[4]=>
string(2) "10"
[5]=>
string(8) "0.001371"
[6]=>
string(8) "0.781126"
[7]=>
string(8) "0.000000"
[8]=>
string(3) "0.0"
[9]=>
string(8) "0.000000"
[10]=>
string(3) "0.0"
[11]=>
string(8) "0.781126"
[12]=>
string(8) "0.000000"
[13]=>
string(8) "1.562252"
[14]=>
string(8) "3.124683"
[15]=>
string(9) "22.791441"
[16]=>
string(8) "2.279144"
[17]=>
string(0) ""
[18]=>
string(0) ""
[19]=>
string(0) ""
[20]=>
string(0) ""
[21]=>
string(0) ""
[22]=>
string(0) ""
[23]=>
string(0) ""
[24]=>
string(0) ""
[25]=>
string(0) ""
[26]=>
string(0) ""
}
[13]=>
array(27) {
[0]=>
string(10) "2017-06-03"
[1]=>
string(25) "Company"
[2]=>
string(6) "559435"
[3]=>
string(5) "30186"
[4]=>
string(2) "42"
[5]=>
string(8) "0.001391"
[6]=>
string(8) "0.690422"
[7]=>
string(8) "0.690422"
[8]=>
string(3) "0.0"
[9]=>
string(8) "0.000000"
[10]=>
string(3) "0.0"
[11]=>
string(8) "0.000000"
[12]=>
string(8) "0.000000"
[13]=>
string(8) "1.380910"
[14]=>
string(8) "2.761773"
[15]=>
string(9) "83.366884"
[16]=>
string(8) "1.984926"
[17]=>
string(1) "0"
[18]=>
string(1) "0"
[19]=>
string(1) "0"
[20]=>
string(1) "1"
[21]=>
string(1) "0"
[22]=>
string(1) "0"
[23]=>
string(1) "0"
[24]=>
string(1) "1"
[25]=>
string(7) "41.6834"
[26]=>
string(4) "2.00"
}
[14]=>
array(27) {
[0]=>
string(10) "2017-06-04"
[1]=>
string(25) "Company"
[2]=>
string(6) "559435"
[3]=>
string(5) "29952"
[4]=>
string(2) "25"
[5]=>
string(8) "0.000835"
[6]=>
string(8) "0.695962"
[7]=>
string(8) "0.695962"
[8]=>
string(3) "0.0"
[9]=>
string(8) "0.000000"
[10]=>
string(3) "0.0"
[11]=>
string(8) "0.000000"
[12]=>
string(8) "0.000000"
[13]=>
string(8) "1.391983"
[14]=>
string(8) "2.783917"
[15]=>
string(9) "83.383894"
[16]=>
string(8) "3.335356"
[17]=>
string(1) "0"
[18]=>
string(1) "0"
[19]=>
string(1) "0"
[20]=>
string(1) "0"
[21]=>
string(1) "0"
[22]=>
string(1) "0"
[23]=>
string(1) "0"
[24]=>
string(2) "12"
[25]=>
string(6) "6.9487"
[26]=>
string(5) "12.00"
}
[15]=>
array(27) {
[0]=>
string(10) "2017-06-05"
[1]=>
string(25) "Company"
[2]=>
string(6) "559435"
[3]=>
string(5) "29863"
[4]=>
string(2) "39"
[5]=>
string(8) "0.001306"
[6]=>
string(8) "0.697854"
[7]=>
string(8) "0.697854"
[8]=>
string(3) "0.0"
[9]=>
string(8) "0.000000"
[10]=>
string(3) "0.0"
[11]=>
string(8) "0.000000"
[12]=>
string(8) "0.000000"
[13]=>
string(8) "1.395772"
[14]=>
string(8) "2.791494"
[15]=>
string(9) "83.362371"
[16]=>
string(8) "2.137497"
[17]=>
string(1) "0"
[18]=>
string(1) "0"
[19]=>
string(1) "0"
[20]=>
string(1) "4"
[21]=>
string(1) "2"
[22]=>
string(1) "1"
[23]=>
string(1) "0"
[24]=>
string(2) "15"
[25]=>
string(6) "3.7892"
[26]=>
string(5) "22.00"
}
[16]=>
array(27) {
[0]=>
string(10) "2017-06-06"
[1]=>
string(25) "Company"
[2]=>
string(6) "559435"
[3]=>
string(5) "29859"
[4]=>
string(2) "40"
[5]=>
string(8) "0.001340"
[6]=>
string(8) "0.698055"
[7]=>
string(8) "0.698055"
[8]=>
string(3) "0.0"
[9]=>
string(8) "0.000000"
[10]=>
string(3) "0.0"
[11]=>
string(8) "0.000000"
[12]=>
string(8) "0.000000"
[13]=>
string(8) "1.396160"
[14]=>
string(8) "2.792283"
[15]=>
string(9) "83.374777"
[16]=>
string(8) "2.084369"
[17]=>
string(1) "0"
[18]=>
string(1) "1"
[19]=>
string(1) "0"
[20]=>
string(1) "4"
[21]=>
string(1) "2"
[22]=>
string(1) "1"
[23]=>
string(1) "0"
[24]=>
string(2) "21"
[25]=>
string(6) "2.8750"
[26]=>
string(5) "29.00"
}
[17]=>
array(27) {
[0]=>
string(10) "2017-06-07"
[1]=>
string(25) "Company"
[2]=>
string(6) "559435"
[3]=>
string(5) "29968"
[4]=>
string(2) "22"
[5]=>
string(8) "0.000734"
[6]=>
string(8) "0.695653"
[7]=>
string(8) "0.695653"
[8]=>
string(3) "0.0"
[9]=>
string(8) "0.000000"
[10]=>
string(3) "0.0"
[11]=>
string(8) "0.000000"
[12]=>
string(8) "0.000000"
[13]=>
string(8) "1.391359"
[14]=>
string(8) "2.782683"
[15]=>
string(9) "83.391458"
[16]=>
string(8) "3.790521"
[17]=>
string(1) "0"
[18]=>
string(1) "0"
[19]=>
string(1) "0"
[20]=>
string(1) "1"
[21]=>
string(1) "0"
[22]=>
string(1) "0"
[23]=>
string(1) "0"
[24]=>
string(2) "25"
[25]=>
string(6) "3.2074"
[26]=>
string(5) "26.00"
}
[18]=>
array(27) {
[0]=>
string(10) "2017-06-08"
[1]=>
string(25) "Company"
[2]=>
string(6) "559435"
[3]=>
string(5) "29967"
[4]=>
string(2) "41"
[5]=>
string(8) "0.001368"
[6]=>
string(8) "0.695284"
[7]=>
string(8) "0.695284"
[8]=>
string(3) "0.0"
[9]=>
string(8) "0.000000"
[10]=>
string(3) "0.0"
[11]=>
string(8) "0.000000"
[12]=>
string(8) "0.000000"
[13]=>
string(8) "1.390634"
[14]=>
string(8) "2.781195"
[15]=>
string(9) "83.344075"
[16]=>
string(8) "2.032782"
[17]=>
string(1) "0"
[18]=>
string(1) "0"
[19]=>
string(1) "0"
[20]=>
string(1) "2"
[21]=>
string(1) "1"
[22]=>
string(1) "0"
[23]=>
string(1) "0"
[24]=>
string(2) "23"
[25]=>
string(6) "3.2055"
[26]=>
string(5) "26.00"
}
[19]=>
array(27) {
[0]=>
string(10) "2017-06-09"
[1]=>
string(25) "Company"
[2]=>
string(6) "559435"
[3]=>
string(5) "14689"
[4]=>
string(1) "8"
[5]=>
string(8) "0.000545"
[6]=>
string(8) "0.686755"
[7]=>
string(8) "0.686755"
[8]=>
string(3) "0.0"
[9]=>
string(8) "0.000000"
[10]=>
string(3) "0.0"
[11]=>
string(8) "0.000000"
[12]=>
string(8) "0.000000"
[13]=>
string(8) "1.373565"
[14]=>
string(8) "2.747041"
[15]=>
string(9) "40.351291"
[16]=>
string(8) "5.043911"
[17]=>
string(1) "0"
[18]=>
string(1) "0"
[19]=>
string(1) "0"
[20]=>
string(1) "0"
[21]=>
string(1) "0"
[22]=>
string(1) "2"
[23]=>
string(1) "0"
[24]=>
string(2) "21"
[25]=>
string(6) "1.7544"
[26]=>
string(5) "23.00"
}
[20]=>
array(27) {
[0]=>
string(0) ""
[1]=>
string(0) ""
[2]=>
string(0) ""
[3]=>
string(6) "286395"
[4]=>
string(3) "426"
[5]=>
string(8) "0.001487"
[6]=>
string(8) "0.718754"
[7]=>
string(8) "0.471867"
[8]=>
string(3) "0.0"
[9]=>
string(8) "0.000000"
[10]=>
string(3) "0.0"
[11]=>
string(8) "0.246886"
[12]=>
string(8) "0.000000"
[13]=>
string(8) "1.437560"
[14]=>
string(8) "2.875126"
[15]=>
string(10) "823.421824"
[16]=>
string(8) "1.932915"
[17]=>
string(1) "0"
[18]=>
string(1) "1"
[19]=>
string(1) "0"
[20]=>
string(2) "12"
[21]=>
string(1) "5"
[22]=>
string(1) "4"
[23]=>
string(1) "0"
[24]=>
string(3) "129"
[25]=>
string(6) "5.4531"
[26]=>
string(6) "151.00"
}
[21]=>
array(1) {
[0]=>
NULL
}
}
Use array_combine and extend your code to:
$data = array_map("str_getcsv", preg_split('/\r*\n+|\r+/', $result));
$new_data = [];
// shift first element of $data, we will use it as keys.
$keys = array_shift($data);
foreach ($data as $item) {
// create key => value array
$new_data[] = array_combine($keys, $item);
}
Thanks to #Kazz - in case of warnings like
array_combine(): Both parameters should have an equal number of elements
you need to check if both elements are arrays and theirs sizes equals (with count(), for example).

PHP session saving array with POST values

First sorry for my English.
I have a weird problem using the php $_SESSION Object.
I spent 2 days without find the solution.
I am triying to save a multidimensional array that stores some post values.
I create an array with this values and then i create a multidimensional array containing this arrays.
Page 1 (setvalues in multi array ans save in session)-> GET -> Page 2 read the session.
My code Page 1:
First, save the post values into a object property and validate them.
public function validateData(){
$this->nombres=$_POST["nombre"];
$this->imp_nombres=$_POST["imp_nombre"];
$this->numeros=$_POST["numero"];
$this->imp_numeros=$_POST["imp_numero"];
$this->tallas=$_POST["talla"];
$this->cantidades=$_POST["cantidad"];
$this->productos=$_POST["products"];
$this->equipos=$_POST["equipo"];
if(WSI_Funtions::compareSizes($this->nombres,$this->imp_nombres,$this->numeros,$this->imp_numeros,$this->tallas,$this->cantidades,$this->productos,$this->equipos))
{
$this->isValidModel=true;
$this->saveProductsValues();
}
else{
$this->isValidModel=false;
$this->errorMessage="Los datos no son correctos. Los parametros no coinciden";
}
}
If data is ok, I save those values:
public function saveProductsValues()
{
$this->productsValues=array();
$this->productsValues["names"]=$this->nombres;
$this->productsValues["imp_nombres"]=$this->imp_nombres;
$this->productsValues["numeros"]=$this->numeros;
$this->productsValues["imp_numeros"]=$this->imp_numeros;
$this->productsValues["tallas"]=$this->tallas;
$this->productsValues["cantidades"]=$this->cantidades;
$this->productsValues["productos"]=$this->productos;
$this->productsValues["equipos"]=$this->equipos;
}
Then I save it in a session :
public function saveSessionValues()
{
$_SESSION['customer'] = $this->customerObject;
$_SESSION['productsValues'] =$this->productsValues;
echo var_dump($_SESSION['productsValues']);
}
The saveSessionValues echo print this:
array(8) { ["names"]=> array(12) { [0]=> string(0) "" [1]=> string(12) "ÁNGEL HDEZ." [2]=> string(11) "VUJASINOVIC" [3]=> string(4) "ABIA" [4]=> string(10) "MUTAKABBIR" [5]=> string(8) "PETROVIC" [6]=> string(5) "DOBOS" [7]=> string(4) "HOMS" [8]=> string(6) "CASTRO" [9]=> string(0) "" [10]=> string(0) "" [11]=> string(0) "" } ["imp_nombres"]=> array(12) { [0]=> string(0) "" [1]=> string(1) "1" [2]=> string(1) "1" [3]=> string(1) "1" [4]=> string(1) "1" [5]=> string(1) "1" [6]=> string(1) "1" [7]=> string(1) "1" [8]=> string(1) "1" [9]=> string(0) "" [10]=> string(0) "" [11]=> string(0) "" } ["numeros"]=> array(12) { [0]=> string(1) "7" [1]=> string(1) "8" [2]=> string(1) "9" [3]=> string(0) "" [4]=> string(2) "11" [5]=> string(2) "12" [6]=> string(2) "18" [7]=> string(2) "19" [8]=> string(2) "22" [9]=> string(1) "5" [10]=> string(2) "33" [11]=> string(0) "" } ["imp_numeros"]=> array(12) { [0]=> string(1) "1" [1]=> string(1) "1" [2]=> string(1) "1" [3]=> string(0) "" [4]=> string(1) "1" [5]=> string(1) "1" [6]=> string(1) "1" [7]=> string(1) "1" [8]=> string(1) "1" [9]=> string(1) "1" [10]=> string(1) "1" [11]=> string(0) "" } ["tallas"]=> array(12) { [0]=> string(4) "XXXL" [1]=> string(3) "XXL" [2]=> string(2) "XL" [3]=> string(3) "XXL" [4]=> string(3) "XXL" [5]=> string(3) "XXL" [6]=> string(4) "XXXL" [7]=> string(3) "XXL" [8]=> string(2) "XL" [9]=> string(4) "XXXL" [10]=> string(2) "XL" [11]=> string(3) "XXL" } ["cantidades"]=> array(12) { [0]=> string(1) "1" [1]=> string(1) "1" [2]=> string(1) "1" [3]=> string(1) "1" [4]=> string(3) "145" [5]=> string(1) "1" [6]=> string(1) "1" [7]=> string(1) "1" [8]=> string(1) "1" [9]=> string(1) "1" [10]=> string(1) "1" [11]=> string(1) "1" } ["productos"]=> array(12) { [0]=> string(3) "109" [1]=> string(3) "109" [2]=> string(3) "109" [3]=> string(3) "109" [4]=> string(3) "109" [5]=> string(3) "109" [6]=> string(3) "109" [7]=> string(3) "109" [8]=> string(3) "109" [9]=> string(3) "109" [10]=> string(3) "109" [11]=> string(3) "109" } ["equipos"]=> array(12) { [0]=> string(7) "LEB ORO" [1]=> string(7) "LEB ORO" [2]=> string(7) "LEB ORO" [3]=> string(7) "LEB ORO" [4]=> string(7) "LEB ORO" [5]=> string(7) "LEB ORO" [6]=> string(12) "ES TALLA 4XL" [7]=> string(7) "LEB ORO" [8]=> string(7) "LEB ORO" [9]=> string(12) "ES TALLA 4XL" [10]=> string(7) "LEB ORO" [11]=> string(7) "LEB ORO" } }
My code Page 2 (php tags ommitted):
if (!isset($_SESSION)) { session_start(); }
echo var_dump($_SESSION['productsValues']);
This echo print the next value:
array(8) { ["names"]=> NULL ["imp_nombres"]=> NULL ["numeros"]=> NULL ["imp_numeros"]=> NULL ["tallas"]=> NULL ["cantidades"]=> NULL ["productos"]=> NULL ["equipos"]=> NULL }
The first level array exists, because the subarrays keys are printed, but all the second level arrays are NULL..
Maybe is for use the $_POST value??
I had try to encode the values of the $_POST object, saving only a json string instead of saving a object with the same result, the first nodes in the JSON are the keys of the arrays but the values are "NULL"
Any help please??
Thanks!!
You are over writing the values. Try with -
$_SESSION['productsValues'][] = $this->productsValues;

Using switch statements with Mysql and php PDO

So I can read two tables by using the following sql statement
$result = $dbc->query("SELECT table1.*, table2.* FROM table1 INNER JOIN table2 WHERE table1.column_name = 'variable' AND table2.column_name = 'variable2'")
Now I want to use a switch statement to read from each row determined by the variable in the columns. so for example
$q = $result->fetch();
switch ($q['column_name']) {
case "i":
echo ///results
break;
case "e":
echo // different results
break;
default:
//whatever code
}
I'm trying to echo two types of images from the two different tables based on the string under the column_name. But the switch method doesn't echo anything so I must not be calling be reading string from the column_name, any help?
Here is the var_dump that I'm getting:
array(46) { ["ad_id"]=> string(1) "1" [0]=> string(1) "1" ["user_id"]=> string(1) "0" [1]=> string(1) "0" ["global"]=> string(0) "" [2]=> string(0) "" ["title"]=> string(7) "Testing" [3]=> string(7) "Testing" ["categories"]=> string(15) "music altnindie" [4]=> string(4) "blog" ["location"]=> string(9) "australia" [5]=> string(7) "austria" ["description"]=> string(16) "New King in town" [6]=> string(8) "A new ad" ["link"]=> string(12) "www.link.com" [7]=> string(12) "www.link.com" ["link_clicks"]=> string(1) "0" [8]=> string(1) "0" ["image_file_path"]=> string(43) "******** 2015-03-07 00.48.33.png" [9]=> string(59) "adimages/11014891_660400880771405_6666691584554052971_n.jpg" ["thumb_file_path"]=> string(52) "********* 2015-03-07 00.48.33.png" [10]=> string(68) "*******-11014891_660400880771405_6666691584554052971_n.jpg" ["date_post"]=> string(19) "2015-05-15 11:14:32" [11]=> string(19) "2015-05-15 12:36:05" ["share_count"]=> string(1) "0" [12]=> string(1) "0" ["type"]=> string(5) "audio" [13]=> string(5) "image" [14]=> string(1) "1" [15]=> string(1) "0" [16]=> string(0) "" [17]=> string(7) "Testing" [18]=> string(15) "music altnindie" [19]=> string(9) "australia" [20]=> string(16) "New King in town" ["plays"]=> string(1) "0" [21]=> string(1) "0" [22]=> string(12) "www.link.com" [23]=> string(1) "0" ["audio_file_path"]=> string(50) "*******/audioI0n7Dr" [24]=> string(50) "*****/audioI0n7Dr" [25]=> string(43) "**** 2015-03-07 00.48.33.png" [26]=> string(52) "***** 2015-03-07 00.48.33.png" [27]=> string(19) "2015-05-15 11:14:32" [28]=> string(1) "0" [29]=> string(5) "audio" }

How to read var_dump in order to know how to load its variable contents in to an array with a foreach?

Here is a sample var_dump. Now, how do I know how to construct a foreach from it to load any particular word or fragment in to an array?
object(stdClass)#1 (2)
{
["noun"]=>
object(stdClass)#2 (1)
{
["syn"]=> array(24)
{
[0]=> string(12) "domestic dog"
[1]=> string(16) "Canis familiaris"
[2]=> string(5) "frump"
[3]=> string(3) "cad"
[4]=> string(7) "bounder"
[5]=> string(10) "blackguard"
[6]=> string(5) "hound"
[7]=> string(4) "heel"
[8]=> string(5) "frank"
[9]=> string(11) "frankfurter"
[10]=> string(6) "hotdog"
[11]=> string(7) "hot dog"
[12]=> string(6) "wiener"
[13]=> string(11) "wienerwurst"
[14]=> string(6) "weenie"
[15]=> string(4) "pawl"
[16]=> string(6) "detent"
[17]=> string(5) "click"
[18]=> string(7) "andiron"
[19]=> string(7) "firedog"
[20]=> string(8) "dog-iron"
[21]=> string(8) "blighter"
[22]=> string(5) "canid"
[23]=> string(6) "canine"
[24]=> string(5) "catch"
}
}
}
Before we can decipher it, we have to format it.
Tobias Kun's answer shows a very good way to format the var_dump output so you can read it.
object(stdClass)#1 (2)
{
["noun"]=>
object(stdClass)#2 (1)
{
["syn"]=> array(24)
{
[0]=> string(12) "domestic dog"
[1]=> string(16) "Canis familiaris"
[2]=> string(5) "frump"
[3]=> string(3) "cad"
[4]=> string(7) "bounder"
[5]=> string(10) "blackguard"
[6]=> string(5) "hound"
[7]=> string(4) "heel"
[8]=> string(5) "frank"
[9]=> string(11) "frankfurter"
[10]=> string(6) "hotdog"
[11]=> string(7) "hot dog"
[12]=> string(6) "wiener"
[13]=> string(11) "wienerwurst"
[14]=> string(6) "weenie"
[15]=> string(4) "pawl"
[16]=> string(6) "detent"
[17]=> string(5) "click"
[18]=> string(7) "andiron"
[19]=> string(7) "firedog"
[20]=> string(8) "dog-iron"
[21]=> string(8) "blighter"
[22]=> string(5) "canid"
[23]=> string(6) "canine"
[24]=> string(5) "catch"
}
}
}
You have a stdClass object with a property called ,"noun". noun is an abject with a property called "syn", which is an array of strings.
Suppose we call the object $object. Then we can access the array like:
echo $object->noun->syn[23];
which gives us canine. So a loop might look like:
foreach($data->noun->syn as $value) {
echo $value . "<br>\n";
}
First of all you should really increase the quality of your questions. The code is not formatted at all.
If you use echo "<pre>" . print_r($your_data_object_or_array,1) . "</pre>" your data will be formatted fine.
If i understand you right this should help you:
foreach($data['noun']['syn'] as $value) {
//with this you loop through all your words in "syn" e.g. domestic, "Canis familiaris etc."
echo $value . "<br>";
}
//Ouput:
domestic
Canis familiaris
frump
cad
etc ...

Categories