I have this array $pages which spits out this data:
Array (
[Name] => Array (
[Subname] => Array (
[0] => 43.2057, -79.9632, 1, -70,-150
[1] => 140240757658.jpg
[2] => 5
[3] => 0
) )
[Name2] => Array (
[Subname2] => Array (
[0] => 43.1769, -79.4703, 5, -70,-150
[1] => 140267498933.png
[2] => 16
[3] => 0
) )
)
and I have this foreach setup:
foreach($pages as $row => $value) {
echo '<li>'.$row.'<ul>';
foreach($value as $x => $y) {
echo
'<li>
'.$x.'
</li></ul></li>';
}
}
What I am trying to do is if [3] in each of the Subname is equal to 0, then skip it from my foreach.
NOTE: Subname and Name are just examples, they will be different for each one.
This should work:
foreach ($pages as $page) {
foreach($page as $subname) {
if ($subname[3] != 0) {
/* Do whatever you want with the data of this subname */
}
}
}
Or this if you want to use the key names:
foreach ($pages as $pageKey => $page) {
foreach($page as $subnameKey => $subname) {
if ($subname[3] != 0) {
/* Do whatever you want with the data of this subname */
}
}
}
If you want to skip the sub-foreach then this is the solution
foreach($pages as $row => $value) {
if($value['Subname'][3] == 0)
continue;
echo '<li>' . $row . '<ul>';
foreach($value as $x => $y) {
echo '<li>' . $x . '</li></ul></li>';
}
}
Since this is an Associative Array you will not be able to to check
$pages['foo']['bar'][0] === 0
before the first foreach begins since we don't know the value of $pages['foo'] before hand. when we get to the check you have already output html at the point in the form of:
echo '<li>' . $row . '<ul>';
Related
I have a multidimensional array that I want to break into 6 div columns, and I can't seem to do it so any help with this ?
Here's the array :
Array
(
[0] => Array
(
[id] => 17
[title] => White
[ref] => 24941
)
[1] => Array
(
[id] => 18
[title] => Blue
[ref] => 11395
)
[2] => Array
(
[id] => 19
[title] => Red
[ref] => 11394
)
.
.
.
and here's my foreach loop:
echo '<div class="row">';
echo '<div class="col-xs-2">';
$i = 1;
foreach ($colors as $key => $value) {
if ($i % 6 === 0) {
echo $value['title']. 'Ref: '. $value['ref']
}
echo '</div><div class="col-xs-2">';
$i++;
}
echo '</div>';
echo '</div>';
Much appreciated.
Though I don't get what you do with $all_colors = explode(",",$check_colors['value']);, this should help you out:
$item = reset($colors);
while ($item) {
echo "<div class='row'>";
for ($i = 0; $i < 6; $i++) {
if ($item)
echo "<div class='col-xs-2'>{$item['title']}Ref: {$item['ref']}</div>";
else
break;
$item = next($colors);
}
echo "</div>";
}
I have an associative array in php, how can i print it with php foreach loop
Array( [0] => Array ( [fb_user_id] => 100000058716604 [accept_status] => 1 ) [1] => Array ( [fb_user_id] => 100004069844270 [accept_status] => 1 ) )
Tried this but no success, i want to print the fb_user_id
foreach($resulttotal[fb_user_id] as $value)
{
echo $value;
}
Please help me, Thanks
You doing incorrectly, It will be like:
foreach($resulttotal as $value)
{
echo $value['fb_user_id'];
}
It would make more sense to use a regular for loop.
for($i = 0; $i < count($resulttotal); $i++) {
$row = & $resulttotal[$i];
echo $row['fb_user_id'];
}
But this could be a foreach too.
foreach($resulttoal as $key) {
echo $key['fb_user_id']
}
I have array which show result like this :
Array ( [standards_id] => 1
[value] => sory
[order] => 10
)
Array ( [standards_id] => 1
[value] => javid
[order] => 3
)
Array ( [standards_id] => 1
[value] => saleem
[order] => 4
).
I want to check the array key ,if it is "value" then i want to concatenate its value.I try code like this but not successeded.
$row = array(.....);
$vali = '';
foreach ($row as $key => $value) {
if( $value[$key] == 'value'){
echo $vali .= $value['value'].",";
}
}
I want to do it in one loop.$row contains multiple arrays like above 3.$row contains all the records that are fetched from data base.hope you understand what $row is.
Use isset() to find out whether the key 'value' is present in each element of $row; you don't need a loop for that at all:
foreach ($row as $data) {
if (isset($data['value'])) {
$vali .= $data['value'] . ',';
}
}
Alternatively, you can build an array with the values:
$values = array();
foreach ($row as $data) {
if (isset($data['value'])) {
$values[] = $data['value'];
}
}
echo join(',', $values);
Your naming suggests that $row contains just:
Array ( [standards_id] => 1
[value] => sory
[order] => 10
)
There is no reason to loop over such a structure. You can test if this 'row' contains a value with
if( isset( $row['value'] ) ) {
$vali .= $row['value'] . ', ';
}
If you have a variable $myRows containing these arrays, you can loop over them using
foreach( $myRows as $k => $row ) { ... }
In this case $row contains the array and you can use the first code to append the value to $vali.
You need this:
foreach ($row as $key => $value) {
if( $key == 'value'){
$vali .= $value.",";
}
}
echo $vali;
$row = array('standards_id' => 1, 'value' => 'saleem', 'order' => 4);
$vali = '';
foreach ($row as $key => $value) {
if( $key == 'value'){
$vali .= $value . ",";
}
}
echo $vali;
Hi,
How can we find the count of duplicate elements in a multidimensional array ?
I have an array like this
Array
(
[0] => Array
(
[lid] => 192
[lname] => sdsss
)
[1] => Array
(
[lid] => 202
[lname] => testing
)
[2] => Array
(
[lid] => 192
[lname] => sdsss
)
[3] => Array
(
[lid] => 202
[lname] => testing
)
)
How to find the count of each elements ?
i.e, count of entries with id 192,202 etc
You can adopt this trick; map each item of the array (which is an array itself) to its respective ['lid'] member and then use array_count_value() to do the counting for you.
array_count_values(array_map(function($item) {
return $item['lid'];
}, $arr);
Plus, it's a one-liner, thus adding to elite hacker status.
Update
Since 5.5 you can shorten it to:
array_count_values(array_column($arr, 'lid'));
foreach ($array as $value)
{
$numbers[$value[lid]]++;
}
foreach ($numbers as $key => $value)
{
echo 'numbers of '.$key.' equal '.$value.'<br/>';
}
Following code will count duplicate element of an array.Please review it and try this code
$arrayChars=array("green","red","yellow","green","red","yellow","green");
$arrLength=count($arrayChars);
$elementCount=array();
for($i=0;$i<$arrLength-1;$i++)
{
$key=$arrayChars[$i];
if($elementCount[$key]>=1)
{
$elementCount[$key]++;
} else {
$elementCount[$key]=1;
}
}
echo "<pre>";
print_r($elementCount);
OUTPUT:
Array
(
[green] => 3
[red] => 2
[yellow] => 2
)
You can also view similar questions with array handling on following link
http://solvemyquest.com/count-duplicant-element-array-php-without-using-built-function/
The following code will get the counts for all of them - anything > 1 at the end will be repeated.
<?php
$lidCount = array();
$lnameCount = array();
foreach ($yourArray as $arr) {
if (isset($lidCount[$arr['lid']])) {
$lidCount[$arr['lid']]++;
} else {
$lidCount[$arr['lid']] = 1;
}
if (isset($lnameCount [$arr['lname']])) {
$lnameCount [$arr['lname']]++;
} else {
$lnameCount [$arr['lname']] = 1;
}
}
$array = array('192', '202', '192', '202');
print_r(array_count_values($array));
$orders = array(
array(
'lid' => '',
'lname' => '',
))....
$foundIds = array();
foreach ( $orders as $index => $order )
{
if ( isset( $foundIds[$order['lid']] ) )
{
$orders[$index]['is_dupe'] = true;
$orders[$foundIds[$order['lid']]]['is_dupe'] = true;
} else {
$orders[$index]['is_dupe'] = false;
}
$foundIds[$order['lid']] = $index;
}
Try this code :
$array_count = array();
foreach ($array as $arr) :
if (in_array($arr, $array_count)) {
foreach ($array_count as $key => $count) :
if ($key == $arr) {
$array_count[$key]++;
break;
}
endforeach;
} else {
$array_count[$arr] = 1;
}
endforeach;
Check with in_array() function.
Below is foreach and the 3 dimension arrays, no problem with the looping but i cannot sepcify which array to echo, they must me the whole arrays like echo $subvalue, any better solutions with looping 3 dimension array? i actually feel weird with this looping. Thanks in adv
foreach ($stories as $key => $story){
//echo "<br />";
foreach($story as $subkey => $subvalue){
echo $subvalue."<br />";
foreach($subvalue as $key => $subsubvalue){
echo $subsubvalue."<br />";
}
}
}
Array
(
[270] => Array
(
[uid] => 36
[user_email] => aaa#hotmail.com
[sid] => 270
[story_name] => Story C
[photo_url] => Array
(
[0] => story_photos/2012/0322/361332381418153311.jpg
[1] => story_photos/2012/0322/361332393792911587.jpg
)
[photo_added_date] => Array
(
[0] => 1332381418
[1] => 1332393792
)
)
[269] => Array
(
[uid] => 36
[user_email] => aaa#hotmail.com
[sid] => 269
[story_name] => Story B
[photo_url] => Array
(
[0] => story_photos/2012/0322/361332381406580761.jpg
)
[photo_added_date] => Array
(
[0] => 1332381406
)
)
[268] => Array
(
[uid] => 36
[user_email] => aaa#hotmail.com
[sid] => 268
[story_name] => Story A
[photo_url] => Array
(
[0] => story_photos/2012/0322/361332381393552719.jpg
)
[photo_added_date] => Array
(
[0] => 1332381393
)
)
)
Why not try this :
foreach ($stories as $key => $story){
if(is_array($story)){
foreach($story as $subkey => $subvalue){
if(is_array($subvalue)){
foreach($subvalue as $key => $subsubvalue){
echo $subsubvalue."<br />";
}
} else {
echo $subvalue."<br />";
}
}
} else {
echo $story."<br />";
}
}
Also, I am not sure because your question isn't really clear or specified.
Or
function echoArray( $array )
{
foreach ($array as $key => $cell)
{
if ( true == is_array($cell) )
{
echoArray($cell);
}
else
{
echo "$cell<br />";
}
}
}
It works for N dimensionnal array
An improved version to know the depth and use different css class for depth and to use set the tag in which the value should be added:
Eg: for depth 0 the class will by arrayclass_0, for depth 1 arrayclass_1, etc...
/**
$array : The array
$depth: The depth ( you should always set it to 0)
$cssclassprefix: The css class prefix you want to set
$tag: the default tag to use to render
$arraytagkey: An optionnal key in your array to extract the tag to use
*/
function echoArray( $array, $depth=0, $cssclassprefix='arrayclass_', $tag='div', $arraytagkey = '' )
{
if ( 0 != strcmp($arraytagkey) && isset($array[$arraytagkey]) )
{
$tag=$array[$arraytagkey];
}
foreach ($array as $key => $cell)
{
if ( true == is_array($cell) )
{
echoArray($cell, $depth+1, $cssclassprefix, $tag, $arraytagkey);
}
else
{
$matches = array();
if ( 0 != preg_match("/^(img|iframe|input)$/i",$tag) )
{
if ( 0 != strcasecmp('input',$tag) )
{
echo "<input class='$cssclassprefix$depth' value='$cell' />";
}
else
{
echo "<$tag class='$cssclassprefix$depth' src='$cell' />";
}
}
else if( 0 != preg_match("/^(br|hr)$/i",$tag) )
{
echo "$cell<$tag />";
}
else
{
echo "<$tag class='$cssclassprefix$depth'>$cell</$tag>";
}
}
}
}
This doesn't really answer your question, but note, your third loop is this:
foreach ($subvalue as $key => $subsubvalue){
But you've already used $key in the first loop:
foreach ($stories as $key => $story){
You should change the third to:
foreach ($subvalue as $subsubkey => $subsubvalue){
Are you just wanting to loop through and get access to the photo_urls and other data? If that's the case then here's a simple example of how you can access the data:
foreach($stories as $key => $story)
{
// here you can echo the $store['user_email'] etc
echo 'Email: ' . $story['user_email'] . '<br />';
// loop over the photo urls
foreach($story['photo_url'] as $photo_url)
{
echo 'Photo URL: ' . $photo_url . '<br />';
}
// loop over the photo added dates
foreach($story['photo_added_date'] as $photo_added_date)
{
echo 'Photo Date: ' . $photo_added_date . '<br />';
}
}
If you're wanting to recursively search the array then the other answers are what you want.