日期:2014-05-17  浏览次数:20520 次

关于数组取值的问题!
PHP code

//已知数组
$info=array();
//输出
print_r($info);
//结果
Array
(
    [0] => Array
        (
            [name] => 8.gif
            [md5] => b68d88dbdfc9d0af2e0bbd85a4ccd28f
            [saveName] => 20120602080557_452.gif
            [size] => 1
            [type] => image/gif
            [originalHeight] => 39
            [originalWidth] => 31
        )

    [1] => Array
        (
            [name] => 9.gif
            [md5] => 4988f85ebebf82f56d70eeaf39a67016
            [saveName] => 20120602080557_776.gif
            [size] => 2
            [type] => image/gif
            [originalHeight] => 39
            [originalWidth] => 31
        )

)




我要想取出对应的 saveName 的值, 请问该如何下手

------解决方案--------------------
PHP code

foreach($info as $value){
foreach($value as $getValue){
echo $getValue['saveName'];
}

}

------解决方案--------------------
foreach($info as $v) $temp[]=$v['saveName'];
print_r($temp);
------解决方案--------------------
看来name是类似于唯一字段了,想取出来和name对应的savename字段。如果这样的话,需要接收你给的name字段值,当你用post表单提交的吧。

二维数组。一个foreach判断就够了

PHP code

$a=Array( Array (
            'name' => '8.gif',
            'md5' => 'b68d88dbdfc9d0af2e0bbd85a4ccd28f' ,
            'saveName' => '20120602080557_452.gif',
            'size' => 1       ,
            'type' => 'image/gif'   ,
            'originalHeight' => 39 ,
            'originalWidth' => 31
        ),
Array(
            'name' => '9.gif'      ,
            'md5' => '4988f85ebebf82f56d70eeaf39a67016',
            'saveName' => '20120602080557_776.gif'  ,
            'size' => 2    ,
            'type' => 'image/gif'  ,
            'originalHeight' => 39 ,
            'originalWidth' => 31 ,
        )

)   ;
foreach ($a as $k=>$v) {
if ($v['name']===$_POST['yourname']){//令$_POST['yourname']=‘8.gif’,将输出$v ['saveName']=20120602080557_452.gif
echo '$v ['.'savename'.']='.$v ['saveName'] .'<br>'   ;  }  }
    
?>