Assign the key a value (apparently named $billdate) in the outer foreach loop.
foreach( $array as $billdate => $date) {
foreach( $date as
v) {
echo $billdate; // Prints something like Nov 18, 2011
}
}
Answer from nickb on Stack OverflowAssign the key a value (apparently named $billdate) in the outer foreach loop.
foreach( $array as $billdate => $date) {
foreach( $date as
v) {
echo $billdate; // Prints something like Nov 18, 2011
}
}
Assuming $billdate is the key of each top-level array:
foreach ($array as $billdate => $date) {
foreach ($date as
v) {
var_dump($billdate,
v);
}
}
You can use a recursive function here, so you don't need to worry how deep your array will be.
Something like this:
function read($arr) {
foreach ($arr as $key => $value) {
if (is_array($value)) {
read($a);
}
else {
// You cann access key and value here (for each array)
}
}
}
read($myArray);
Just use another foreach inside your foreach
foreach ($myArray as $key => $value) {
foreach ($value as $subkey => $subvalue) {
if (is_array($subvalue)) {
foreach ($subvalue as $subsubkey => $subsubvalue) {
// .... and so on
}
}
}
}
Alternatively, you could use SPL RecursiveArrayIterator to get the values. Consider this example:
$values = array( 'student' => array( 'admission_no' => array('isEmpty' => 'Please enter Admission No'), 'admission_date' => array('isEmpty' => 'Please enter Admission Date'), ), 'student_personal_details' => array( 'first_name' => array('isEmpty' => 'Please enter First Name'), 'gender' => array('isEmpty' => 'Please enter Gender'), 'birth_date' => array('isEmpty' => 'Please enter Birth Date'), ), 'student_gardian_details' => array( 'first_name' => array('isEmpty' => 'Please enter Gardian First Name'), ), 'student_education_details' => array( 'roll_no' => array('isEmpty' => 'Please enter Pin Code'), ),);
$iterator = new RecursiveIteratorIterator(new RecursiveArrayIterator($values));
foreach($iterator as $key => $value) {
$data[] = $value;
}
Sample Output:
Array
(
[0] => Please enter Admission No
[1] => Please enter Admission Date
[2] => Please enter First Name
[3] => Please enter Gender
[4] => Please enter Birth Date
[5] => Please enter Gardian First Name
[6] => Please enter Pin Code
)
You can check the php function array_values and the examples in the documentation page.
See also How to “flatten” a multi-dimensional array to simple one in PHP?
Since the array elements are arrays, use array indexing to access that part of the elements:
foreach ($selected as $key => $value) {
$binding_clause[':selected_'.$key] = $value['screenshot_id'];
}
Assuming you have the array format as explained above.
for($i=0; $i<=$array.count(); $i++){
foreach($array[$i] as $key => $value){
echo "Key: ". $i . " Value:" . $key['screenshot_id']
}
}
$last = count($arr_nav) - 1;
foreach ($arr_nav as $i => $row)
{
$isFirst = ($i == 0);
$isLast = ($i == $last);
echo ... $row['name'] ... $row['url'] ...;
}
<?php
$php_multi_array = array("lang"=>"PHP", "type"=>array("c_type"=>"MULTI", "p_type"=>"ARRAY"));
//Iterate through an array declared above
foreach($php_multi_array as $key => $value)
{
if (!is_array($value))
{
echo $key ." => ". $value ."\r\n" ;
}
else
{
echo $key ." => array( \r\n";
foreach ($value as $key2 => $value2)
{
echo "\t". $key2 ." => ". $value2 ."\r\n";
}
echo ")";
}
}
?>
OUTPUT:
lang => PHP
type => array(
c_type => MULTI
p_type => ARRAY
)
Just a quick browse of the documentation on foreach would answer this question.
The first foreach is looping through all the elements of the $people array. Each key of the array is the $name, and the value (the second-level array) is the $person.
Then in the second loop, foreach person's attribute, the type of attribute is the $key and the value of that attribute is the $value.
Please do read that manual link I just gave, it explains it far better than me!
As far as book/course, the best thing I have used is self-teaching and web resources. php.net has documentation for everything, and this methodology has been more than enough to get me through various professional settings. This includes, of course, things like posting on stackoverflow. I have never had the need to buy a book or take a course. These things can help, of course, but I'm not aware of any particular magic bullet solution.
In your example, PHP does not require that the variables used in a foreach loop be declared elsewhere first. The first variable in the foreach loop, people in your example, is the array to loop over. Then you have the keyword as and then the variable that will be used inside the loop for each entry in the array. If you want, you can spec that variable as key => value instead of just value.
Read the PHP documentation. Ask questions when you don't understand.