是否有可能逸出array(1) { [0]=> string(12)}
从var_dump($variable)
,因为我想从的var_dump和除了仅显示值array string
?
测试代码
<?php
$array = array(
"foo" => "bar",
"bar" => "foo",
100 => -100,
-100 => 100,
);
var_dump($array);
?>
现在的结果会是这样
array(4) {
["foo"]=>
string(3) "bar"
["bar"]=>
string(3) "foo"
[100]=>
int(-100)
[-100]=>
int(100)
}
但是,我想只有bar
和foo
除了串(3)和阵列(4)值?
就在这儿:
foreach ($array as $key => $value){
echo $key.'=>'.$value."\n";
}
对于每一个键 - 值对,如所期望这将回声的项目。
<?php
$array = array(
"foo" => "bar",
"bar" => "foo",
100 => -100,
-100 => 100
);
print_r($array);
$newArray = array_filter($array, function($v) {
return (gettype($v) != 'string');
});
print_r($newArray);
?>
输出:
Array
(
[foo] => bar
[bar] => foo
[100] => -100
[-100] => 100
)
Array
(
[100] => -100
[-100] => 100
)
$newArray
包含了除字符串的所有值。 (你可以改变!=
到==
只得到字符串值)
您的编辑后,我想你可能想这(关联数组访问单个项目):
echo $array['bar'];
echo $array['foo'];
输出:
foo
bar
http://php.net/manual/en/function.array-values.php
$array = array(
"a" => "bar",
"b" => "foo",
);
var_dump($array);
//bar
//foo
尝试
<?php
$array = array(
"foo" => "bar",
"bar" => "foo",
100 => -100,
-100 => 100,
);
echo '<pre>';
print_r($array);
echo '</pre>';
?>
你可以这样做:
call_user_func_array('var_dump', $array);
即使用var_dump()
上的每个值$array
,而不是整个阵列:
$array = array(
"foo" => "bar",
"bar" => "foo",
100 => -100,
-100 => 100,
);
call_user_func_array('var_dump', $array);
echo implode(', ', $array); # for comparison
输出:
string(3) "bar"
string(3) "foo"
int(-100)
int(100)
bar, foo, -100, 100