What is the best way to loop through this array in PHP?
Using a foreach
loop without a key:
foreach($array as $item) {
echo $item['filename'];
echo $item['filepath'];
// To know what's in $item
echo '<pre>'; var_dump($item);
}
Using a foreach
loop with a key:
foreach($array as $i => $item) {
echo $array[$i]['filename'];
echo $array[$i]['filepath'];
// $array[$i] is same as $item
}
Using a for
loop:
for ($i = 0; $i < count($array); $i++) {
echo $array[$i]['filename'];
echo $array[$i]['filepath'];
}
var_dump
is a really useful function to get a snapshot of an array or object.
How can I loop through an array in Php and get 3 items at a time?
It looks like what you're looking for is exactly what the array_chunk
method does.
Have a look here:
https://www.php.net/manual/en/function.array-chunk.php
In your case:
$array = [1, 2, 3, 4, 5, 6, 7, 8, 9 , 10, 11];
$chunked_array = array_chunk($array, 3);
print_r($chunked_array);
Will return
Array
(
[0] => Array
(
[0] => 1
[1] => 2
[2] => 3
)
[1] => Array
(
[1] => 4
[2] => 5
[3] => 6
)
...
)
Once you have the chunked array you can just loop over that and then output all 3 keys at a time:
foreach ($chunked_array as $key => $value) {
echo $value[0];
echo $value[1];
echo $value[2];
}
PHP loop through arrays inside array
First of all, $slides => array(
is invalid syntax. I will assume you actually meant 'slides' => array(
- if that's true, the following code will list all of the properties from each array using implode()
.
foreach($gallery as $key => $slides){
echo implode(', ', $slides['slides']) . "<br>";
}
If you want to do additional processing for each slide, loop through like this:
foreach($gallery as $key => $slides){
foreach($slides['slides'] as $sub_key => $slide) {
echo "<div>Key: $sub_key<br>Slide: $slide</div>";
}
}
PHP loop through array with condition
Here is more universal function- you can pass an array as argument, and amount of elements you want to display.
<?php
$array = array(1,2,3,4,5,6,7);
function getFirstValues(&$array, $amount){
for($i=0; $i<$amount; $i++){
echo $array[0];
array_push($array, array_shift($array));
}
echo "<br />";
}
getFirstValues($array, 4);
getFirstValues($array, 4);
getFirstValues($array, 4);
getFirstValues($array, 4);
?>
The result is:
1234
5671
2345
6712
how to loop through an array within an array in php
Here's how you get the domains from in_links. The others are similar (but I'm not sure what the indexes in the impressions sub-array represent).
foreach ($array as $element) {
foreach ($element['in_links']['domains'] as $domain => $count) {
echo "Domain: $domain, Count: $count\n";
}
}
How to loop through an associative array and get the key?
You can do:
foreach ($arr as $key => $value) {
echo $key;
}
As described in PHP docs.
PHP Loop through array with no index names
You are only looping through the main array.That's the reason why you are getting an array when you are printing the result set.(because those values are stored in sub arrays and you are not looping them.)
And to remove the sub arrays with empty values I'll use isset()
so that you will get only the values.
Change your code into this.
foreach ($myarray as $item) {
foreach($item as $key=>$val){
if(isset($val){
$values[] = $val;
// $result[] = $this->myMethod($values);
}
}
}
Related Topics
How to Perform Static Code Analysis in PHP
Turning Multidimensional Array into One-Dimensional Array
Create Word Document Using PHP in Linux
Finding N-Th Permutation Without Computing Others
PHP: Custom Error Handler - Handling Parse & Fatal Errors
Encrypt and Decrypt Text With Rsa in PHP
Reference Assignment Operator in PHP, =&
File_Put_Contents - Failed to Open Stream: Permission Denied
Using a .PHP File to Generate a MySQL Dump
Examples of SQL Injections Through Addslashes()
PHP 5.4 Call-time pass-by-reference - Easy Fix Available
How to Increase Maximum Execution Time in PHP
Laravel 5.5 Ajax Call 419 (Unknown Status)