1

是否可以逃脱array(1) { [0]=> string(12)}var_dump($variable)因为我只想显示 var_dump 和 except 的值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)
}

但我只想获取字符串(3)和数组(4)以外的值barfoo

4

5 回答 5

2

就在这儿:

foreach ($array as $key => $value){
    echo $key.'=>'.$value."\n";
}

对于每个键值对,这将根据需要回显项目。

于 2013-04-12T21:48:03.527 回答
1
<?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
于 2013-04-12T21:47:23.010 回答
1

http://php.net/manual/en/function.array-values.php

$array = array(
    "a" => "bar",
    "b" => "foo",
);


var_dump($array);
//bar
//foo
于 2015-04-21T13:50:59.940 回答
0

尝试

<?php
  $array = array(
    "foo" => "bar",
    "bar" => "foo",
    100   => -100,
    -100  => 100,
  );

  echo '<pre>';
  print_r($array);
  echo '</pre>';
?>
于 2013-04-12T21:47:01.990 回答
0

你可以这样做:

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
于 2013-04-12T21:51:53.717 回答