Create an array from another array result - php [duplicate] - php

This question already has answers here:
MYSQL SUM GROUP BY
(2 answers)
Closed 7 years ago.
I have 2 kind of classes: math and physics.
In the loop , count(lessonclasses) is equal to 2 then 3.
How can I proceed for my expected output ,
My code is :
$result[]=array();
foreach($lessons as $nameLesson)
{
$lessonclasses=$Board->GetLessonsClass($iduser,$nameLesson);
$tabPrice=$redevance->detailPriceForLesson($nameLesson)
$price=$tabPrice[0];
$frequency=$tabPrice[1];
//echo "nb of class per lesson: ".count($lessonclasses);
for($i=0;$i<count($lessonclasses);$i++)
{
//var_dump($lessonclasses);
$name=$lessonclasses[$i][0];
$amount=$lessonclasses[$i][1];
$timelessonstart=$lessonclasses[$i][2];
$timelessonend=$lessonclasses[$i][3];
$result[$i][]=array($name,$price."€/".$frequency,$timelessonstart,$timelessonend,$amount);
//$arrayOptions[$nbLessons][]=$options; //for keep values
//$nbLessons=$nbLessons+1;
}
}
var_dump($result);
I have an array like :
array(3) {
[0]=> array(2) {
[0]=> array(5) {
[0]=> string(15) "maths"
[1]=> string(11) "10.00"
[2]=> string(10) "2015-06-17"
[3]=> string(1) "-"
[4]=> string(6) "10.00"
}
[1]=> array(5) {
[0]=> string(31) "physique"
[1]=> string(10) "6.00"
[2]=> string(10) "2015-01-01"
[3]=> string(1) "-"
[4]=> string(5) "36.00"
}
}
[1]=> array(2) {
[0]=> array(5) {
[0]=> string(15) "maths"
[1]=> string(11) "20.00"
[2]=> string(10) "2015-06-17"
[3]=> string(1) "-"
[4]=> string(6) "100.0"
}
[1]=> array(5) {
[0]=> string(31) "physique"
[1]=> string(10) "16.00"
[2]=> string(10) "2015-05-01"
[3]=> string(1) "-"
[4]=> string(5) "36.00"
}
}
[2]=> array(1) {
[0]=> array(5) {
[0]=> string(15) "Maths" [1]=> string(11) "11.00" [2]=> string(10) "2015-05-17" [3]=> string(1) "-" [4]=> string(5) "20.00" } } }
My expected output is :
array(3) {
[0]=> array(2) {
[0]=> array(5) {
[0]=> string(15) "maths"
[1]=> string(11) "10.00"
[2]=> string(10) "2015-06-17"
[3]=> string(1) "-"
[4]=> string(6) "10.00"
}
[1]=> array(2) {
[0]=> array(5) {
[0]=> string(15) "maths"
[1]=> string(11) "20.00"
[2]=> string(10) "2015-06-17"
[3]=> string(1) "-"
[4]=> string(6) "100.0"
}
[2]=> array(5) {
[0]=> string(31) "math"
[1]=> string(10) "11.00"
[2]=> string(10) "2015-05-01"
[3]=> string(1) "-"
[4]=> string(5) "36.00"
}
}
[1]=> array(2) {
[0]=> array(5) {
[0]=> string(31) "physique"
[1]=> string(10) "6.00"
[2]=> string(10) "2015-01-01"
[3]=> string(1) "-"
[4]=> string(5) "36.00"
}
[1]=> array(5) {
[0]=> string(31) "physique"
[1]=> string(10) "16.00"
[2]=> string(10) "2015-05-01"
[3]=> string(1) "-"
[4]=> string(5) "36.00"
}
}
} }

SELECT name,sum(qty) FROM tableName group by name;

Simply
SELECT name, SUM(qty) AS total_amount FROM table GROUP BY name

you need to use sum
SELECT name,sum(qty) FROM table_name group by name;

Its working for you..
SELECT name,sum(qty) FROM table_name GROUP BY name;

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 insert data from excel file - Loop error

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?

How to pair those database rows?

