json_encode() doesn't return results? - php

So I have an array named $subcats like this :
Array
(
[83] => Array
(
[0] => Array
(
[id_sous_categ] => 4
[val] => filtre a essance
)
[1] => Array
(
[id_sous_categ] => 6
[val] => filtre AIR
)
)
[89] => Array
(
[0] => Array
(
[id_sous_categ] => 8
[val] => plaquette de frein
)
[1] => Array
(
[id_sous_categ] => 9
[val] => disque de frien
)
)
)
When i'm doing : $jsonSubCats = json_encode($subcats);
The result is empty.. What am I doing wrong ?
Edit : var_dump($subcats) shows :
array(7) { [83]=> array(4) { [0]=> array(2) { ["id_sous_categ"]=> string(1) "4" ["val"]=> string(16) "filtre a essance" } [1]=> array(2) { ["id_sous_categ"]=> string(1) "6" ["val"]=> string(10) "filtre AIR" } [2]=> array(2) { ["id_sous_categ"]=> string(1) "7" ["val"]=> string(14) "filtre a huile" } [3]=> array(2) { ["id_sous_categ"]=> string(2) "14" ["val"]=> string(16) "filtre a gasoile" } } [89]=> array(2) { [0]=> array(2) { ["id_sous_categ"]=> string(1) "8" ["val"]=> string(19) "plaquette de frein " } [1]=> array(2) { ["id_sous_categ"]=> string(1) "9" ["val"]=> string(15) "disque de frien" } } [84]=> array(6) { [0]=> array(2) { ["id_sous_categ"]=> string(2) "10" ["val"]=> string(23) "huile SAE 50 5L 5000 km" } [1]=> array(2) { ["id_sous_categ"]=> string(2) "11" ["val"]=> string(23) "huile SAE 50 1L 5000 km" } [2]=> array(2) { ["id_sous_categ"]=> string(2) "12" ["val"]=> string(22) "huile 15W40 5L 7000 km" } [3]=> array(2) { ["id_sous_categ"]=> string(2) "13" ["val"]=> string(22) "huile 15w40 1L 7000 km" } [4]=> array(2) { ["id_sous_categ"]=> string(2) "15" ["val"]=> string(22) "huile 10W40 5L10000 km" } [5]=> array(2) { ["id_sous_categ"]=> string(2) "16" ["val"]=> string(22) "huile 10W40 1L10000 km" } } [91]=> array(3) { [0]=> array(2) { ["id_sous_categ"]=> string(2) "17" ["val"]=> string(13) "joint culasse" } [1]=> array(2) { ["id_sous_categ"]=> string(2) "18" ["val"]=> string(20) "joint cache soupape " } [2]=> array(2) { ["id_sous_categ"]=> string(2) "19" ["val"]=> string(13) "joint carter " } } [86]=> array(6) { [0]=> array(2) { ["id_sous_categ"]=> string(2) "20" ["val"]=> string(6) "avant " } [1]=> array(2) { ["id_sous_categ"]=> string(2) "21" ["val"]=> string(7) "arrier " } [2]=> array(2) { ["id_sous_categ"]=> string(2) "22" ["val"]=> string(18) "amortisseur avant " } [3]=> array(2) { ["id_sous_categ"]=> string(2) "23" ["val"]=> string(18) "amortisseur arrier" } [4]=> array(2) { ["id_sous_categ"]=> string(2) "24" ["val"]=> string(28) "biellette suspension arriere" } [5]=> array(2) { ["id_sous_categ"]=> string(2) "25" ["val"]=> string(26) "biellette suspension avant" } } [96]=> array(1) { [0]=> array(2) { ["id_sous_categ"]=> string(2) "26" ["val"]=> string(6) "moteur" } } [80]=> array(1) { [0]=> array(2) { ["id_sous_categ"]=> string(2) "27" ["val"]=> string(15) "but�e embrayage" } } }
Thanks in advance.

json_encode fails to encode non-utf8 strings. Check your strings encoding. If that's the case, you have to either convert strings to utf8 or use your own implementation of json_encode (which you can quickly google by phrase "php json_encode implementation").

Make sure you have json support enabled in your server.
Run a simple <?php phpinfo() ?> in any php file and search for JSON.

Related

PHP: Convert Google Sheets result object to simple array

