I'm trying to get result from a postgres database with the fetchAll() function. But I have to get the same result from an Oracle DB and it doesn't work with the fetchAll() function.
With fetchall() i have an array with index numbers and each array represent a row from the db, but with oci_fetch_all each array have an array with the column name as index, and each array represent a column from the db and that's what I need
Postgres :
$stmt = $db->prepare($req);
$stmt->execute();
$res = $stmt->fetchAll();
output :
array(4) {
[0]=>
array(14) {
["PLU_PRESCRIPTION_S"]=>
string(18) "PLU_PRESCRIPTION_S"
[0]=>
string(18) "PLU_PRESCRIPTION_S"
["Critère"]=>
string(6) "02_PPA"
[1]=>
string(6) "02_PPA"
["type_prescription"]=>
string(6) "risque"
[2]=>
string(6) "risque"
["Aire parcelle (m²)"]=>
string(6) "298.62"
[3]=>
string(6) "298.62"
["Aire de lintersection (m²)"]=>
string(6) "298.62"
[4]=>
string(6) "298.62"
["%"]=>
string(6) "100.00"
[5]=>
string(6) "100.00"
["trig_comm"]=>
NULL
[6]=>
NULL
}
[1]=>
array(14) {
["PLU_PRESCRIPTION_S"]=>
string(18) "PLU_PRESCRIPTION_S"
[0]=>
string(18) "PLU_PRESCRIPTION_S"
["Critère"]=>
string(8) "04_zone1"
[1]=>
string(8) "04_zone1"
["type_prescription"]=>
string(3) "PDU"
[2]=>
string(3) "PDU"
["Aire parcelle (m²)"]=>
string(6) "298.62"
[3]=>
string(6) "298.62"
["Aire de lintersection (m²)"]=>
string(6) "298.62"
[4]=>
string(6) "298.62"
["%"]=>
string(6) "100.00"
[5]=>
string(6) "100.00"
["trig_comm"]=>
NULL
[6]=>
NULL
}
[2]=>
array(14) {
["PLU_PRESCRIPTION_S"]=>
string(18) "PLU_PRESCRIPTION_S"
[0]=>
string(18) "PLU_PRESCRIPTION_S"
["Critère"]=>
string(5) "07_MH"
[1]=>
string(5) "07_MH"
["type_prescription"]=>
string(10) "patrimoine"
[2]=>
string(10) "patrimoine"
["Aire parcelle (m²)"]=>
string(6) "298.62"
[3]=>
string(6) "298.62"
["Aire de lintersection (m²)"]=>
string(6) "298.62"
[4]=>
string(6) "298.62"
["%"]=>
string(6) "100.00"
[5]=>
string(6) "100.00"
["trig_comm"]=>
NULL
[6]=>
NULL
}
[3]=>
array(14) {
["PLU_PRESCRIPTION_S"]=>
string(18) "PLU_PRESCRIPTION_S"
[0]=>
string(18) "PLU_PRESCRIPTION_S"
["Critère"]=>
string(20) "18_OAP_communautaire"
[1]=>
string(20) "18_OAP_communautaire"
["type_prescription"]=>
string(3) "OAP"
[2]=>
string(3) "OAP"
["Aire parcelle (m²)"]=>
string(6) "298.62"
[3]=>
string(6) "298.62"
["Aire de lintersection (m²)"]=>
string(6) "298.62"
[4]=>
string(6) "298.62"
["%"]=>
string(6) "100.00"
[5]=>
string(6) "100.00"
["trig_comm"]=>
string(3) "CEN"
[6]=>
string(3) "CEN"
}
}
ORACLE :
if (oci_execute($ora_req)) {
// récupération des infos dans la base de donnée
$nrows = oci_fetch_all($ora_req, $res);
// Stockage des infos dans le tableau
array_push($arrPrescriptionS, $res);
}
OUTPUT :
array(0) { }
array(7) {
["'PLU_PRESCRIPTION_S'"]=>
array(4) {
[0]=>
string(18) "PLU_PRESCRIPTION_S"
[1]=>
string(18) "PLU_PRESCRIPTION_S"
[2]=>
string(18) "PLU_PRESCRIPTION_S"
[3]=>
string(18) "PLU_PRESCRIPTION_S"
}
["Critère"]=>
array(4) {
[0]=>
string(6) "02_PPA"
[1]=>
string(8) "04_zone1"
[2]=>
string(5) "07_MH"
[3]=>
string(20) "18_OAP_communautaire"
}
["TYPE_PRESCRIPTION"]=>
array(4) {
[0]=>
string(2) "02"
[1]=>
string(2) "04"
[2]=>
string(2) "07"
[3]=>
string(2) "18"
}
["Aire parcelle (m²)"]=>
array(4) {
[0]=>
string(6) "298,62"
[1]=>
string(6) "298,62"
[2]=>
string(6) "298,62"
[3]=>
string(6) "298,62"
}
["Aire de l'intersection (m²)"]=>
array(4) {
[0]=>
string(6) "298,62"
[1]=>
string(6) "298,62"
[2]=>
string(6) "298,62"
[3]=>
string(6) "298,62"
}
["%"]=>
array(4) {
[0]=>
string(3) "100"
[1]=>
string(3) "100"
[2]=>
string(3) "100"
[3]=>
string(3) "100"
}
["TRIG_COMM"]=>
array(4) {
[0]=>
NULL
[1]=>
NULL
[2]=>
NULL
[3]=>
string(3) "CEN"
}
}
thank's
This is not possible in PDO. PDO doesn't return results indexed by column names. This behaviour is not very intuitive when writing code, so it's best avoided. Use the normal fetch modes, which order results by rows.
If you need to write some shim for backwards compatibility you can do it yourself with nested loops.
$s = $pdo->query("SELECT * FROM bar");
$allByColumns = [];
foreach ($s->fetchAll(PDO::FETCH_ASSOC) as $index => $row) {
foreach ($row as $column => $value) {
$allByColumns[$column][$index] = $value;
}
}
var_dump($allByColumns);
Related
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.
while developing Laravel app I stuck on one problem with array sorting. At the beginning, I used partition problem to split my array into equal chunks by time, but now I have a problem sorting by the id of the array.
My array:
array(2) {
[0]=>
array(3) {
[0]=>
array(8) {
[0]=>
int(4) <----- this is value i need to sort by
[1]=>
int(15)
[2]=>
string(64) "text..."
[3]=>
string(6) "650008"
[4]=>
string(13) "NP08KDT"
[5]=>
int(48)
[6]=>
string(6) "456467"
[7]=>
int(33)
}
[1]=>
array(8) {
[0]=>
int(6)
[1]=>
int(10)
[2]=>
string(64) "text..."
[3]=>
string(6) "650008"
[4]=>
string(13) "NP08KDT"
[5]=>
int(48)
[6]=>
string(6) "456467"
[7]=>
int(33)
}
[2]=>
array(8) {
[0]=>
int(2)
[1]=>
int(8)
[2]=>
string(85) "text..."
[3]=>
string(6) "650008"
[4]=>
string(13) "NP08KDT"
[5]=>
int(48)
[6]=>
string(6) "456467"
[7]=>
int(33)
}
}
[1]=>
array(3) {
[0]=>
array(8) {
[0]=>
int(3)
[1]=>
int(14)
[2]=>
string(158) "text..."
[3]=>
string(6) "650008"
[4]=>
string(13) "NP08KDT"
[5]=>
int(48)
[6]=>
string(6) "456467"
[7]=>
int(33)
}
[1]=>
array(8) {
[0]=>
int(5)
[1]=>
int(12)
[2]=>
string(158) "text..."
[3]=>
string(6) "650008"
[4]=>
string(13) "NP08KDT"
[5]=>
int(48)
[6]=>
string(6) "456467"
[7]=>
int(33)
}
[2]=>
array(8) {
[0]=>
int(1)
[1]=>
int(7)
[2]=>
string(55) "text..."
[3]=>
string(6) "650008"
[4]=>
string(13) "NP08KDT"
[5]=>
int(48)
[6]=>
string(6) "456467"
[7]=>
int(33)
}
}
}
I need to sort by the first value of each array, so called my id.
Output I need:
array(2) {
[0]=>
array(3) {
[0]=>
array(8) {
[0]=>
int(1)
[1]=>
int(15)
[2]=>
string(64) "text..."
[3]=>
string(6) "650008"
[4]=>
string(13) "NP08KDT"
[5]=>
int(48)
[6]=>
string(6) "456467"
[7]=>
int(33)
}
[1]=>
array(8) {
[0]=>
int(2)
[1]=>
int(10)
[2]=>
string(64) "text..."
[3]=>
string(6) "650008"
[4]=>
string(13) "NP08KDT"
[5]=>
int(48)
[6]=>
string(6) "456467"
[7]=>
int(33)
}
[2]=>
array(8) {
[0]=>
int(3)
[1]=>
int(8)
[2]=>
string(85) "text..."
[3]=>
string(6) "650008"
[4]=>
string(13) "NP08KDT"
[5]=>
int(48)
[6]=>
string(6) "456467"
[7]=>
int(33)
}
}
[1]=>
array(3) {
[0]=>
array(8) {
[0]=>
int(4)
[1]=>
int(14)
[2]=>
string(158) "text..."
[3]=>
string(6) "650008"
[4]=>
string(13) "NP08KDT"
[5]=>
int(48)
[6]=>
string(6) "456467"
[7]=>
int(33)
}
[1]=>
array(8) {
[0]=>
int(5)
[1]=>
int(12)
[2]=>
string(158) "text..."
[3]=>
string(6) "650008"
[4]=>
string(13) "NP08KDT"
[5]=>
int(48)
[6]=>
string(6) "456467"
[7]=>
int(33)
}
[2]=>
array(8) {
[0]=>
int(6)
[1]=>
int(7)
[2]=>
string(55) "text..."
[3]=>
string(6) "650008"
[4]=>
string(13) "NP08KDT"
[5]=>
int(48)
[6]=>
string(6) "456467"
[7]=>
int(33)
}
}
}
maybe any of you guys have any solutions how i could solve this problem, so far i tried a lot of sorting functions, none of them seems to work properly for me.
Thank you!
this may not be the best way to do this, but you can try something like this.
$tempArray = array();
$sortedArray = array();
foreach ($yourArray as $key1 => $arr){// create 2D array
foreach ($arr as $key2 => $arr2){
$tempArray[] = $arr2;
}
}
foreach ($tempArray as $keyTemp => $tempElem){
$sortingArray[] = $tempElem[0];//get array value you need to sort
}
asort($sortingArray);// sort array maintaining the key
foreach ($sortingArray as $keySort => $sortElem){//loop through sorted array to push elements to a new array
$sortedArray[] = $tempArray[$keySort];
}
print_r($sortedArray);
I'm trying to get the data that I uploaded from excel using foreach and for loop, but the weird thing is when I upload excel file w/ 12 rows it successfully inserted, but when I upload excel w/ 13 rows and up its only get the last row and I'm getting the error Uninitialized Offset :8.
this is the array when I uploaded excel w/ 12 rows.
array(12)
{
[0]=> array(1) { [0]=> array(9) { [0]=> string(8) "CC-00057" [1]=> float(123) [2]=> string(5) "apple" [3]=> float(45) [4]=> string(2) "pc" [5]=> string(5) "A1011" [6]=> float(1101) [7]=> float(42110) [8]=> string(3) "asd" } }
[1]=> array(1) { [0]=> array(9) { [0]=> string(8) "CC-00057" [1]=> float(124) [2]=> string(6) "grapes" [3]=> float(2) [4]=> string(3) "box" [5]=> string(5) "A1012" [6]=> float(1101) [7]=> float(42111) [8]=> string(3) "asd" } }
[2]=> array(1) { [0]=> array(9) { [0]=> string(8) "CC-00057" [1]=> float(124) [2]=> string(6) "grapes" [3]=> float(20) [4]=> string(3) "box" [5]=> string(5) "A1016" [6]=> float(1101) [7]=> float(42111) [8]=> string(3) "asd" } }
[3]=> array(1) { [0]=> array(9) { [0]=> string(8) "CC-00057" [1]=> float(124) [2]=> string(5) "chico" [3]=> float(7) [4]=> string(3) "box" [5]=> string(5) "A1012" [6]=> float(1101) [7]=> float(42111) [8]=> string(3) "ads" } }
[4]=> array(1) { [0]=> array(9) { [0]=> string(8) "CC-00057" [1]=> float(125) [2]=> string(9) "pineapple" [3]=> float(8) [4]=> string(3) "box" [5]=> string(5) "A1013" [6]=> float(1102) [7]=> float(42112) [8]=> string(3) "ads" } }
[5]=> array(1) { [0]=> array(9) { [0]=> string(8) "CC-00057" [1]=> float(123) [2]=> string(5) "apple" [3]=> float(45) [4]=> string(2) "pc" [5]=> string(5) "A1011" [6]=> float(1101) [7]=> float(42110) [8]=> string(3) "asd" } }
[6]=> array(1) { [0]=> array(9) { [0]=> string(8) "CC-00057" [1]=> float(124) [2]=> string(6) "grapes" [3]=> float(2) [4]=> string(3) "box" [5]=> string(5) "A1012" [6]=> float(1101) [7]=> float(42111) [8]=> string(3) "asd" } }
[7]=> array(1) { [0]=> array(9) { [0]=> string(8) "CC-00057" [1]=> float(124) [2]=> string(6) "grapes" [3]=> float(20) [4]=> string(3) "box" [5]=> string(5) "A1016" [6]=> float(1101) [7]=> float(42111) [8]=> string(3) "asd" } }
[8]=> array(1) { [0]=> array(9) { [0]=> string(8) "CC-00057" [1]=> float(124) [2]=> string(5) "chico" [3]=> float(7) [4]=> string(3) "box" [5]=> string(5) "A1012" [6]=> float(1101) [7]=> float(42111) [8]=> string(3) "ads" } }
[9]=> array(1) { [0]=> array(9) { [0]=> string(8) "CC-00057" [1]=> float(125) [2]=> string(9) "pineapple" [3]=> float(8) [4]=> string(3) "box" [5]=> string(5) "A1013" [6]=> float(1102) [7]=> float(42112) [8]=> string(3) "ads" } }
[10]=> array(1) { [0]=> array(9) { [0]=> string(8) "CC-00057" [1]=> float(123) [2]=> string(5) "apple" [3]=> float(45) [4]=> string(2) "pc" [5]=> string(5) "A1011" [6]=> float(1101) [7]=> float(42110) [8]=> string(3) "asd" } }
[11]=> array(1) { [0]=> array(9) { [0]=> string(8) "CC-00057" [1]=> float(124) [2]=> string(6) "grapes" [3]=> float(2) [4]=> string(3) "box" [5]=> string(5) "A1012" [6]=> float(1101) [7]=> float(42111) [8]=> string(4) "a1a1" } } }
this is when 13 rows it only get the last row.
array(1)
{
[0]=> array(9) { [0]=> string(8) "CC-00057" [1]=> float(124) [2]=> string(6) "grapes" [3]=> float(2) [4]=> string(3) "box" [5]=> string(5) "A1012" [6]=> float(1101) [7]=> float(42111) [8]=> string(4) "a1a1" } }
below is my code.
var_dump ($dataRow);
if (isset($dataRow)) {
echo '<pre>';
foreach($dataRow as $key => $dataVal) {
foreach($dataVal as $key => $dataSume) {
$fArray = $dataSume;
for($x=0; $x < 1; $x++ ){
$refs = strlen($fArray[0]);
$this->varOrder->addCount($fArray[0],$fArray[1],$fArray[2],$fArray[3],$fArray[4],$fArray[5],$fArray[6],$fArray[7],$fArray[8],$flag2);
}
}
}
echo '</pre>';
}
$data['mode'] = 'Empty';
$data['message'] = 'Data Successfully Uploaded!';
$this->session->unset_userdata($dataRow);
$this->session->unset_userdata('Count');
$this->load->view('home_view', $data);
My question is why I'm only get the last row when I'm start inserting 13 rows and up, is this on my foreach or for loop syntax?
I'm trying to upload excel file get the value and save it to database, the weird thing is when my excel file has 13 rows and up then the error Uninitialized Offset is occurred.
if (isset($dataRow)) {
foreach($dataRow as $key => $dataVal) :
foreach($dataVal as $key => $dataSume)
$fArray = $dataSume;
$y = count ($dataSume);
for($x=0; $x < 1; $x++ ){
if ( isset($fArray) ){
$this->varOrder->addCount($fArray[0],$fArray[1],$fArray[2],$fArray[3],$fArray[4],$fArray[5],$fArray[6],$fArray[7],$fArray[8],$flag2);
}
}
endforeach;
this is the value of $dataRow array.
array(12) { [0]=> array(1) { [0]=> array(9) { [0]=> string(8) "CC-00057" [1]=> float(123) [2]=> string(5) "apple" [3]=> float(45) [4]=> string(2) "pc" [5]=> string(5) "A1011" [6]=> float(1101) [7]=> float(42110) [8]=> string(3) "asd" } } [1]=> array(1) { [0]=> array(9) { [0]=> string(8) "CC-00057" [1]=> float(124) [2]=> string(6) "grapes" [3]=> float(2) [4]=> string(3) "box" [5]=> string(5) "A1012" [6]=> float(1101) [7]=> float(42111) [8]=> string(3) "asd" } } [2]=> array(1) { [0]=> array(9) { [0]=> string(8) "CC-00057" [1]=> float(124) [2]=> string(6) "grapes" [3]=> float(20) [4]=> string(3) "box" [5]=> string(5) "A1016" [6]=> float(1101) [7]=> float(42111) [8]=> string(3) "asd" } } [3]=> array(1) { [0]=> array(9) { [0]=> string(8) "CC-00057" [1]=> float(124) [2]=> string(5) "chico" [3]=> float(7) [4]=> string(3) "box" [5]=> string(5) "A1012" [6]=> float(1101) [7]=> float(42111) [8]=> string(3) "ads" } } [4]=> array(1) { [0]=> array(9) { [0]=> string(8) "CC-00057" [1]=> float(125) [2]=> string(9) "pineapple" [3]=> float(8) [4]=> string(3) "box" [5]=> string(5) "A1013" [6]=> float(1102) [7]=> float(42112) [8]=> string(3) "ads" } } [5]=> array(1) { [0]=> array(9) { [0]=> string(8) "CC-00057" [1]=> float(123) [2]=> string(5) "apple" [3]=> float(45) [4]=> string(2) "pc" [5]=> string(5) "A1011" [6]=> float(1101) [7]=> float(42110) [8]=> string(3) "asd" } } [6]=> array(1) { [0]=> array(9) { [0]=> string(8) "CC-00057" [1]=> float(124) [2]=> string(6) "grapes" [3]=> float(2) [4]=> string(3) "box" [5]=> string(5) "A1012" [6]=> float(1101) [7]=> float(42111) [8]=> string(3) "asd" } } [7]=> array(1) { [0]=> array(9) { [0]=> string(8) "CC-00057" [1]=> float(124) [2]=> string(6) "grapes" [3]=> float(20) [4]=> string(3) "box" [5]=> string(5) "A1016" [6]=> float(1101) [7]=> float(42111) [8]=> string(3) "asd" } } [8]=> array(1) { [0]=> array(9) { [0]=> string(8) "CC-00057" [1]=> float(124) [2]=> string(5) "chico" [3]=> float(7) [4]=> string(3) "box" [5]=> string(5) "A1012" [6]=> float(1101) [7]=> float(42111) [8]=> string(3) "ads" } } [9]=> array(1) { [0]=> array(9) { [0]=> string(8) "CC-00057" [1]=> float(125) [2]=> string(9) "pineapple" [3]=> float(8) [4]=> string(3) "box" [5]=> string(5) "A1013" [6]=> float(1102) [7]=> float(42112) [8]=> string(3) "ads" } } [10]=> array(1) { [0]=> array(9) { [0]=> string(8) "CC-00057" [1]=> float(123) [2]=> string(5) "apple" [3]=> float(45) [4]=> string(2) "pc" [5]=> string(5) "A1011" [6]=> float(1101) [7]=> float(42110) [8]=> string(3) "asd" } } [11]=> array(1) { [0]=> array(9) { [0]=> string(8) "CC-00057" [1]=> float(124) [2]=> string(6) "grapes" [3]=> float(2) [4]=> string(3) "box" [5]=> string(5) "A1012" [6]=> float(1101) [7]=> float(42111) [8]=> string(3) "asd" } } }
this is the only value that system get when the number of rows is 13.
array(1) { [0]=> array(9) { [0]=> string(8) "CC-00057" [1]=> float(124) [2]=> string(6) "grapes" [3]=> float(2) [4]=> string(3) "box" [5]=> string(5) "A1012" [6]=> float(1101) [7]=> float(42111) [8]=> string(4) "a1a1" } }
I tried to add if ( isset($fArray) ) before the for loop to catch the offset, but still didn't work.
is this problem is on my foreach or for loop?
thanks in advance!
I have an function which retrieves specific data from some other functions.
I loop through the retrieved data and explode every result by the # character, the data looks like this:
Not exploded
string(14) "1#28#92#28#103"
string(17) "551#36#112#36#126"
string(17) "651#45#132#45#150"
string(17) "751#59#159#59#179"
string(17) "851#77#194#91#208"
string(18) "951#99#229#129#245"
string(20) "1051#122#264#166#282"
string(20) "1151#145#300#203#318"
string(20) "1251#168#335#240#355"
string(20) "1351#190#371#278#392"
string(20) "1451#213#406#315#428"
string(20) "1551#236#441#352#465"
string(20) "1651#258#477#389#501"
string(20) "1751#281#512#427#538"
string(20) "1851#304#548#464#575"
string(20) "1951#327#583#501#611"
string(20) "2051#349#618#539#648"
string(20) "2151#372#654#576#685"
string(20) "2251#395#689#613#721"
string(20) "2351#417#725#650#758"
string(20) "2451#440#760#688#795"
string(20) "2551#463#795#725#831"
string(20) "2651#486#831#762#868"
string(20) "2751#508#866#799#905"
string(20) "2851#531#902#837#941"
string(20) "2951#554#937#874#978"
string(21) "3051#576#972#911#1015"
string(22) "3151#599#1008#949#1051"
string(22) "3251#618#1040#982#1084"
string(23) "3351#637#1071#1015#1117"
string(23) "3451#655#1102#1049#1149"
string(23) "3551#674#1133#1082#1182"
string(23) "3651#693#1165#1115#1214"
string(23) "3751#711#1196#1148#1247"
string(23) "3851#730#1227#1181#1279"
string(23) "3951#748#1258#1214#1312"
string(23) "4051#767#1290#1247#1344"
string(23) "4151#785#1321#1280#1377"
string(23) "4251#804#1352#1314#1410"
string(23) "4351#823#1383#1347#1442"
string(23) "4451#841#1415#1380#1475"
string(23) "4551#860#1446#1413#1507"
string(23) "4651#878#1477#1446#1540"
string(23) "4751#897#1508#1479#1572"
string(23) "4851#915#1540#1512#1605"
string(23) "4951#934#1571#1546#1637"
Exploded
array(5) {
[0]=>
string(1) "1"
[1]=>
string(2) "28"
[2]=>
string(2) "92"
[3]=>
string(2) "28"
[4]=>
string(3) "103"
}
array(5) {
[0]=>
string(3) "551"
[1]=>
string(2) "36"
[2]=>
string(3) "112"
[3]=>
string(2) "36"
[4]=>
string(3) "126"
}
array(5) {
[0]=>
string(3) "651"
[1]=>
string(2) "45"
[2]=>
string(3) "132"
[3]=>
string(2) "45"
[4]=>
string(3) "150"
}
array(5) {
[0]=>
string(3) "751"
[1]=>
string(2) "59"
[2]=>
string(3) "159"
[3]=>
string(2) "59"
[4]=>
string(3) "179"
}
array(5) {
[0]=>
string(3) "851"
[1]=>
string(2) "77"
[2]=>
string(3) "194"
[3]=>
string(2) "91"
[4]=>
string(3) "208"
}
array(5) {
[0]=>
string(3) "951"
[1]=>
string(2) "99"
[2]=>
string(3) "229"
[3]=>
string(3) "129"
[4]=>
string(3) "245"
}
array(5) {
[0]=>
string(4) "1051"
[1]=>
string(3) "122"
[2]=>
string(3) "264"
[3]=>
string(3) "166"
[4]=>
string(3) "282"
}
array(5) {
[0]=>
string(4) "1151"
[1]=>
string(3) "145"
[2]=>
string(3) "300"
[3]=>
string(3) "203"
[4]=>
string(3) "318"
}
array(5) {
[0]=>
string(4) "1251"
[1]=>
string(3) "168"
[2]=>
string(3) "335"
[3]=>
string(3) "240"
[4]=>
string(3) "355"
}
array(5) {
[0]=>
string(4) "1351"
[1]=>
string(3) "190"
[2]=>
string(3) "371"
[3]=>
string(3) "278"
[4]=>
string(3) "392"
}
array(5) {
[0]=>
string(4) "1451"
[1]=>
string(3) "213"
[2]=>
string(3) "406"
[3]=>
string(3) "315"
[4]=>
string(3) "428"
}
array(5) {
[0]=>
string(4) "1551"
[1]=>
string(3) "236"
[2]=>
string(3) "441"
[3]=>
string(3) "352"
[4]=>
string(3) "465"
}
array(5) {
[0]=>
string(4) "1651"
[1]=>
string(3) "258"
[2]=>
string(3) "477"
[3]=>
string(3) "389"
[4]=>
string(3) "501"
}
array(5) {
[0]=>
string(4) "1751"
[1]=>
string(3) "281"
[2]=>
string(3) "512"
[3]=>
string(3) "427"
[4]=>
string(3) "538"
}
array(5) {
[0]=>
string(4) "1851"
[1]=>
string(3) "304"
[2]=>
string(3) "548"
[3]=>
string(3) "464"
[4]=>
string(3) "575"
}
array(5) {
[0]=>
string(4) "1951"
[1]=>
string(3) "327"
[2]=>
string(3) "583"
[3]=>
string(3) "501"
[4]=>
string(3) "611"
}
array(5) {
[0]=>
string(4) "2051"
[1]=>
string(3) "349"
[2]=>
string(3) "618"
[3]=>
string(3) "539"
[4]=>
string(3) "648"
}
array(5) {
[0]=>
string(4) "2151"
[1]=>
string(3) "372"
[2]=>
string(3) "654"
[3]=>
string(3) "576"
[4]=>
string(3) "685"
}
array(5) {
[0]=>
string(4) "2251"
[1]=>
string(3) "395"
[2]=>
string(3) "689"
[3]=>
string(3) "613"
[4]=>
string(3) "721"
}
array(5) {
[0]=>
string(4) "2351"
[1]=>
string(3) "417"
[2]=>
string(3) "725"
[3]=>
string(3) "650"
[4]=>
string(3) "758"
}
array(5) {
[0]=>
string(4) "2451"
[1]=>
string(3) "440"
[2]=>
string(3) "760"
[3]=>
string(3) "688"
[4]=>
string(3) "795"
}
array(5) {
[0]=>
string(4) "2551"
[1]=>
string(3) "463"
[2]=>
string(3) "795"
[3]=>
string(3) "725"
[4]=>
string(3) "831"
}
array(5) {
[0]=>
string(4) "2651"
[1]=>
string(3) "486"
[2]=>
string(3) "831"
[3]=>
string(3) "762"
[4]=>
string(3) "868"
}
array(5) {
[0]=>
string(4) "2751"
[1]=>
string(3) "508"
[2]=>
string(3) "866"
[3]=>
string(3) "799"
[4]=>
string(3) "905"
}
array(5) {
[0]=>
string(4) "2851"
[1]=>
string(3) "531"
[2]=>
string(3) "902"
[3]=>
string(3) "837"
[4]=>
string(3) "941"
}
array(5) {
[0]=>
string(4) "2951"
[1]=>
string(3) "554"
[2]=>
string(3) "937"
[3]=>
string(3) "874"
[4]=>
string(3) "978"
}
array(5) {
[0]=>
string(4) "3051"
[1]=>
string(3) "576"
[2]=>
string(3) "972"
[3]=>
string(3) "911"
[4]=>
string(4) "1015"
}
array(5) {
[0]=>
string(4) "3151"
[1]=>
string(3) "599"
[2]=>
string(4) "1008"
[3]=>
string(3) "949"
[4]=>
string(4) "1051"
}
array(5) {
[0]=>
string(4) "3251"
[1]=>
string(3) "618"
[2]=>
string(4) "1040"
[3]=>
string(3) "982"
[4]=>
string(4) "1084"
}
array(5) {
[0]=>
string(4) "3351"
[1]=>
string(3) "637"
[2]=>
string(4) "1071"
[3]=>
string(4) "1015"
[4]=>
string(4) "1117"
}
array(5) {
[0]=>
string(4) "3451"
[1]=>
string(3) "655"
[2]=>
string(4) "1102"
[3]=>
string(4) "1049"
[4]=>
string(4) "1149"
}
array(5) {
[0]=>
string(4) "3551"
[1]=>
string(3) "674"
[2]=>
string(4) "1133"
[3]=>
string(4) "1082"
[4]=>
string(4) "1182"
}
array(5) {
[0]=>
string(4) "3651"
[1]=>
string(3) "693"
[2]=>
string(4) "1165"
[3]=>
string(4) "1115"
[4]=>
string(4) "1214"
}
array(5) {
[0]=>
string(4) "3751"
[1]=>
string(3) "711"
[2]=>
string(4) "1196"
[3]=>
string(4) "1148"
[4]=>
string(4) "1247"
}
array(5) {
[0]=>
string(4) "3851"
[1]=>
string(3) "730"
[2]=>
string(4) "1227"
[3]=>
string(4) "1181"
[4]=>
string(4) "1279"
}
array(5) {
[0]=>
string(4) "3951"
[1]=>
string(3) "748"
[2]=>
string(4) "1258"
[3]=>
string(4) "1214"
[4]=>
string(4) "1312"
}
array(5) {
[0]=>
string(4) "4051"
[1]=>
string(3) "767"
[2]=>
string(4) "1290"
[3]=>
string(4) "1247"
[4]=>
string(4) "1344"
}
array(5) {
[0]=>
string(4) "4151"
[1]=>
string(3) "785"
[2]=>
string(4) "1321"
[3]=>
string(4) "1280"
[4]=>
string(4) "1377"
}
array(5) {
[0]=>
string(4) "4251"
[1]=>
string(3) "804"
[2]=>
string(4) "1352"
[3]=>
string(4) "1314"
[4]=>
string(4) "1410"
}
array(5) {
[0]=>
string(4) "4351"
[1]=>
string(3) "823"
[2]=>
string(4) "1383"
[3]=>
string(4) "1347"
[4]=>
string(4) "1442"
}
array(5) {
[0]=>
string(4) "4451"
[1]=>
string(3) "841"
[2]=>
string(4) "1415"
[3]=>
string(4) "1380"
[4]=>
string(4) "1475"
}
array(5) {
[0]=>
string(4) "4551"
[1]=>
string(3) "860"
[2]=>
string(4) "1446"
[3]=>
string(4) "1413"
[4]=>
string(4) "1507"
}
array(5) {
[0]=>
string(4) "4651"
[1]=>
string(3) "878"
[2]=>
string(4) "1477"
[3]=>
string(4) "1446"
[4]=>
string(4) "1540"
}
array(5) {
[0]=>
string(4) "4751"
[1]=>
string(3) "897"
[2]=>
string(4) "1508"
[3]=>
string(4) "1479"
[4]=>
string(4) "1572"
}
array(5) {
[0]=>
string(4) "4851"
[1]=>
string(3) "915"
[2]=>
string(4) "1540"
[3]=>
string(4) "1512"
[4]=>
string(4) "1605"
}
array(5) {
[0]=>
string(4) "4951"
[1]=>
string(3) "934"
[2]=>
string(4) "1571"
[3]=>
string(4) "1546"
[4]=>
string(4) "1637"
}
I want it so whenever the last index - 1 is being handled by the for loop, the code should do a specific action, this is my own try (function) for it:
public function getDataByVehicle(VehicleType $vehicleType, VehicleOwner $vehicleOwner) {
$data = $vehicleType->resolveVehicleTypeWithData($vehicleType);
$dataKey = $vehicleType->resolveDataKey($data, $vehicleOwner);
$dataLocation = $this->data->$dataKey;
if (is_a($vehicleType, "PassengerCar")) {
for ($dataLocationIndex = 0; $dataLocationIndex < count($dataLocation); $dataLocationIndex++) {
$formattedData = explode("#", $dataLocation[$dataLocationIndex]);
var_dump($formattedData);
//My own try!
if (!$dataLocationIndex == count($dataLocation)) {
// do action
}
}
}
return null;
}
Question
How do I make my code do an specific action when the for loop is at its last index - 1?
Note
I want to use an for loop because i want to be able to check what the next index of the array would be, if it's not exists, take the last one it remembered.
The for-loop is only running while: $dataLocationIndex < count($dataLocation), which means you should check the count($dataLocation) minus 1, because the loop never reaches the count of $dataLocation.
//My own try!
if ($dataLocationIndex === count($dataLocation)-1) {
// do action
}
Hope it worked.