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和3维数组,循环没有问题但我不能确定要回送哪个数组,它们必须让整个数组像echo $subvalue,有更好的3维数组解决方案吗?我对这个循环感到奇怪。由于阿德
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
)
)
)
)
4 个解决方案
#1
3
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.
另外,我也不确定,因为你的问题不是很清楚或明确。
#2
3
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
它适用于N维数组
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:
改进后的版本了解深度,并使用不同的css类进行深度,并使用设置应该添加值的标记:
Eg: for depth 0 the class will by arrayclass_0, for depth 1 arrayclass_1, etc...
对于深度0,类将由arrayclass_0,用于depth 1 arrayclass_1,等等…
/**
$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>";
}
}
}
}
#3
0
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:
但是你已经在第一个循环中使用了$key:
foreach ($stories as $key => $story){
You should change the third to:
你应该把第三个改为:
foreach ($subvalue as $subsubkey => $subsubvalue){
#4
0
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:
您是否只想循环遍历并访问photo_urls和其他数据?如果是这样的话,这里有一个简单的例子来说明如何访问数据:
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.
如果你想递归地搜索数组,那么其他的答案就是你想要的。
#1
3
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.
另外,我也不确定,因为你的问题不是很清楚或明确。
#2
3
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
它适用于N维数组
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:
改进后的版本了解深度,并使用不同的css类进行深度,并使用设置应该添加值的标记:
Eg: for depth 0 the class will by arrayclass_0, for depth 1 arrayclass_1, etc...
对于深度0,类将由arrayclass_0,用于depth 1 arrayclass_1,等等…
/**
$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>";
}
}
}
}
#3
0
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:
但是你已经在第一个循环中使用了$key:
foreach ($stories as $key => $story){
You should change the third to:
你应该把第三个改为:
foreach ($subvalue as $subsubkey => $subsubvalue){
#4
0
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:
您是否只想循环遍历并访问photo_urls和其他数据?如果是这样的话,这里有一个简单的例子来说明如何访问数据:
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.
如果你想递归地搜索数组,那么其他的答案就是你想要的。