-2

I have a string array like this,

C:\wamp64\www\date\task.php:37:
array (size=6)
  0 => string '_06_CM_1640=[04];' (length=17)
  1 => string '_06_CM_1899=[05];' (length=17)
  2 => string '_06_CM_2094=[24];' (length=17)
  3 => string '_06_CM_2096=[12];' (length=17)
  4 => string '_06_CM_1871=[16,12];' (length=20)
  5 => string '_06_CM_3369=[09];' (length=17)

and I want to convert it to int array.

$_06_CM_1640 = [4];
$_06_CM_1899 = [5];
$_06_CM_2096 = [12]; 
$_06_CM_2094 = [24]; 
$_06_CM_1871 = [16,12]; 
$_06_CM_3369 = [16]; 

How can I do this in PHP? please assist me about this matter? regards, Bos

4
  • You mean make individual variables? I am not sure I understand what you are trying to do.... Commented Dec 12, 2019 at 13:48
  • Using array_map seems like a good start to me. Commented Dec 12, 2019 at 13:50
  • And a regex to split/extract from the square brackets. Commented Dec 12, 2019 at 13:52
  • @Jeremy Harris, yes Commented Dec 12, 2019 at 13:53

5 Answers 5

0

You can use array_walk

array_walk($aa, function($v, $k) use (&$new){
  $inner = explode('=', $v);
  $new[$inner[0]] = $inner[1];
});
print_r($new);// this will give you array with keys

Now you can use extract($new) to make each key as variable and access them using $_06_CM_1640

Example :- https://3v4l.org/ddKZj

Sign up to request clarification or add additional context in comments.

1 Comment

Dear Rakesh Jakhar your code is good. But I want to separately arrays, like this below.$_06_CM_1640 = [4]; $_06_CM_1899 = [5]; $_06_CM_2096 = [12]; $_06_CM_2094 = [24]; $_06_CM_1871 = [16,12]; $_06_CM_3369 = [16];
0

This splits it down in stages, first by = which gives the name and value, then using trim() to remove any of =[]; round the values, finally explode the string to make it into an array of numbers...

$arr = [0 => '_06_CM_1640=[04];',
    1 => '_06_CM_1899=[05];',
    2 => '_06_CM_2094=[24];',
    3 =>  '_06_CM_2096=[12];',
    4 =>  '_06_CM_1871=[16,12];',
    5 =>  '_06_CM_3369=[09];'];

$output = [];
foreach ( $arr as $entry )  {
    list($name, $values) = explode("=", $entry,2);
    $output[$name] = explode(",", trim($values, "=[];" ));
} 


print_r($output);

gives..

Array
(
    [_06_CM_1640] => Array
        (
            [0] => 04
        )

    [_06_CM_1899] => Array
        (
            [0] => 05
        )

    [_06_CM_2094] => Array
        (
            [0] => 24
        )

    [_06_CM_2096] => Array
        (
            [0] => 12
        )

    [_06_CM_1871] => Array
        (
            [0] => 16
            [1] => 12
        )

    [_06_CM_3369] => Array
        (
            [0] => 09
        )

)

I would actually leave it as an array rather than individual values.

Comments

0

It's not very secure but something like this do the trick :

<?php
function convertStringArrayToIntArray($src)
{
    $int = [];
    foreach ($src as $key => $value){
        $split = explode('=', $value);
        $newKey = $split[0];
        $newValue = $split[1];
        $newValue = str_replace(['[', ']', ';'], '', $newValue);
        $newValue = explode(',', $newValue);
        array_walk($newValue, function(&$value, &$key){
            $value = intval($value);
        });
        $int[$newKey] = $newValue;
    }
    return $int;
}

$src = [
    0 => '_06_CM_1640=[04];',
    1 => '_06_CM_1899=[05];',
    2 => '_06_CM_2094=[24];',
    3 => '_06_CM_2096=[12];',
    4 => '_06_CM_1871=[16,12];',
    5 => '_06_CM_3369=[09];'
];
$ints = convertStringArrayToIntArray($src);
extract($ints);
var_dump(get_defined_vars());
//        $_06_CM_1640 = [4];
//        $_06_CM_1899 = [5];
//        $_06_CM_2096 = [12];
//        $_06_CM_2094 = [24];
//        $_06_CM_1871 = [16,12];
//        $_06_CM_3369 = [16];

Comments

0
$arr = [
    '_06_CM_1640=[04];',
    '_06_CM_1899=[05];',
    '_06_CM_2094=[24];',
    '_06_CM_2096=[12];',
    '_06_CM_1871=[16,12];',
    '_06_CM_3369=[09];'
];

$results = [];

foreach($arr as $v) {
    if (!preg_match_all('~ (?: , | (?<key> \w+ ) = \[ ) (?<val> \d+ ) ~xA', $v, $m))
        continue;

    $results[$m['key'][0]] = array_map('intval', $m['val']); 

}

var_dump($results);

// extract($results); // if you want to create the variables

2 Comments

dear Casimir et Hippolte, your code is very usefull but I want to individual array
@Bos: that's why I wrote the last commented line if you want to do that. extract create individual variables named with the array keys. Uncomment it and do for example print_r($_06_CM_1640);. Note that I commented it since using extract is generally a bad practice.
0

You can make a new one array:

foreach($arr as $stri){
    $tmp = explode('=',$stri); 
    $ar[$tmp[0]] = explode(',',preg_replace('~[\[\]\;]~', '', $tmp[1]));  // clear values
} 

and then use extract() function:

extract($ar);

print_r($_06_CM_1871);
print_r($_06_CM_3369);

Outputs:

Array
(
    [0] => 16
    [1] => 12
)
Array
(
    [0] => 09
)

Demo

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.