0

I have a array

["item1","item2","item3"]

I want to get an array of ["1","2","3"]

how to get that in php

2
  • 1) Have you tried something to achieve this? 2) What is the pattern behind this? Commented Jan 13, 2016 at 4:46
  • no, this is an array I want only the number of array from that Commented Jan 13, 2016 at 4:51

3 Answers 3

3

You need this

$arr = ["item1","item2","item3"];

for ($i = 0; $i < sizeof($arr); $i++) {
    // replace "item" with ""
    $arr[$i] = str_replace("item","",$arr[$i]);
}
Sign up to request clarification or add additional context in comments.

Comments

0
<?php

$given_array = ["item1","item2","item3"];
$new_array = array();
foreach ($given_array as $arr) {
    $new_array[] = intval(preg_replace('/[^0-9]+/', '', $arr), 10);
}
echo '<pre>';
print_r($new_array);

?>

Comments

0

1) Simply use

$res = str_replace('item', '', $array);

Output $res

Array
(
  [0] => 1
  [1] => 2
  [2] => 3
)

2) Using array_map()

$array = array_map(
  function($str) {
    return str_replace('item', '', $str);
  },
  $array
);

Comments

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.