1

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

4
  • Why the colons at all? Get rid of them, use just regular words as keys... Commented Nov 9, 2013 at 3:13
  • 1
    Perhaps this is some space funkiness. Maybe there is more than one space that is swallowed by the browser? Make sure you copy & paste from the browser's source code view. (But what Henrique says applies too) Commented Nov 9, 2013 at 3:15
  • 2
    string(137) "21 Years. " But there's significantly less than 137 characters. You probably are not seeing all of the spaces actually present, as @Pekka웃 says. Commented Nov 9, 2013 at 3:17
  • @HenriqueBarcelos @Pekka @Waleed: Thank you for your input. Why I have colons in array is cause I get this array with file_get_contents and surly I want to trim them. Now I found out why I cant echo and it was after looking at source code I found out that this Array have a LOT of HTML codes inside. How can I remove HTML codes from this Array? Should I use strip_tags how to use it? Commented Nov 9, 2013 at 3:45

1 Answer 1

4

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); 
Sign up to request clarification or add additional context in comments.

3 Comments

I guess you are right. It looks like a whitespace/space after colon signs. But now I copy pasted my source code to a NEW text document and the whitespace/space made NEW LINES. So it looks like space but is actually \n Please have a look at my question: stackoverflow.com/questions/19873071
Cybrog, I have modified my answer. You can try this it will remove all html tags from keys and values.
Thanks a Million!!! This works 100%! thats exactly what I needed and what I was asking for. Thanks again for helping me Ram

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.