I've google sheet with this data:
I'm pulling it from google drive with simple code:
$ranges = [
'Price_1'
];
$params = array(
'ranges' => $ranges
);
$response = $service->spreadsheets_values->batchGet($spreadsheetId, $params);
The response is a complicated object:
object(Google_Service_Sheets_BatchGetValuesResponse)#58 (8) { ["collection_key":protected]=> string(11) "valueRanges" ["spreadsheetId"]=> string(44) "1BQpKLuvlaVeGqIqoGUOynF9IZ5C3zK6gagZiRJO1UEU" ["valueRangesType":protected]=> string(32) "Google_Service_Sheets_ValueRange" ["valueRangesDataType":protected]=> string(5) "array" ["internal_gapi_mappings":protected]=> array(0) { } ["modelData":protected]=> array(0) { } ["processed":protected]=> array(0) { } ["valueRanges"]=> array(1) { [0]=> object(Google_Service_Sheets_ValueRange)#67 (7) { ["collection_key":protected]=> string(6) "values" ["majorDimension"]=> string(4) "ROWS" ["range"]=> string(14) "Price_1!E2:E74" ["values"]=> array(65) { [0]=> array(1) { [0]=> string(2) "28" } [1]=> array(1) { [0]=> string(2) "14" } [2]=> array(1) { [0]=> string(1) "0" } [3]=> array(0) { } [4]=> array(1) { [0]=> string(2) "55" } [5]=> array(1) { [0]=> string(2) "28" } [6]=> array(1) { [0]=> string(1) "0" } [7]=> array(0) { } [8]=> array(0) { } [9]=> array(0) { } [10]=> array(0) { } [11]=> array(0) { } [12]=> array(0) { } [13]=> array(0) { } [14]=> array(0) { } [15]=> array(0) { } [16]=> array(0) { } [17]=> array(0) { } [18]=> array(0) { } [19]=> array(0) { } [20]=> array(1) { [0]=> string(2) "59" } [21]=> array(1) { [0]=> string(2) "49" } [22]=> array(0) { } [23]=> array(1) { [0]=> string(2) "38" } [24]=> array(1) { [0]=> string(2) "38" } [25]=> array(0) { } [26]=> array(1) { [0]=> string(2) "37" } [27]=> array(1) { [0]=> string(2) "25" } [28]=> array(1) { [0]=> string(1) "0" } [29]=> array(0) { } [30]=> array(1) { [0]=> string(2) "79" } [31]=> array(1) { [0]=> string(2) "99" } [32]=> array(0) { } [33]=> array(1) { [0]=> string(2) "75" } [34]=> array(1) { [0]=> string(2) "99" } [35]=> array(0) { } [36]=> array(1) { [0]=> string(2) "79" } [37]=> array(1) { [0]=> string(2) "99" } [38]=> array(0) { } [39]=> array(1) { [0]=> string(3) "160" } [40]=> array(1) { [0]=> string(3) "190" } [41]=> array(0) { } [42]=> array(0) { } [43]=> array(0) { } [44]=> array(0) { } [45]=> array(0) { } [46]=> array(0) { } [47]=> array(0) { } [48]=> array(0) { } [49]=> array(0) { } [50]=> array(0) { } [51]=> array(0) { } [52]=> array(1) { [0]=> string(2) "33" } [53]=> array(0) { } [54]=> array(1) { [0]=> string(2) "59" } [55]=> array(1) { [0]=> string(2) "34" } [56]=> array(1) { [0]=> string(1) "0" } [57]=> array(0) { } [58]=> array(1) { [0]=> string(2) "29" } [59]=> array(1) { [0]=> string(2) "14" } [60]=> array(1) { [0]=> string(1) "0" } [61]=> array(0) { } [62]=> array(1) { [0]=> string(2) "37" } [63]=> array(1) { [0]=> string(2) "25" } [64]=> array(1) { [0]=> string(1) "0" } } ["internal_gapi_mappings":protected]=> array(0) { } ["modelData":protected]=> array(0) { } ["processed":protected]=> array(0) { } } } }
and var_dump($response->getValueRanges()[0]['values']); gives this result:
array(74) { [0]=> array(5) { [0]=> string(2) "ID" [1]=> string(10) "Ticket for" [2]=> string(9) "Excursion" [3]=> string(8) "Duration" [4]=> string(14) "Price (Brutto)" } [1]=> array(5) { [0]=> string(3) "01A" [1]=> string(13) "adult ( 12+ )" [2]=> string(9) "Catamaran" [3]=> string(2) "2h" [4]=> string(2) "28" } [2]=> array(5) { [0]=> string(3) "01B" [1]=> string(18) "child ( age 5-11 )" [2]=> string(9) "Catamaran" [3]=> string(2) "2h" [4]=> string(2) "14" } [3]=> array(5) { [0]=> string(3) "01C" [1]=> string(18) "toodler ( age 0-4)" [2]=> string(9) "Catamaran" [3]=> string(2) "2h" [4]=> string(1) "0" } [4]=> array(0) { } [5]=> array(5) { [0]=> string(3) "02A" [1]=> string(13) "adult, age 8+" [2]=> string(17) "Luxurt yacht trip" [3]=> string(2) "3h" [4]=> string(2) "55" } [6]=> array(5) { [0]=> string(3) "02B" [1]=> string(14) "child, age 3-7" [2]=> string(17) "Luxurt yacht trip" [3]=> string(2) "3h" [4]=> string(2) "28" } [7]=> array(5) { [0]=> string(3) "02C" [1]=> string(16) "toodler, age 0-2" [2]=> string(17) "Luxurt yacht trip" [3]=> string(2) "3h" [4]=> string(1) "0" } [8]=> array(0) { } [9]=> array(3) { [0]=> string(3) "03A" [1]=> string(0) "" [2]=> string(8) "Sailboat" } [10]=> array(3) { [0]=> string(3) "03B" [1]=> string(0) "" [2]=> string(8) "Sailboat" } [11]=> array(3) { [0]=> string(3) "03C" [1]=> string(0) "" [2]=> string(8) "Sailboat" } [12]=> array(0) { } [13]=> array(3) { [0]=> string(3) "04A" [1]=> string(0) "" [2]=> string(9) "Submarine" } [14]=> array(3) { [0]=> string(3) "04B" [1]=> string(0) "" [2]=> string(9) "Submarine" } [15]=> array(3) { [0]=> string(3) "04C" [1]=> string(0) "" [2]=> string(9) "Submarine" } [16]=> array(0) { } [17]=> array(3) { [0]=> string(3) "05A" [1]=> string(0) "" [2]=> string(13) "JetSki safari" } [18]=> array(3) { [0]=> string(3) "05B" [1]=> string(0) "" [2]=> string(13) "JetSki safari" } [19]=> array(3) { [0]=> string(3) "05C" [1]=> string(0) "" [2]=> string(13) "JetSki safari" } [20]=> array(0) { } [21]=> array(5) { [0]=> string(3) "06A" [1]=> string(9) "fisherman" [2]=> string(7) "Fishing" [3]=> string(2) "4h" [4]=> string(2) "59" } [22]=> array(5) { [0]=> string(3) "06B" [1]=> string(9) "spectator" [2]=> string(7) "Fishing" [3]=> string(2) "4h" [4]=> string(2) "49" } [23]=> array(0) { } [24]=> array(5) { [0]=> string(3) "07A" [1]=> string(5) "adult" [2]=> string(12) "Parascending" [3]=> string(5) "30min" [4]=> string(2) "38" } [25]=> array(5) { [0]=> string(3) "07B" [1]=> string(13) "child, age 6+" [2]=> string(12) "Parascending" [3]=> string(5) "30min" [4]=> string(2) "38" } [26]=> array(0) { } [27]=> array(5) { [0]=> string(3) "08A" [1]=> string(14) "adult, age 12+" [2]=> string(9) "Siam Park" [3]=> string(0) "" [4]=> string(2) "37" } [28]=> array(5) { [0]=> string(3) "08B" [1]=> string(15) "child, age 3-11" [2]=> string(9) "Siam Park" [3]=> string(0) "" [4]=> string(2) "25" } [29]=> array(5) { [0]=> string(3) "08C" [1]=> string(16) "toodler, age 0-1" [2]=> string(9) "Siam Park" [3]=> string(0) "" [4]=> string(1) "0" } [30]=> array(0) { } [31]=> array(5) { [0]=> string(3) "09A" [1]=> string(17) "flight from 800m." [2]=> string(18) "Paragliding tandem" [3]=> string(5) "15min" [4]=> string(2) "79" } [32]=> array(5) { [0]=> string(3) "09B" [1]=> string(18) "flight from 1000m." [2]=> string(18) "Paragliding tandem" [3]=> string(5) "25min" [4]=> string(2) "99" } [33]=> array(0) { } [34]=> array(5) { [0]=> string(3) "10A" [1]=> string(21) "Single (for 1 person)" [2]=> string(27) "Quad safari Forest explorer" [3]=> string(2) "3h" [4]=> string(2) "75" } [35]=> array(5) { [0]=> string(3) "10B" [1]=> string(22) "Double (for 2 persons)" [2]=> string(27) "Quad safari Forest explorer" [3]=> string(2) "3h" [4]=> string(2) "99" } [36]=> array(0) { } [37]=> array(5) { [0]=> string(3) "11A" [1]=> string(21) "Single (for 1 person)" [2]=> string(24) "Quad Safari - Teide tour" [3]=> string(2) "4h" [4]=> string(2) "79" } [38]=> array(5) { [0]=> string(3) "11B" [1]=> string(22) "Double (for 2 persons)" [2]=> string(24) "Quad Safari - Teide tour" [3]=> string(2) "4h" [4]=> string(2) "99" } [39]=> array(0) { } [40]=> array(5) { [0]=> string(3) "12A" [1]=> string(21) "Single (for 1 person)" [2]=> string(15) "Buggy adventure" [3]=> string(2) "3h" [4]=> string(3) "160" } [41]=> array(5) { [0]=> string(3) "12B" [1]=> string(22) "Double (for 2 persons)" [2]=> string(15) "Buggy adventure" [3]=> string(2) "3h" [4]=> string(3) "190" } [42]=> array(0) { } [43]=> array(3) { [0]=> string(3) "13A" [1]=> string(0) "" [2]=> string(8) "Trekking" } [44]=> array(3) { [0]=> string(3) "13B" [1]=> string(0) "" [2]=> string(8) "Trekking" } [45]=> array(3) { [0]=> string(3) "13C" [1]=> string(0) "" [2]=> string(8) "Trekking" } [46]=> array(0) { } [47]=> array(3) { [0]=> string(3) "14A" [1]=> string(0) "" [2]=> string(7) "Karting" } [48]=> array(3) { [0]=> string(3) "14B" [1]=> string(0) "" [2]=> string(7) "Karting" } [49]=> array(3) { [0]=> string(3) "14C" [1]=> string(0) "" [2]=> string(7) "Karting" } [50]=> array(0) { } [51]=> array(3) { [0]=> string(3) "15A" [1]=> string(0) "" [2]=> string(11) "Jungle park" } [52]=> array(3) { [0]=> string(3) "15B" [1]=> string(0) "" [2]=> string(11) "Jungle park" } [53]=> array(5) { [0]=> string(3) "15C" [1]=> string(0) "" [2]=> string(11) "Jungle park" [3]=> string(0) "" [4]=> string(2) "33" } [54]=> array(0) { } [55]=> array(5) { [0]=> string(3) "16A" [1]=> string(14) "adult, age 12+" [2]=> string(16) "La Gomera island" [3]=> string(12) "7:40 - 18:00" [4]=> string(2) "59" } [56]=> array(5) { [0]=> string(3) "16B" [1]=> string(15) "child, age 2-11" [2]=> string(16) "La Gomera island" [3]=> string(12) "7:40 - 18:00" [4]=> string(2) "34" } [57]=> array(5) { [0]=> string(3) "16C" [1]=> string(16) "toodler, age 0-1" [2]=> string(16) "La Gomera island" [3]=> string(12) "7:40 - 18:00" [4]=> string(1) "0" } [58]=> array(0) { } [59]=> array(5) { [0]=> string(3) "17A" [1]=> string(14) "adult, age 12+" [2]=> string(31) "Santa Cruz/ La Laguna/ Taganana" [3]=> string(1) "?" [4]=> string(2) "29" } [60]=> array(5) { [0]=> string(3) "17B" [1]=> string(15) "child, age 2-11" [2]=> string(31) "Santa Cruz/ La Laguna/ Taganana" [3]=> string(1) "?" [4]=> string(2) "14" } [61]=> array(5) { [0]=> string(3) "17C" [1]=> string(16) "toodler, age 0-1" [2]=> string(31) "Santa Cruz/ La Laguna/ Taganana" [3]=> string(1) "?" [4]=> string(1) "0" } [62]=> array(0) { } [63]=> array(5) { [0]=> string(3) "18A" [1]=> string(14) "adult, age 12+" [2]=> string(11) "Loro parque" [3]=> string(12) "8:30 - 18:45" [4]=> string(2) "37" } [64]=> array(5) { [0]=> string(3) "18B" [1]=> string(15) "child, age 6-11" [2]=> string(11) "Loro parque" [3]=> string(0) "" [4]=> string(2) "25" } [65]=> array(5) { [0]=> string(3) "18C" [1]=> string(16) "toodler, age 0-1" [2]=> string(11) "Loro parque" [3]=> string(0) "" [4]=> string(1) "0" } [66]=> array(0) { } [67]=> array(3) { [0]=> string(3) "19A" [1]=> string(0) "" [2]=> string(9) "Antologia" } [68]=> array(3) { [0]=> string(3) "19B" [1]=> string(0) "" [2]=> string(9) "Antologia" } [69]=> array(3) { [0]=> string(3) "19C" [1]=> string(0) "" [2]=> string(9) "Antologia" } [70]=> array(0) { } [71]=> array(3) { [0]=> string(3) "20A" [1]=> string(0) "" [2]=> string(13) "Medieval show" } [72]=> array(3) { [0]=> string(3) "20B" [1]=> string(0) "" [2]=> string(13) "Medieval show" } [73]=> array(3) { [0]=> string(3) "20C" [1]=> string(0) "" [2]=> string(13) "Medieval show" } }
And:
for($i = 1; $i < 5; $i++) {
print_r ($response->getValueRanges()[0]['values'][$i]);
}
Gives: Array ( [0] => 01A [1] => adult ( 12+ ) [2] => Catamaran [3] => 2h [4] => 28 ) Array ( [0] => 01B [1] => child ( age 5-11 ) [2] => Catamaran [3] => 2h [4] => 14 ) Array ( [0] => 01C [1] => toodler ( age 0-4) [2] => Catamaran [3] => 2h [4] => 0 ) Array ( )
In result I need a simple array, which will hold a key[row id], the ID from from ID row and price from last row, somethig like this: $result[$i] = unique_id=1, ID=01A, price=28
How it could be achieved?
So after getting the data for google sheet with:
$arr = $response->getValueRanges()[0]['values'];
You can use this simple for loop to get the data as you need:
// notice start from 1 to avoid the headers
for($i = 1; $i < count($arr); $i++) {
$res[$i] = array("unique_id" => $i, "ID" => $arr[$i][0], "price" => $arr[$i][4]);
}
Now $res will contain you structure.

Php getting data out of an array

I have a massive array that i can't get data out of. I can only get data out of the first set of items.
I need to get to
["USFICO"]=>
array(3) {
["FICOScore"]=>
string(5) "00650"
out of it. But i can only get the top portion to work w/ $tester = $array['EfxReport']['#attributes']['reportId']; outputs to string(22) "USConsumerCreditReport" as expected.
how can i get the FICO score out of this array.
Here is the array.
array(1) {
["EfxReport"]=>
array(3) {
["#attributes"]=>
array(2) {
["requestNumber"]=>
string(1) "1"
["reportId"]=>
string(22) "USConsumerCreditReport"
}
["USDecisionPowerExpressReports"]=>
array(1) {
["USDecisionPowerExpressReport"]=>
array(4) {
["#attributes"]=>
array(2) {
["subjectType"]=>
string(7) "Subject"
["multipleNumber"]=>
string(1) "1"
}
["USMasterHeader"]=>
array(5) {
["CustomerReferenceNumber"]=>
string(6) "EFX QA"
["CustomerNumber"]=>
string(10) "999KI00553"
["ECOAInquiryType"]=>
string(1) "I"
["DateOfRequest"]=>
string(10) "02/23/2017"
["EquifaxReferenceNumber"]=>
string(9) "187391427"
}
["USConsumerCreditReport"]=>
array(6) {
["#attributes"]=>
array(2) {
["subjectType"]=>
string(7) "Subject"
["multipleNumber"]=>
string(1) "1"
}
["USHeader"]=>
array(3) {
["Request"]=>
array(6) {
["CustomerReferenceNumber"]=>
string(6) "EFX QA"
["CustomerNumber"]=>
string(10) "999KI00553"
["ConsumerReferralCode"]=>
string(3) "024"
["ECOAInquiryType"]=>
string(1) "I"
["NumberOfMonthsToCountInquiries"]=>
string(14) "Last 24 Months"
["NumberOfMonthsToCountDelinquencies"]=>
string(23) "Last 6 Years, 11 Months"
}
["CreditFile"]=>
array(11) {
["HitCode"]=>
array(1) {
["#attributes"]=>
array(2) {
["code"]=>
string(1) "1"
["description"]=>
string(3) "Hit"
}
}
["FileSinceDate"]=>
string(10) "04/16/2003"
["DateOfLastActivity"]=>
string(10) "01/24/2017"
["DateOfRequest"]=>
string(10) "02/23/2017"
["Identityscans"]=>
array(1) {
["Identityscan"]=>
array(1) {
["#attributes"]=>
array(2) {
["code"]=>
string(1) "8"
["description"]=>
string(74) "Unable to perform telephone validation due to insufficient telephone input"
}
}
}
["AddressDiscrepancyIndicator"]=>
string(1) "N"
["CreateCode"]=>
string(1) "2"
["FileStatus1"]=>
string(3) "016"
["FileStatus2"]=>
string(3) "242"
["FileStatus3"]=>
string(3) "004"
["BureauCode"]=>
string(3) "244"
}
["Subject"]=>
array(2) {
["SubjectName"]=>
array(3) {
["LastName"]=>
string(7) "DGNNLXZ"
["FirstName"]=>
string(7) "CHANITA"
["MiddleName"]=>
string(1) "S"
}
["SubjectId"]=>
array(2) {
["SubjectSSN"]=>
string(8) "66639074"
["DateOfBirth"]=>
string(10) "08/17/1988"
}
}
}
["USAddresses"]=>
array(1) {
["USAddress"]=>
array(3) {
[0]=>
array(8) {
["#attributes"]=>
array(2) {
["code"]=>
string(2) "CA"
["description"]=>
string(15) "Current Address"
}
["ParsedStreetAddress"]=>
array(3) {
["StreetNumber"]=>
string(4) "4338"
["StreetName"]=>
string(9) "W POTOMAC"
["StreetType"]=>
string(3) "AVE"
}
["City"]=>
string(7) "CHICAGO"
["State"]=>
string(2) "IL"
["PostalCode"]=>
string(5) "60651"
["DateAddressFirstReported"]=>
string(7) "07/2015"
["AddressSource"]=>
array(1) {
["#attributes"]=>
array(2) {
["code"]=>
string(1) "T"
["description"]=>
string(4) "Tape"
}
}
["DateAddressLastReported"]=>
string(7) "02/2017"
}
[1]=>
array(8) {
["#attributes"]=>
array(2) {
["code"]=>
string(2) "FA"
["description"]=>
string(14) "Former Address"
}
["ParsedStreetAddress"]=>
array(3) {
["StreetNumber"]=>
string(4) "1301"
["StreetName"]=>
string(7) "BRUMMEL"
["StreetType"]=>
string(8) "ST APT 8"
}
["City"]=>
string(8) "EVANSTON"
["State"]=>
string(2) "IL"
["PostalCode"]=>
string(5) "60202"
["DateAddressFirstReported"]=>
string(7) "04/2015"
["AddressSource"]=>
array(1) {
["#attributes"]=>
array(2) {
["code"]=>
string(1) "T"
["description"]=>
string(4) "Tape"
}
}
["DateAddressLastReported"]=>
string(7) "04/2015"
}
[2]=>
array(8) {
["#attributes"]=>
array(2) {
["code"]=>
string(2) "F2"
["description"]=>
string(21) "Second Former Address"
}
["ParsedStreetAddress"]=>
array(3) {
["StreetNumber"]=>
string(4) "1208"
["StreetName"]=>
string(11) "W JEFFERSON"
["StreetType"]=>
string(8) "ST APT C"
}
["City"]=>
string(11) "SPRINGFIELD"
["State"]=>
string(2) "IL"
["PostalCode"]=>
string(5) "62702"
["DateAddressFirstReported"]=>
string(7) "06/2014"
["AddressSource"]=>
array(1) {
["#attributes"]=>
array(2) {
["code"]=>
string(1) "T"
["description"]=>
string(4) "Tape"
}
}
["DateAddressLastReported"]=>
string(7) "06/2014"
}
}
}
["USEmployments"]=>
array(1) {
["USEmployment"]=>
array(3) {
["#attributes"]=>
array(2) {
["code"]=>
string(2) "ES"
["description"]=>
string(24) "Last Reported Employment"
}
["Occupation"]=>
string(14) "CORRECTION OFF"
["Employer"]=>
string(18) "MACON STATE PRISON"
}
}
["USFICO"]=>
array(3) {
["FICOScore"]=>
string(5) "00650"
["ScoreReasons"]=>
array(1) {
["ScoreReason"]=>
array(4) {
[0]=>
array(1) {
["#attributes"]=>
array(3) {
["code"]=>
string(5) "00038"
["description"]=>
string(69) "Serious delinquency, and derogatory public record or collection filed"
["number"]=>
string(1) "1"
}
}
[1]=>
array(1) {
["#attributes"]=>
array(3) {
["code"]=>
string(5) "00010"
["description"]=>
string(76) "Prop of balances to credit limits is too high on bank rev or other rev accts"
["number"]=>
string(1) "2"
}
}
[2]=>
array(1) {
["#attributes"]=>
array(3) {
["code"]=>
string(5) "00018"
["description"]=>
string(35) "Number of accounts with delinquency"
["number"]=>
string(1) "3"
}
}
[3]=>
array(1) {
["#attributes"]=>
array(3) {
["code"]=>
string(5) "00014"
["description"]=>
string(45) "Length of time accounts have been established"
["number"]=>
string(1) "4"
}
}
}
}
["ScoreIndicator"]=>
array(1) {
["#attributes"]=>
array(2) {
["code"]=>
string(1) "J"
["description"]=>
string(39) "FICO Score 5 based on Equifax Data (NF)"
}
}
}
["USIdentificationSSN"]=>
array(4) {
["MDBSubjectSSN"]=>
string(9) "666390749"
["InquirySubjectSSN"]=>
string(9) "666390749"
["InquirySSNDateIssued"]=>
string(4) "1992"
["InquirySSNStateIssued"]=>
string(2) "GA"
}
}
["USDecisionPowerExpressSegment"]=>
array(4) {
["ConsumerDisclosureIndicator"]=>
array(0) {
}
["ReportTextMessage"]=>
string(7) "OFFER C"
["SSNVarianceIndicator"]=>
array(0) {
}
["DPExpressProducts"]=>
array(1) {
["DPExpressProduct"]=>
array(4) {
[0]=>
array(5) {
["#attributes"]=>
array(1) {
["number"]=>
string(1) "1"
}
["Description"]=>
string(1) "A"
["ApprovalIndicator"]=>
string(1) "N"
["Limit"]=>
array(0) {
}
["Miscellaneous"]=>
array(0) {
}
}
[1]=>
array(5) {
["#attributes"]=>
array(1) {
["number"]=>
string(1) "2"
}
["Description"]=>
string(1) "B"
["ApprovalIndicator"]=>
string(1) "N"
["Limit"]=>
array(0) {
}
["Miscellaneous"]=>
array(0) {
}
}
[2]=>
array(5) {
["#attributes"]=>
array(1) {
["number"]=>
string(1) "3"
}
["Description"]=>
string(1) "C"
["ApprovalIndicator"]=>
string(1) "Y"
["Limit"]=>
array(0) {
}
["Miscellaneous"]=>
array(0) {
}
}
[3]=>
array(5) {
["#attributes"]=>
array(1) {
["number"]=>
string(1) "4"
}
["Description"]=>
string(1) "D"
["ApprovalIndicator"]=>
string(1) "N"
["Limit"]=>
array(0) {
}
["Miscellaneous"]=>
array(0) {
}
}
}
}
}
}
}
["USPrintImage"]=>
array(0) {
}
}
}
What's the error you're getting? Try:
$tester = $array['EfxReport']['USDecisionPowerExpressReports']['USDecisionPowerExpressReport']['USConsumerCreditReport']['USFICO']['FICOScore'];
Also I really hope this is fake data, you've included very personal/sensitive information in your array printout.

using array_unique, I appear to have stripped out the records that were duplicated?

Within available_options I have somehow stripped out Express when I just wanted to keep one of them?
The array looks like this
["options"]=>
array(9) {
[0]=>
array(8) {
["id"]=>
string(2) "79"
["product_id"]=>
string(2) "15"
["sku"]=>
string(9) "CSR-FTC4S"
["status"]=>
string(1) "1"
["is_default"]=>
string(1) "0"
["option_price"]=>
string(6) "35.000"
["sequence"]=>
string(4) "9999"
["available_options"]=>
array(3) {
[0]=>
array(6) {
["id"]=>
string(3) "219"
["product_options_base_id"]=>
string(2) "79"
["option_id"]=>
string(2) "16"
["option_data_id"]=>
string(1) "1"
["sequence"]=>
string(4) "9999"
["option_data"]=>
array(1) {
[0]=>
array(8) {
["id"]=>
string(1) "1"
["admin_name"]=>
string(19) "Five Ten C4 Stealth"
["name"]=>
string(11) "Resole Type"
["sku"]=>
string(5) "FTC4S"
["user_value"]=>
string(25) "Five Ten C4 Stealth 5.5mm"
["sequence"]=>
string(1) "0"
["status"]=>
string(1) "1"
["option_price"]=>
string(5) "0.000"
}
}
}
[1]=>
array(6) {
["id"]=>
string(3) "220"
["product_options_base_id"]=>
string(2) "79"
["option_id"]=>
string(2) "12"
["option_data_id"]=>
string(1) "1"
["sequence"]=>
string(4) "9999"
["option_data"]=>
array(1) {
[0]=>
array(8) {
["id"]=>
string(1) "1"
["admin_name"]=>
string(7) "Express"
["name"]=>
string(7) "Express"
["sku"]=>
string(3) "EXP"
["user_value"]=>
string(1) "1"
["sequence"]=>
string(4) "9999"
["status"]=>
string(1) "1"
["option_price"]=>
string(6) "25.000"
}
}
}
[2]=>
array(6) {
["id"]=>
string(3) "221"
["product_options_base_id"]=>
string(2) "79"
["option_id"]=>
string(2) "23"
["option_data_id"]=>
string(1) "1"
["sequence"]=>
string(4) "9999"
["option_data"]=>
array(1) {
[0]=>
array(8) {
["id"]=>
string(1) "1"
["admin_name"]=>
string(16) "Rand Toe Patches"
["name"]=>
string(3) "RTP"
["sku"]=>
string(3) "RTP"
["user_value"]=>
string(1) "1"
["sequence"]=>
string(4) "9999"
["status"]=>
string(1) "1"
["option_price"]=>
string(6) "10.000"
}
}
}
}
}
[1]=>
array(8) {
["id"]=>
string(2) "80"
["product_id"]=>
string(2) "15"
["sku"]=>
string(10) "CSR-FTONYX"
["status"]=>
string(1) "1"
["is_default"]=>
string(1) "0"
["option_price"]=>
string(6) "37.000"
["sequence"]=>
string(4) "9999"
["available_options"]=>
array(3) {
[0]=>
array(6) {
["id"]=>
string(3) "222"
["product_options_base_id"]=>
string(2) "80"
["option_id"]=>
string(2) "16"
["option_data_id"]=>
string(1) "2"
["sequence"]=>
string(4) "9999"
["option_data"]=>
array(1) {
[0]=>
array(8) {
["id"]=>
string(1) "2"
["admin_name"]=>
string(13) "Five Ten Onyx"
["name"]=>
string(11) "Resole Type"
["sku"]=>
string(6) "FTONYX"
["user_value"]=>
string(19) "Five Ten Onyx 4.5mm"
["sequence"]=>
string(1) "1"
["status"]=>
string(1) "1"
["option_price"]=>
string(5) "0.000"
}
}
}
[1]=>
array(6) {
["id"]=>
string(3) "223"
["product_options_base_id"]=>
string(2) "80"
["option_id"]=>
string(2) "12"
["option_data_id"]=>
string(1) "1"
["sequence"]=>
string(4) "9999"
["option_data"]=>
array(1) {
[0]=>
array(8) {
["id"]=>
string(1) "1"
["admin_name"]=>
string(7) "Express"
["name"]=>
string(7) "Express"
["sku"]=>
string(3) "EXP"
["user_value"]=>
string(1) "1"
["sequence"]=>
string(4) "9999"
["status"]=>
string(1) "1"
["option_price"]=>
string(6) "25.000"
}
}
}
and my code goes like this
foreach($this->_data as &$data) {
foreach($data['options'] as &$option) {
$option['available_options'] = array_unique($option['available_options']);
}
}
It's working apart from it's stripped out the duplicates rather than showing them once?
array_unique does not work recursively, you need to go inside your array to apply it on option_data directly.
foreach($this->_data as &$data) {
foreach ($data['options'] as &$option) {
foreach ($option['available_options'] as &$available_option) {
foreach ($available_option['option_data'] as &$option_data) {
$option_data = array_unique($option_data);
}
}
}
}
This way, the last option_data looks like
'option_data' => [
[
'id' => '1',
'admin_name' => 'Express',
'sku' => 'EXP',
'sequence' => '9999',
'option_price' => '25.000'
]
]
But as you can see, the value Express only appear once, but user_value and status are removed too, because there value is 1, like id.

Data mismatch while dumping array in csv file

I have 7 array fetched by mysql and its working correctly each array have exactly 9 rows which are city_names but one of the array has only 7 rows since two values on the results where null . This causes confunsion while dumping file in csv. I use the below code. Is there any way to check it by city name without using foreach for eact array ?
for($i=0 ; $i <= $count-2 ; $i++)
{
$data[] = $all_restaurants_opr_no_temp_off[$i]['city_name'];
$data[] = $all_restaurants_opr[$i]['total'];
$data[] = $all_restaurants_opr_no_temp_off[$i]['total'];
$data[] = $restaurants_opr_temp_off[$i]['total'];
$data[] = $restaurants_opr_operations_closed[$i]['total'];
$data[] = $restaurants_opr_automated[$i]['total'];
$data[] = $restaurants_opr_automated_working[$i]['total'];
$data[] = $restaurants_opr_online_payment[$i]['total'];
$data[] = $restaurants_opr_online_payment_with_tempoff[$i]['total'];
$data[] = $restaurants_opr_atleast_one_order[$i]['total'];
fputcsv($fp, $data,",");
unset($data);
}
Array 1:
array(9) {
[0]=>
array(2) {
["city_name"]=>
string(9) "Bangalore"
["total"]=>
string(3) "687"
}
[1]=>
array(2) {
["city_name"]=>
string(9) "Hyderabad"
["total"]=>
string(2) "16"
}
[2]=>
array(2) {
["city_name"]=>
string(6) "Mumbai"
["total"]=>
string(3) "568"
}
[3]=>
array(2) {
["city_name"]=>
string(7) "Chennai"
["total"]=>
string(3) "139"
}
[4]=>
array(2) {
["city_name"]=>
string(4) "Pune"
["total"]=>
string(3) "232"
}
[5]=>
array(2) {
["city_name"]=>
string(9) "Ghaziabad"
["total"]=>
string(2) "57"
}
[6]=>
array(2) {
["city_name"]=>
string(5) "Noida"
["total"]=>
string(2) "77"
}
[7]=>
array(2) {
["city_name"]=>
string(9) "Faridabad"
["total"]=>
string(1) "4"
}
[8]=>
array(2) {
["city_name"]=>
string(7) "Gurgaon"
["total"]=>
string(3) "113"
}
}
Array 2:
array(9) {
[0]=>
array(2) {
["city_name"]=>
string(9) "Bangalore"
["total"]=>
string(3) "674"
}
[1]=>
array(2) {
["city_name"]=>
string(9) "Hyderabad"
["total"]=>
string(2) "16"
}
[2]=>
array(2) {
["city_name"]=>
string(6) "Mumbai"
["total"]=>
string(3) "547"
}
[3]=>
array(2) {
["city_name"]=>
string(7) "Chennai"
["total"]=>
string(3) "135"
}
[4]=>
array(2) {
["city_name"]=>
string(4) "Pune"
["total"]=>
string(3) "202"
}
[5]=>
array(2) {
["city_name"]=>
string(9) "Ghaziabad"
["total"]=>
string(2) "56"
}
[6]=>
array(2) {
["city_name"]=>
string(5) "Noida"
["total"]=>
string(2) "77"
}
[7]=>
array(2) {
["city_name"]=>
string(9) "Faridabad"
["total"]=>
string(1) "4"
}
[8]=>
array(2) {
["city_name"]=>
string(7) "Gurgaon"
["total"]=>
string(3) "111"
}
}
Array 3:(Problem is here)
array(6) {
[0]=>
array(2) {
["city_name"]=>
string(9) "Bangalore"
["total"]=>
string(2) "13"
}
[1]=>
array(2) {
["city_name"]=>
string(6) "Mumbai"
["total"]=>
string(2) "21"
}
[2]=>
array(2) {
["city_name"]=>
string(7) "Chennai"
["total"]=>
string(1) "4"
}
[3]=>
array(2) {
["city_name"]=>
string(4) "Pune"
["total"]=>
string(2) "30"
}
[4]=>
array(2) {
["city_name"]=>
string(9) "Ghaziabad"
["total"]=>
string(1) "1"
}
[5]=>
array(2) {
["city_name"]=>
string(7) "Gurgaon"
["total"]=>
string(1) "2"
}
}
I'm not sure I understand the question 100%, but perhaps you can test each value if it is null and put a default value there if it is, for example, for this row, you could use this above your for loop:
foreach($badArray as $b){
$reformattedArray[$b['city_name']] = $b['total'];
}
then replace the bad array line with this in your for loop
data[] = is_null($reformattedArray[$all_restaurants_opr_no_temp_off[$i]['city_name']]) ? 'Default' : $reformattedArray[$all_restaurants_opr_no_temp_off[$i]['city_name']];

Displaying an array of values via foreach loop and how can I limit the array to be displayed?

I want to know how can I limit the values of array to be displayed. Assuming my $color is equals to Indigo
I tried implementing this code by it doesn't work
foreach($data as $item){
if($item['color'] == $color){
echo $item['size'].'-'.$item['color'].'-'.$item['price'].'<br>';
}
}
And if I remove the if statement the var_dump($data)
array(16) {
[0]=>
array(3) {
["size"]=>
string(1) "2"
["price"]=>
string(6) "$59.00"
["color"]=>
string(11) "Black Rinse"
}
[1]=>
array(3) {
["size"]=>
string(1) "2"
["price"]=>
string(6) "$59.00"
["color"]=>
string(6) "Indigo"
}
[2]=>
array(3) {
["size"]=>
string(1) "4"
["price"]=>
string(6) "$59.00"
["color"]=>
string(11) "Black Rinse"
}
[3]=>
array(3) {
["size"]=>
string(1) "4"
["price"]=>
string(6) "$59.00"
["color"]=>
string(6) "Indigo"
}
[4]=>
array(3) {
["size"]=>
string(1) "6"
["price"]=>
string(6) "$59.00"
["color"]=>
string(11) "Black Rinse"
}
[5]=>
array(3) {
["size"]=>
string(1) "6"
["price"]=>
string(6) "$59.00"
["color"]=>
string(6) "Indigo"
}
[6]=>
array(3) {
["size"]=>
string(1) "8"
["price"]=>
string(6) "$59.00"
["color"]=>
string(11) "Black Rinse"
}
[7]=>
array(3) {
["size"]=>
string(1) "8"
["price"]=>
string(6) "$59.00"
["color"]=>
string(6) "Indigo"
}
[8]=>
array(3) {
["size"]=>
string(2) "10"
["price"]=>
string(6) "$59.00"
["color"]=>
string(11) "Black Rinse"
}
[9]=>
array(3) {
["size"]=>
string(2) "10"
["price"]=>
string(6) "$59.00"
["color"]=>
string(6) "Indigo"
}
[10]=>
array(3) {
["size"]=>
string(2) "12"
["price"]=>
string(6) "$59.00"
["color"]=>
string(11) "Black Rinse"
}
[11]=>
array(3) {
["size"]=>
string(2) "12"
["price"]=>
string(6) "$59.00"
["color"]=>
string(6) "Indigo"
}
[12]=>
array(3) {
["size"]=>
string(2) "14"
["price"]=>
string(6) "$59.00"
["color"]=>
string(11) "Black Rinse"
}
[13]=>
array(3) {
["size"]=>
string(2) "14"
["price"]=>
string(6) "$59.00"
["color"]=>
string(6) "Indigo"
}
[14]=>
array(3) {
["size"]=>
string(2) "16"
["price"]=>
string(6) "$59.00"
["color"]=>
string(11) "Black Rinse"
}
[15]=>
array(3) {
["size"]=>
string(2) "16"
["price"]=>
string(6) "$59.00"
["color"]=>
string(6) "Indigo"
}
}
I want only to display values which is the color is equal to $color. How should I do this the correct way?
You might be dealing with case sensitivity or white space .. you can try :
$data = array(
0 => array("size" => "2","price" => "$59.00","color" => "Black Rinse"),
1 => array("size" => "2","price" => "$59.00","color" => "Indigo"));
$color = "Indigo";
foreach ( $data as $item ) {
if (strtolower($item['color']) == strtolower($color)) {
echo $item['size'] . '-' . $item['color'] . '-' . $item['price'] . '<br>';
}
}
Output
2-Indigo-$59.00
See Live Demo
This should fix your issue (assuming you're comparing two strings, I suggest you use strcasecmp- for case insensitive string comparison in your if loop)
foreach($data as $item){
if(strcasecmp(trim($item['color']),trim($color))==0){
echo $item['size'].'-'.$item['color'].'-'.$item['price'].'<br>';
}
}
Be elegant, be simple:
$items = array(
array('size'=>'4', 'color'=>'Indigo', 'price'=>'$59.00'),
array('size'=>'5', 'color'=>'black', 'price'=>'$59.00'),
array('size'=>'5', 'color'=>'green', 'price'=>'$54.00'),
array('size'=>'7', 'color'=>'Indigo', 'price'=>'$50.00'),
array('size'=>'5', 'color'=>'purple', 'price'=>'$51.00'));
print_r(array_filter($items, function($i){
return in_array('Indigo', $i);
})
);
or PHP < 5.3:
print_r(array_filter($items,
create_function('$i', 'return in_array("Indigo", $i);'))
);
Take advantage of features that PHP offers.

Categories