请问如何从VAR转储阵列只值?(how do i get only values from var

2019-10-18 00:44发布

是否有可能逸出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)
}

但是,我想只有barfoo除了串(3)和阵列(4)值?

Answer 1:

就在这儿:

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

对于每一个键 - 值对,如所期望这将回声的项目。



Answer 2:

<?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


Answer 3:

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

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


var_dump($array);
//bar
//foo


Answer 4:

尝试

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

  echo '<pre>';
  print_r($array);
  echo '</pre>';
?>


Answer 5:

你可以这样做:

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


文章来源: how do i get only values from var dump array?