我正在网站上实现memcached并且我正在缓存特定查询的结果,这很有效,但我在编写代码时遇到了问题,以设置使缓存可用的所需变量。 / p>
我的数组如下,其中包含两组数据:
Array ( [0] => Array ( [0] => 9126 [id] => 9126 [1] => Oh penguin, you so silly. [title] => Oh penguin, you so silly. [2] => November-01-2011-00-14-09-ScreenShot20111031at9.jpg [path] => November-01-2011-00-14-09-ScreenShot20111031at9.jpg ) [1] => Array ( [0] => 9131 [id] => 9131 [1] => Reasons you die... [title] => Reasons you die... [2] => November-01-2011-00-17-04-ScreenShot20111031at8.jpg [path] => November-01-2011-00-17-04-ScreenShot20111031at8.jpg ) )
我可以手动设置它们,并像这样调用它们:
$id = $clean[0][0];
$title = $clean[0][1];
$path = $clean[0][2];
但是我在编写WHILE循环时遇到了问题,需要动态设置变量。我也试过FOR EACH声明无济于事:
for each($clean as $image){
$id = $image->id;
$path = $image->path;
$title = $image->title;
echo "THIS IS YOUR FREAKING ID $id THIS IS YOUR TITLE $title THIS IS YOUR PATH $path";
}
有什么见解?
编辑:
解决方案是不要将它们称为对象,正如所指出的那样,更改为引用它们如下:
$id = $image["id"];
$path = $image["path"];
$title = $image["title"];
干杯。
答案 0 :(得分:1)
$array = array(
array ( 0 => 9126,
'id' => 9126,
1 => 'Oh penguin, you so silly.',
'title' => 'Oh penguin, you so silly.',
2 => 'November-01-2011-00-14-09-ScreenShot20111031at9.jpg',
'path' => 'November-01-2011-00-14-09-ScreenShot20111031at9.jpg' ),
array ( 0 => 9126,
'id' => 9126,
1 => 'Oh penguin, you so silly.',
'title' => 'Oh penguin, you so silly.',
2 => 'November-01-2011-00-14-09-ScreenShot20111031at9.jpg',
'path' => 'November-01-2011-00-14-09-ScreenShot20111031at9.jpg' )
);
foreach( $array as $row)
{
// Based on your array, you can either do:
echo $row['id'] . ' ' . $row['title'] . $row['path']. "\n";
echo $row[0] . ' ' . $row[1] . ' ' . $row[ 2 ] . "\n";
}
答案 1 :(得分:0)
每个人都应该为你服务。如果你需要遍历$ clean,那么只需绕过另一个foreach。
$arr = $clean[0];
foreach ($arr as $value) {
echo $value; // or in your case set
}
答案 2 :(得分:0)
您可以在保存到缓存之前序列化整个数组,然后在从缓存中检索时反序列化。然后只需引用您指示的值。