0

How do I extract the numeric values from this array?

array (
  0 => '\'268',
  1 => '252',)

Just need the numbers stripped out, then I need to do some calculations.

2
  • What is your expected output ??? Commented Apr 2, 2013 at 17:25
  • two strings or two integers, but just the number, no special characters. Commented Apr 2, 2013 at 18:34

1 Answer 1

1
$source = array(
    0 => '\'268',
    1 => '252',
);

function strip($element)
{
    $matches = array();
    preg_match('#[0-9]+#', $element, $matches);
    return (int)reset($matches);
}

$result = array_map('strip', $source);

var_dump($result);

with result:

array (size=2)
  0 => int 268
  1 => int 252
Sign up to request clarification or add additional context in comments.

1 Comment

thanks. I did it the long and hard way, but this looks much easier :). I used $tcx = (int)(trim($tc2[0],"'\'"); $tcy = (int)(trim($tc2[1],"'"); and that returns me two strings. I am assuming I don't need to do a var_dump($result) in the end code?

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.