so, I have this database, I need to know how many profile.stateX-profile.licenseX have value and also to pair them. I am able to pull them in a array but I can't find the propper php code to get what I need.
Again, what I need to know is this:
1: how many profile.state1/2/3/4/5-profile.license1/2/3/4/5 pairs I have with value
2:output the pairs
This is the array returned form query:
<pre>
array(16) {
[0]=>
array(2) {
[0]=>
string(15) "profile.address"
[1]=>
string(10) "Bld. Indep"
}
[1]=>
array(2) {
[0]=>
string(21) "profile.certification"
[1]=>
string(7) "cert112"
}
[2]=>
array(2) {
[0]=>
string(16) "profile.license1"
[1]=>
string(5) "12345"
}
[3]=>
array(2) {
[0]=>
string(16) "profile.license2"
[1]=>
string(0) ""
}
[4]=>
array(2) {
[0]=>
string(16) "profile.license3"
[1]=>
string(0) ""
}
[5]=>
array(2) {
[0]=>
string(16) "profile.license4"
[1]=>
string(0) ""
}
[6]=>
array(2) {
[0]=>
string(16) "profile.license5"
[1]=>
string(0) ""
}
[7]=>
array(2) {
[0]=>
string(21) "profile.licensenumber"
[1]=>
string(7) "lice112"
}
[8]=>
array(2) {
[0]=>
string(14) "profile.school"
[1]=>
string(5) "nr, 2"
}
[9]=>
array(2) {
[0]=>
string(14) "profile.state1"
[1]=>
string(4) "Ohio"
}
[10]=>
array(2) {
[0]=>
string(14) "profile.state2"
[1]=>
string(0) ""
}
[11]=>
array(2) {
[0]=>
string(14) "profile.state3"
[1]=>
string(0) ""
}
[12]=>
array(2) {
[0]=>
string(14) "profile.state4"
[1]=>
string(0) ""
}
[13]=>
array(2) {
[0]=>
string(14) "profile.state5"
[1]=>
string(0) ""
}
[14]=>
array(2) {
[0]=>
string(18) "profile.user_state"
[1]=>
string(8) "Roumania"
}
[15]=>
array(2) {
[0]=>
string(11) "profile.zip"
[1]=>
string(3) "123"
}
}
</pre>
I hope this makes sense.
Assuming the database is MySQL:
SELECT t1.profile_key, t1.profile_value, t2.profile_key, t2.profile_value
FROM profile AS t1
JOIN profile AS t2 ON t2.profile_key = CONCAT('profile.license', SUBSTR(t1.profile_key, 14))
WHERE t1.profile_key LIKE 'profile.state%' AND t1.profile_value != ''
AND t2.profile_key LIKE 'profile.license%' AND t2.profile_value != ''
SUBSTR(t1.profile_key, 14) gets the number after profile.state in profile_key column. Then we use CONCAT() to append that to profile.license to get the profile_key for the paired row.

Working with multiple array in PHP

I have an array like this :
$optionarray:
array(3) { [0]=> array(3) { [0]=> string(15) "Extension" [1]=> string(1) "1" [2]=> string(2) "10" } [1]=> array(3) { [0]=> string(15) "Extension" [1]=> string(1) "1" [2]=> string(2) "10" } [2]=> array(3) { [0]=> string(19) "Formation" [1]=> string(1) "1" [2]=> string(1) "0" } }
$dataarray:
array(2) { [0]=> array(3) { [0]=> string(23) "Physical room" [1]=> int(1) [2]=> string(7) "12,00 €" } [1]=> array(3) { [0]=> string(26) "user desk" [1]=> int(4) [2]=> string(7) "40,00 €" } }
$fullarray:
array(5) { [0]=> array(3) { [0]=> string(23) "Physical room" [1]=> int(1) [2]=> string(7) "12,00 €" } [1]=> array(3) { [0]=> string(26) "user desk" [1]=> int(4) [2]=> string(7) "40,00 €" } [2]=> array(3) { [0]=> array(3) { [0]=> string(15) "Extension" [1]=> string(1) "1" [2]=> string(2) "10" } [1]=> array(3) { [0]=> string(15) "Extension" [1]=> string(1) "1" [2]=> string(2) "10" } [2]=> array(3) { [0]=> string(19) "Formation " [1]=> string(1) "1" [2]=> string(1) "0" } } [3]=> array(3) { [0]=> array(3) { [0]=> string(15) "Extension" [1]=> string(1) "1" [2]=> string(2) "10" } [1]=> array(3) { [0]=> string(19) "Formation" [1]=> string(1) "1" [2]=> string(1) "0" } [2]=> NULL } [4]=> array(3) { [0]=> array(3) { [0]=> string(19) "Formation " [1]=> string(1) "1" [2]=> string(1) "0" } [1]=> NULL [2]=> NULL } }
My fullarray has not a unique structure , I want just to add $optionarray to $dataarray and iterate on $fullarray
My code :
$data[0] = array('Physical room', $nbprice, $totalprice);
$data[1] = array('user desk', $nbuser, $totaluserprice );
$i=2;
for($j=0;$j<count($optionsdata);$j++){
$data[$i]=array($optionsdata[$j],$optionsdata[$j+1],$optionsdata[$j+2]);
$i=$i+1;
}
How can I proceed for create my $fullarray properly for iterate and display each value ?
Expected output:
array(5){
[0]=>array(3){ [0]=> string(23) "Physical room" [1]=> int(1) [2]=> string(7) "12,00 €" }
[1]=> array(3) { [0]=> string(26) "user desk" [1]=> int(4) [2]=> string(7) "40,00 €" }
[2]=> array(3) { [0]=> array(3) { [0]=> string(15) "Extension" [1]=> string(1) "1" [2]=> string(2) "10" }
[3]=> array(3) { [0]=> string(15) "Extension" [1]=> string(1) "1" [2]=> string(2) "10" }
[4]=> array(3) { [0]=> string(19) "Formation " [1]=> string(1) "1" [2]=> string(1) "0" }
}

Do action when for loop is at it's last index - 1

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.

Categories