简体   繁体   中英

PHP - How to echo a value from mapped Array?

I have a mapped Array named: $mapped

Below is a result of var_dump($mapped);

array(32) {
  ["Age: "]=> string(137) "21 Years. "
  ["Ethnicity: "]=> string(122) "Caucasian "
  ["Location: "]=> string(152) "Paris, France "
}

The problem is I don't get any results with: echo $mapped["Age: "];

I have tried:

echo $mapped["Age: "];           // No results
echo $mapped["Age:"];            // No results
echo $mapped[" Age:  "];         // No results
echo $mapped['Age: '];           // No results
echo $mapped['Age:'];            // No results
var_dump($mapped["Age: "]);      // result: NULL

What am I doing wrong? I want echo $mapped["Age: "]; to result: 21 Years

Thank you for your help

Cybrog, white spaces are creating problem for you. Try the code below to remove the white space and access any element without any extra effort.

$keys = str_replace( ' ', '', array_keys($mapped) );
$values = array_values($mapped);
$mapped = array_combine($keys, $values); 
var_dump($mapped); 

try this one to remove html

$keys = array_map("trim", array_map("strip_tags", array_keys($mapped)));
$values = array_map("trim", array_map("strip_tags", array_values($mapped)));
$mapped = array_combine($keys, $values); 
var_dump($mapped); 

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM