When I use the function var_dump, in my file php, It doesn't print the array like before. I haven't touched the file php.ini in my wampserver,
function getFolder_recursive(&$array,$dir){
if(is_dir($dir)){
$handle=opendir($dir);
while (false!== ($entry=readdir($handle))) {
if ($entry != "." && $entry != "..") {
if (is_dir($dir.$entry)){
$allFiles[]= "D: " . $dir . "/" . $entry;
// echo "<a href='http://localhost/eclipse/access_log/logfiles.php?cartella=$c' class='btn btn-light>Cartella</a>";
}
else{
$allFiles[]="F: " . $dir . "/" . $entry;
}
}
}
closedir($handle);
--> var_dump($allFiles);
}
return $array;
}
This is how it prints:
array(7) { [0]=> string(22) "D: files/log_files//10" [1]=> string(21) "D: files/log_files//2" [2]=> string(21) "D: files/log_files//3" [3]=> string(21) "D: files/log_files//4" [4]=> string(21) "D: files/log_files//6" [5]=> string(21) "D: files/log_files//7" [6]=> string(21) "D: files/log_files//
I expect that var_dump prints my array formatted
Just add <pre> tags
function getFolder_recursive(&$array,$dir){
if(is_dir($dir)){
$handle=opendir($dir);
while (false!== ($entry=readdir($handle))) {
if ($entry != "." && $entry != "..") {
if (is_dir($dir.$entry)){
$allFiles[]= "D: " . $dir . "/" . $entry;
// echo "<a href='http://localhost/eclipse/access_log/logfiles.php?cartella=$c' class='btn btn-light>Cartella</a>";
}
else{
$allFiles[]="F: " . $dir . "/" . $entry;
}
}
}
closedir($handle);
echo "<pre>";
var_dump($allFiles);
echo "</pre>"
}
return $array;
}
Related
I have an array list that I wish to implode as I want to display each array with indexing to target that element. I'm trying but it is not displaying correctly. What am I doing wrong?
<?php
/*
var_dump($names); // below
array(4) {
[0]=>
string(22) "David Kaul"
[1]=>
string(23) "Julius Kaul"
[2]=>
string(22) "Robert Kaul"
[3]=>
string(22) "Juohn Kaul"
} */
for ($i = 0; $i <= 10; $i+=1) {
echo '<li class="item-' . $i . '">' . implode('</li><li class="item-'.$i.'">', $names) . '</li>';
}
?>
I think you just want a simple foreach over the $names array:
$names = array("David Kaul","Julius Kaul","Robert Kaul","Juohn Kaul");
foreach ($names as $i => $name) {
echo '<li class="item-' . $i . '">' . $name. '</li>' . "\n";
}
Output:
<li class="item-0">David Kaul</li>
<li class="item-1">Julius Kaul</li>
<li class="item-2">Robert Kaul</li>
<li class="item-3">Juohn Kaul</li>
Demo on 3v4l.org
I have an array of arrays and accessing the data is fine, but when I try to create separate tables based on a key, which is set to the numeric month that the data pulled from the database falls under.
I have:
array(1) {
[5]=>
array(12) {
["ID"]=>
string(5) "1001"
["created_timestamp"]=>
string(26) "2015-06-11 19:10:00"
["firstname"]=>
string(4) "John"
["lastname"]=>
string(3) "Doe"
}
}
array(1) {
[5]=>
array(12) {
["ID"]=>
string(5) "1002"
["created_timestamp"]=>
string(26) "2015-06-12 19:10:00"
["firstname"]=>
string(4) "Jane"
["lastname"]=>
string(3) "Doe"
}
}
array(1) {
[6]=>
array(12) {
["ID"]=>
string(5) "1003"
["created_timestamp"]=>
string(26) "2015-07-16 19:20:00"
["firstname"]=>
string(4) "John"
["lastname"]=>
string(4) "John"
}
}
The [5] Represents the current month -1. What I want to do is create a table and headers but only once, then populate the table with first and last name.
foreach($m as $key => $value)
{
if(key($m) != $currentKey)
{
$currentKey = key($m);
}
if($key == date("n", strtotime($value['created_timestamp']))-1);
{
for($i=$currentKey; $i<=$lastkey; $i++) {
echo "<br>".count($key);
$table .= '<table id=' . date("F-Y", strtotime($m[$currentKey]['created_timestamp'])) . ' class="tblclass">
<thead><tr><th>Month</th><th>ID</th><th>Customer</th></tr></thead>';
$table .= '<tbody>';
$table .= '<tr><td>'.date("F Y", strtotime($m[$currentKey]['created_timestamp'])).'</td><td>' . $value['ID'] . '</td><td>' . $value['lastname'] . ' ' . $value['firstname'] . '</td></tr>';
$table .= '</tbody>';
$table .= '</table>';
}
}
}
Whenever I run the code it creates a table for each separate array. I want to create one table for each separate month and populate it with anything with the same key. I feel like I'm missing something though. Any help would be greatly appreciated!
A dirty solution. Since info is lacking
Here are a few assumptions:
- Array is sorted in ASC order
- There is only one item in each array [ID, Name, Timestamp]
- IF there are more items, then you can just add a loop in there
- $m is the multi-dimensional array.
NOTE: Not tested.
$currentKey = 0;
$count= 0;
$lastKey = count($m) - 1;
foreach($m as $key => $value) {
if(key($m) != $currentKey) {
$currentKey = key($m);
if($count== 0) {
echo "<br>".count($key);
$table .= '<table id=' . date("F-Y", strtotime($m[$currentKey]['created_timestamp'])) . ' class="tblclass">
<thead><tr><th>Month</th><th>ID</th><th>Customer</th></tr></thead>';
$table .= '<tbody>';
} else {
$table .= '</tbody>';
$table .= '</table>';
$table .= '<table id=' . date("F-Y", strtotime($m[$currentKey]['created_timestamp'])) . ' class="tblclass">
<thead><tr><th>Month</th><th>ID</th><th>Customer</th></tr></thead>';
$table .= '<tbody>';
}
}
if($key == date("n", strtotime($value['created_timestamp']))-1) {
$table .= '<tr><td>'.date("F Y", strtotime($value['created_timestamp'])).'</td><td>' . $value['ID'] . '</td><td>' . $value['lastname'] . ' ' . $value['firstname'] . '</td></tr>';
}
if($lastKey == $count) {
$table .= '</tbody>';
$table .= '</table>';
}
$count++;
}
Hy,
how I retrieve information from an associative array like list in php.
var_dump shows something like this:
array(2) { [1]=> string(1) "1"
[2]=> string(1) "2" }
array(2) { [1]=> string(15) "gica_craioveanu"
[2]=> string(14) "alexandra_minu" }
array(2) { [0]=> string(10) "craiovaMAX"
[1]=> string(10) "alexMinu10" }
First array containd the id's, second the user name and third the passwords.
My attempt:
foreach ($row as $i => $id,$user_name,$user_pass) {
echo "<tr>\n" .
" <td>".var_dump($id)."</td>\n".
" <td>".var_dump($user_name)."</td>\n".
" <td>".var_dump($user_pass)."</td>\n".
" </tr>\n"
}
one of php file contains the script to construct the list:
while($row = mysqli_fetch_row($result))
{
/*$id[]=$row['user_id'];
$user_name[]=$row['user_name'];
$user_pass[]=$row['user_pass'];
$i=$i+1;*/
list($id[$i],$user_name[$i],$user_pass[$i++])=$row;
}
/*for($no=0; $no<$i; $no++){
echo $row[$i]."/".$row[$i]."/";
}*/
include 'user.html.php';
}
?>
and the user.html.php
to view the rows:
<?php
// while (list($id, $user_name, $user_pass)= each($row)) {
foreach($row as $i => $id,$user_name,$user_pass){
echo " <tr>\n" .
" <td>".$id."</td>\n".
" <td>".$user_name."</td>\n".
" <td>".$user_pass."</td>\n".
" </tr>\n";
/*echo "<tr>\n" .
" <td>".var_dump($id)."</td>\n".
" <td>".var_dump($user_name)."</td>\n".
" <td>".var_dump($user_pass)."</td>\n".
" </tr>\n"
*/
/*for($no=0; $no<$i; $no++){
echo "<tr>".
"<td>".$row[$i]."</td>".
"<td>".$row[$i]."</td>".
"<td>".$row[$i]."</td>".
"</tr>";*/
}
?>
while($row = mysqli_fetch_row($result)) {
list($id[$i],$user_name[$i],$user_pass[$i++])=$row;
}
...
for($i = 1; $i < count($id) + 1; $i++) {
echo " <tr>\n" .
" <td>".$id[$i]."</td>\n".
" <td>".$user_name[$i]."</td>\n".
" <td>".$user_pass[($i - 1)]."</td>\n".
" </tr>\n";
}
$id = array("1", "2");
$user_name = array("gica_craioveanu", "alexandra_minu");
$user_pass = array("craiovaMAX", "alexMinu10");
We presume that 3 arrays are always the same size :
// You make a loop on first array
for ( $i = 0 ; $i < count($id) ; $i++ )
{
// You use index $i to access to 3 arrays.
echo 'ID: '.$id[$i].'<br />';
echo 'Name: '.$user_name[$i].'<br />';
echo 'Password: '.$user_pass[$i].'<br />';
}
for($no=1; $no<$i+1; $no++)
{
echo "<tr>".
"<td>".$id[$no]."</td>".
"<td>".$user_name[$no]."</td>".
"<td>".$user_pass[($no-1)]."</td>".
"</tr>";
}
Inspired by Typoheads
I have a problem, were I need to loop through an array inside a "foreach-loop". The PHP code below is per now looping through the JSON and looking for ["Question"], and looping these correctly.
But these questions have different amounts of ["Answers"]. Also this JSON is made dynamically so I don't know from before how many questions/answers there'll be.
So what I need is the ["Answers"] should be looped withing the <li> inside each question.
Would be really greatful for some expertise here!
JSON
["Questions"]=>
array(2) {
[0]=>
object(stdClass)#13 (2) {
["Question"]=>
string(30) "My first questions is this...."
["Answers"]=>
array(2) {
[0]=>
object(stdClass)#14 (2) {
["Answers"]=>
string(18) "I like this answer"
["Correct"]=>
bool(true)
}
}
}
[1]=>
object(stdClass)#16 (2) {
["Question"]=>
string(22) "Another funny question"
["Answers"]=>
array(2) {
[0]=>
object(stdClass)#17 (2) {
["Answers"]=>
string(9) "Or is it?"
["Correct"]=>
bool(false)
}
[1]=>
object(stdClass)#18 (2) {
["Answers"]=>
string(10) "Yes it is!"
["Correct"]=>
bool(true)
}
}
}
}
PHP
$questions = array();
$i = 0;
foreach($question as $key => $items) {
$i++;
if ($i>1) {$hide=' hide';}
$questions[] = "
<div class='question-holder" . $hide . "' id='q" . $i . "'>
<h2>" . $items->Question . "</h2>
<ul class='answers' id='quiz-answers" . $i . "'>
<li>
<input tabindex='1' type='radio' name='txtAnswer" . $i . "' id='txtAlt1' value='sdsds'>
<label for='txtAlt1'><span>" . $items->Answers[0]->Answers . "</span></label>
</li>
</ul>
</div>";
}
Use a nested loop for the answers.
$tab = 0;
foreach ($question as $items) {
$i++;
$j = 0;
if ($i > 1) { $hide = ' hide'; }
$this_q = "<div class='question-holder" . $hide . "' id='q" . $i . "'>
<h2>" . $items->Question . "</h2>
<ul class='answers' id='quiz-answers" . $i . "'>
";
foreach ($items->Answers as $ans) {
$tab++;
$j++;
$qa = "$i-$j";
$this_q .= "<li>
<input tabindex='" . $tab . "' type='radio' name='txtAnswer" . $qa . "' id='txtAlt" . $qa . "' value='sdsds'>
<label for='txtAlt" . $qa . "'><span>" . $ans->Answers . "</span></label>
</li>
";
}
$this_q .= "</ul>
</div>";
$questions[] = $this_q;
}
i got this array.
array(24) {
[0]=> array(3) {
["id"]=> string(1) "1"
["category"]=> string(5) "Alles"
["description"]=> string(0) ""
}
[1]=> array(3) {
["id"]=> string(2) "11"
["category"]=> string(6) "Cinema"
["description"]=> string(0) ""
}
[2]=> array(3) {
["id"]=> string(1) "8"
["category"]=> string(8) "Computer"
["description"]=> string(0) ""
}
}
And i dont know how to show this informations:
I tried with:
for ($x = 0; $x < sizeof($array); ++$x)
{
echo "key: ".key($array)."<br>value: ".current($array)."<br>";
next($array);
}
The Key value is now right, but the current-value is also an array :(
I hope someone can help.
Should be something like
foreach($array as $key => $value)
{
echo "key: " . ' '.$key .', '.
"category: " . ' ' . $value['category'].',
"description: " . ' ' . $value['description'];
}
Use nested foreach loops i.e.
foreach ($array as $key => $value){
echo "[ ".$key." ]<br/>";
foreach ($value as $innerKey => $innerValue){
echo $innerKey." => ".$innerValue."<br/>";
}
}
You could iterate over this array as follows:
foreach($sourceArray as $key => $value) {
echo 'Item: ' . $value . '<br />';
foreach($value as $subKey => $subValue) {
echo $subKey.' -> '.$subValue.'<br />';
}
}