how do i get only values from var dump array?

拥有回忆 提交于 2019-12-24 16:26:03

问题


Is it possible to escape array(1) { [0]=> string(12)} from var_dump($variable) because I want to show only values from var_dump and except array string?

Testing Code

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

now results will be like this

array(4) {
["foo"]=>
string(3) "bar"
["bar"]=>
string(3) "foo"
[100]=>
int(-100)
[-100]=>
int(100)
}

But I want to get only bar and foo values except string(3) and array(4)?


回答1:


Right here:

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

For each key-value pair, this will echo the items as desired.




回答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);
?>

Outputs:

Array
(
    [foo] => bar
    [bar] => foo
    [100] => -100
    [-100] => 100
)
Array
(
    [100] => -100
    [-100] => 100
)

$newArray contains all values except strings. (you can change != to == to get only the string values)

After your edit I think you may want this (accessing individual items in an associative array):

echo $array['bar'];
echo $array['foo'];

Outputs:

foo
bar



回答3:


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

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


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



回答4:


Try

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

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



回答5:


You could do:

call_user_func_array('var_dump', $array);

That is using var_dump() on each value of the $array instead of the whole array:

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

call_user_func_array('var_dump', $array);

echo implode(', ', $array); # for comparison

Output:

string(3) "bar"
string(3) "foo"
int(-100)
int(100)
bar, foo, -100, 100


来源:https://stackoverflow.com/questions/15981320/how-do-i-get-only-values-from-var-dump-array

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!