I have a newbie question here, but I'm really stucked. I have a multidimensional array that comes from a SELECT from a database, what I need is pretty simple, I'm doing a graphic where contains the number of results of each column of every user specified.
Heres some code to clarify your minds:
try {
$pdo = new PDO('mysql:host=***; dbname=**;charset=utf8;', '***', '**');
}catch(PDOException $err){
echo "Erro:\n".$err->getMessage();
}
$sql = "SELECT * FROM results
WHERE ra like '****'";
$statement = $pdo->prepare($sql);
$statement->execute();
$result = $statement->fetchAll();
$resultPgto = array_column($result, 'indicado_pgto');
print_r($resultadoPgto);
This returns me this array:
array(3) {
[0]=>
string(18) "Indicacao Invalida"
[1]=>
string(3) "Nao"
[2]=>
string(3) "Nao"
}
I need to count the results that are equal to Sim AND it should returns me 0 but instead when I do something like print_r($resultadoPgto == 'Sim'); it returns '1'...
Other funny fact is, when I try to return results that are equal to Nao (theres 2 there) it returns me 0 or 1...
What I'm missing here? Can someone please help?
EDIT 1 :
Counting the result itself will return the number of keys the array has.
Here is the full Result to your understanding.
array(3) {
[0]=>
array(22) {
["dsadsad"]=>
string(7) "***"
[0]=>
string(7) "****"
["nome_aluno"]=>
string(25) "lore"
[1]=>
string(25) "ipsum"
["nome_indicado"]=>
string(25) "lore"
[2]=>
string(25) "ipsum"
["indicado_inscrito"]=>
string(18) "Indica��o Inv�lida"
[3]=>
string(18) "Indica��o Inv�lida"
["indicado_confirmado"]=>
string(18) "Indica��o Inv�lida"
[4]=>
string(18) "Indica��o Inv�lida"
["indicado_presente"]=>
string(18) "Indica��o Inv�lida"
[5]=>
string(18) "Indica��o Inv�lida"
["indicado_aprovado"]=>
string(18) "Indica��o Inv�lida"
[6]=>
string(18) "Indica��o Inv�lida"
["indicado_matriculado"]=>
string(18) "Indica��o Inv�lida"
[7]=>
string(18) "Indica��o Inv�lida"
["indicado_pgto"]=>
string(18) "Indicacao Invalida"
[8]=>
string(18) "Indicacao Invalida"
["validacao_indicacao"]=>
string(3) "N�o"
[9]=>
string(3) "N�o"
["validacao_desconto"]=>
string(3) "N�o"
[10]=>
string(3) "N�o"
}
[1]=>
array(22) {
["dsdsdasda"]=>
string(7) "***"
[0]=>
string(7) "***"
["nome_aluno"]=>
string(25) "lore"
[1]=>
string(25) "ipsum"
["nome_indicado"]=>
string(23) "lore"
[2]=>
string(23) "ipsum"
["indicado_inscrito"]=>
string(3) "Sim"
[3]=>
string(3) "Sim"
["indicado_confirmado"]=>
string(3) "N�o"
[4]=>
string(3) "N�o"
["indicado_presente"]=>
string(9) "EM ABERTO"
[5]=>
string(9) "EM ABERTO"
["indicado_aprovado"]=>
string(9) "EM ABERTO"
[6]=>
string(9) "EM ABERTO"
["indicado_matriculado"]=>
string(3) "N�o"
[7]=>
string(3) "N�o"
["indicado_pgto"]=>
string(3) "Nao"
[8]=>
string(3) "Nao"
["validacao_indicacao"]=>
string(3) "Sim"
[9]=>
string(3) "Sim"
["validacao_desconto"]=>
string(3) "N�o"
[10]=>
string(3) "N�o"
}
[2]=>
array(22) {
["asdasdsad"]=>
string(7) "***"
[0]=>
string(7) "***"
["nome_aluno"]=>
string(25) "lore"
[1]=>
string(25) "ipsum"
["nome_indicado"]=>
string(15) "lore"
[2]=>
string(15) "ipsum"
["indicado_inscrito"]=>
string(3) "Sim"
[3]=>
string(3) "Sim"
["indicado_confirmado"]=>
string(3) "N�o"
[4]=>
string(3) "N�o"
["indicado_presente"]=>
string(9) "EM ABERTO"
[5]=>
string(9) "EM ABERTO"
["indicado_aprovado"]=>
string(9) "EM ABERTO"
[6]=>
string(9) "EM ABERTO"
["indicado_matriculado"]=>
string(3) "N�o"
[7]=>
string(3) "N�o"
["indicado_pgto"]=>
string(3) "Nao"
[8]=>
string(3) "Nao"
["validacao_indicacao"]=>
string(3) "Sim"
[9]=>
string(3) "Sim"
["validacao_desconto"]=>
string(3) "N�o"
[10]=>
string(3) "N�o"
}
}
The only field I need information is "indicado_pgto", thats why I did the array_column()
What I really need is compare each value and count them, to return me any position that have the string 'Sim' on it, and even if don't have any 'Sim' string on it, it returns me 1
If you have this array after using array_column() on the SQL results, then the assumption is that your target array looks like this:
array(3) {
[0]=>
string(18) "Indicacao Invalida"
[1]=>
string(3) "Nao"
[2]=>
string(3) "Nao"
}
If so, then you should be able to do something simple like this?
<?php
$counter = 0;
foreach ($resultadoPgto as $result) {
if ($result === 'Nao') {
$counter++;
}
}
print_r($counter);
Note that I used the triple equality operator === instead of the double equality operator==.
The weird behaviour you are experiencing with your print_r($resultadoPgto == 'Sim') returning '1' or '0' could be an side effect of using the double equality operator since it does not take the data type in consideration when evaluating the expression.
This will count the occurances of any field in an array that match a specified value.
function count_occ($array, $field, $content) {
$cnt = 0;
foreach( $array as $a) {
if ($a[$field] = $content) { $cnt++; }
}
return $cnt;
}
echo count_occ($result, 'indicado_pgto', 'Sim');
Related
i returned a array off all my admin submenu items. but this returns like this:
array(2) { [0]=> string(4) "read" [10]=> string(11) "update_core" } array(0) { } array(4) { [5]=> string(10) "edit_posts" [10]=> string(10) "edit_posts" [15]=> string(17) "manage_categories" [16]=> string(16) "manage_post_tags" } array(3) { [5]=> string(12) "upload_files" [10]=> string(12) "upload_files" [15]=> string(17) "manage_categories" } array(2) { [5]=> string(10) "edit_pages" [10]=> string(10) "edit_pages" } array(0) { } array(4) { [5]=> string(10) "edit_posts" [10]=> string(10) "edit_posts" [15]=> string(17) "manage_categories" [16]=> string(17) "manage_categories" } array(3) { [5]=> string(10) "edit_posts" [10]=> string(10) "edit_posts" [15]=> string(17) "manage_categories" } array(0) { } array(8) { [0]=> string(18) "edit_theme_options" [1]=> string(19) "edit_us_page_blocks" [2]=> string(19) "edit_us_page_blocks" [3]=> string(19) "edit_us_page_blocks" [4]=> string(19) "edit_us_page_blocks" [5]=> string(14) "manage_options" [6]=> string(14) "manage_options" [7]=> string(14) "manage_options" } array(4) { [5]=> string(13) "switch_themes" [6]=> string(9) "customize" [7]=> string(18) "edit_theme_options" [10]=> string(18) "edit_theme_options" } array(2) { [5]=> string(16) "activate_plugins" [10]=> string(15) "install_plugins" } array(3) { [5]=> string(10) "list_users" [10]=> string(12) "create_users" [15]=> string(4) "read" } array(7) { [5]=> string(10) "edit_posts" [10]=> string(6) "import" [15]=> string(6) "export" [20]=> string(23) "view_site_health_checks" [25]=> string(27) "export_others_personal_data" [30]=> string(26) "erase_others_personal_data" [50]=> string(13) "setup_network" } array(5) { [0]=> string(14) "manage_options" [1]=> string(14) "manage_options" [2]=> string(14) "manage_options" [3]=> string(14) "manage_options" [5]=> string(10) "edit_posts" } array(7) { [10]=> string(14) "manage_options" [15]=> string(14) "manage_options" [20]=> string(14) "manage_options" [25]=> string(14) "manage_options" [30]=> string(14) "manage_options" [40]=> string(14) "manage_options" [45]=> string(22) "manage_privacy_options" } array(3) { [0]=> string(14) "manage_options" [1]=> string(14) "manage_options" [2]=> string(14) "manage_options" } array(0) { } array(0) { } array(2) { [0]=> string(14) "manage_options" [1]=> string(4) "read" }
php code:
$subMenuItems = wp_list_pluck($submenu_list, 1);
var_dump($subMenuItems);
i know this array is long, is it me or is this broken
i hope that someone can explain why the array items are not like [0] => .... [1] => .... but random
According to the documentation:
wp_list_pluck( array $list, int|string $field, int|string $index_key = null ): array
Array of found values. If $index_key is set, an array of found values with keys corresponding to $index_key. If $index_key is null, array keys from the original $list will be preserved in the results.
You need to add a third parameter, if you want to conserve indexed keys order.
Regards,
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 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.
Here is a sample var_dump. Now, how do I know how to construct a foreach from it to load any particular word or fragment in to an array?
object(stdClass)#1 (2)
{
["noun"]=>
object(stdClass)#2 (1)
{
["syn"]=> array(24)
{
[0]=> string(12) "domestic dog"
[1]=> string(16) "Canis familiaris"
[2]=> string(5) "frump"
[3]=> string(3) "cad"
[4]=> string(7) "bounder"
[5]=> string(10) "blackguard"
[6]=> string(5) "hound"
[7]=> string(4) "heel"
[8]=> string(5) "frank"
[9]=> string(11) "frankfurter"
[10]=> string(6) "hotdog"
[11]=> string(7) "hot dog"
[12]=> string(6) "wiener"
[13]=> string(11) "wienerwurst"
[14]=> string(6) "weenie"
[15]=> string(4) "pawl"
[16]=> string(6) "detent"
[17]=> string(5) "click"
[18]=> string(7) "andiron"
[19]=> string(7) "firedog"
[20]=> string(8) "dog-iron"
[21]=> string(8) "blighter"
[22]=> string(5) "canid"
[23]=> string(6) "canine"
[24]=> string(5) "catch"
}
}
}
Before we can decipher it, we have to format it.
Tobias Kun's answer shows a very good way to format the var_dump output so you can read it.
object(stdClass)#1 (2)
{
["noun"]=>
object(stdClass)#2 (1)
{
["syn"]=> array(24)
{
[0]=> string(12) "domestic dog"
[1]=> string(16) "Canis familiaris"
[2]=> string(5) "frump"
[3]=> string(3) "cad"
[4]=> string(7) "bounder"
[5]=> string(10) "blackguard"
[6]=> string(5) "hound"
[7]=> string(4) "heel"
[8]=> string(5) "frank"
[9]=> string(11) "frankfurter"
[10]=> string(6) "hotdog"
[11]=> string(7) "hot dog"
[12]=> string(6) "wiener"
[13]=> string(11) "wienerwurst"
[14]=> string(6) "weenie"
[15]=> string(4) "pawl"
[16]=> string(6) "detent"
[17]=> string(5) "click"
[18]=> string(7) "andiron"
[19]=> string(7) "firedog"
[20]=> string(8) "dog-iron"
[21]=> string(8) "blighter"
[22]=> string(5) "canid"
[23]=> string(6) "canine"
[24]=> string(5) "catch"
}
}
}
You have a stdClass object with a property called ,"noun". noun is an abject with a property called "syn", which is an array of strings.
Suppose we call the object $object. Then we can access the array like:
echo $object->noun->syn[23];
which gives us canine. So a loop might look like:
foreach($data->noun->syn as $value) {
echo $value . "<br>\n";
}
First of all you should really increase the quality of your questions. The code is not formatted at all.
If you use echo "<pre>" . print_r($your_data_object_or_array,1) . "</pre>" your data will be formatted fine.
If i understand you right this should help you:
foreach($data['noun']['syn'] as $value) {
//with this you loop through all your words in "syn" e.g. domestic, "Canis familiaris etc."
echo $value . "<br>";
}
//Ouput:
domestic
Canis familiaris
frump
cad
etc ...
I'm trying to make a multidimensional array that should print out like this (without formatting):
array(3) {
[0]=> array(5) {
[0]=> int(0)
[1]=> string(5) "Arena"
[2]=> string(18) "2012-05-3017:00:00"
[3]=> string(18) "2012-05-3000:00:00"
[4]=> string(33) "Masquerade Checkin (Participants)"
},
[1]=> array(5) {
[0]=> int(0)
[1]=> string(10) "Workshop 1"
[2]=> string(18) "2012-05-3017:00:00"
[3]=> string(18) "2012-05-3000:00:00"
[4]=> string(15) "Death Note (Live)"
},
[2]=> array(5) {
[0]=> int(0)
[1]=> string(7) "Video 6"
[2]=> string(18) "2012-05-3017:00:00"
[3]=> string(18) "2012-05-3000:00:00"
[4]=> string(26) "Takeuchi Fan Panel"
}
}
Notice from the above code that the inner array() length is always 5.
Here is my code below:
$loopsArray = array();
$data=array();
// graphing info come in here.
foreach ($events as $key => $event) {
$el=$event['event_location'] ;
$eln=$event['event_locationName'];
$ed=$event['event_date'];
$es=$event['event_start'];
$ee=$event['event_end'];
$en=$event['event_name'];
array_push($loopsArray,$el,$eln, $ed.$es,$ed.$ee,$en);
array_push($data,$loopsArray);
}
var_dump($data);
Here the print out
array(27) {
[0]=> array(5) {
[0]=> int(0)
[1]=> string(5) "Arena"
[2]=> string(18) "2012-05-3017:00:00"
[3]=> string(18) "2012-05-3000:00:00"
[4]=> string(33) "Masquerade Checkin (Participants)"
}
[1]=> array(10) {
[0]=> int(0)
[1]=> string(5) "Arena"
[2]=> string(18) "2012-05-3017:00:00"
[3]=> string(18) "2012-05-3000:00:00"
[4]=> string(33) "Masquerade Checkin (Participants)"
[5]=> int(13)
[6]=> string(11) "Autograph 1"
[7]=> string(18) "2012-06-2419:00:00"
[8]=> string(18) "2012-06-2422:00:00"
[9]=> string(17) "Parents and Anime"
}
//... continues
}
Notice that the inner arrays length double each iteration. array(5) array(10) array(15)array(20).
It doubles up to 60 elements in the last inner array. Each inner array should only have 5 elements in them. I don't understand why it is doubling or how to fix it.
Can you look over my loop and let me know how to fix it?
I have to use this multidimensional array for this code to work in JpGraph.
TIP : write $loopsArray = array(); inside foreach
better approach
instead of
array_push($loopsArray,$el,$eln, $ed.$es,$ed.$ee,$en);
array_push($data,$loopsArray);
try this
$temp = array ($el,$eln, $ed.$es,$ed.$ee,$en);
$data[] = $temp;