Saeidhp
Saeidhp

Reputation: 89

How to split the string in two arrays in Php

I have one array like this :

$array='{b_price,9500,b_discount,10,mainPrice,95000,total,95000,title,obj1},{b_price,1500,b_discount,15,mainPrice,15000,total,22500,title,obj2}'

I want first split to two array like this :

$array[0]={b_price,9500,b_discount,10,mainPrice,95000,total,95000,title,obj1}

And

$array[1]={b_price,1500,b_discount,15,mainPrice,15000,total,22500,title,obj2}

I change every array with this code

foreach ($b as $k => $m) {

                    if ($k % 2 == 0) {
                        $even[]= $m;
                    }
                    else {
                        $odd[] = $m;
                    }
                }

    $ff=array_combine($even,$odd); 

I want output change like this

 Array( Array[0] =>  ([b_price] => 9500 [b_discount] => 10 [mainPrice] => 95000 [total] => 95000 [title] =>obj1)
Array[1] => ([b_price] => 1500 [b_discount] => 15 [mainPrice] => 15000 [total] => 22500 [title] => obj2))

Upvotes: 1

Views: 213

Answers (3)

RomanPerekhrest
RomanPerekhrest

Reputation: 92854

Two approaches:

-- using explode and array_map functions:

$str = '{b_price,9500,b_discount,10,mainPrice,95000,total,95000,title,obj1},{b_price,1500,b_discount,15,mainPrice,15000,total,22500,title,obj2}';

$result = array_map(function($v){
    $r = [];
    $arr = explode(',', trim($v, '{}'));
    foreach ($arr as $k => $v) {
        if (!($k % 2)) $r[$v] = $arr[$k+1];
    }
    return $r;
}, explode('},{', $str));

print_r($result);

-- using additional preg_match_all and array_combine functions:

$str = '{b_price,9500,b_discount,10,mainPrice,95000,total,95000,title,obj1},{b_price,1500,b_discount,15,mainPrice,15000,total,22500,title,obj2}';

$result = array_map(function($v){
    preg_match_all('/([^,]+),([^,]+),?/', trim($v, '{}'), $m);
    return array_combine($m[1], $m[2]);
}, explode('},{', $str));

print_r($result);

The output:

Array
(
    [0] => Array
        (
            [b_price] => 9500
            [b_discount] => 10
            [mainPrice] => 95000
            [total] => 95000
            [title] => obj1
        )

    [1] => Array
        (
            [b_price] => 1500
            [b_discount] => 15
            [mainPrice] => 15000
            [total] => 22500
            [title] => obj2
        )
)

Upvotes: 2

kaszmaryk
kaszmaryk

Reputation: 1

So, I write this decision. Maybe it can be more clear, but it works.

$array='{b_price,9500,b_discount,10,mainPrice,95000,total,95000,title,obj1},{b_price,1500,b_discount,15,mainPrice,15000,total,22500,title,obj2}';

/*Making array from string*/
$tmp_array = explode("},{", $array);

/*Removing { symbols*/
$tmp_array[0] = substr($tmp_array[0],1);
$tmp_array[1] = substr($tmp_array[1],0,-1);

/*Making arrays from string [0] and [1]*/
$tmp_array[0] = explode(',',$tmp_array[0]);
$tmp_array[1] = explode(',',$tmp_array[1]);

$new_array = [];

/*Creating associative arrays*/
for($a = 0; $a < count($tmp_array); $a++) {
    $new_as_array = [];
    for($i = 0; $i <= count($tmp_array[0]); $i+=2) {
        if($i + 1 <= count($tmp_array[0])) {
            $new_as_array[$tmp_array[$a][$i]] = $tmp_array[$a][$i + 1];        
        }
    }
    $new_array[] = $new_as_array;
}

print_r($new_array);

Upvotes: 0

Muhammad Akber Khan
Muhammad Akber Khan

Reputation: 757

you should change your needle, in your array string, i have changed it with semicolon,

    $arrayString='{b_price,9500,b_discount,10,mainPrice,95000,total,95000,title,obj1};{b_price,1500,b_discount,15,mainPrice,15000,total,22500,title,obj2}';
    echo $arrayString;
    echo "<pre>"; print_r (explode(";",$arrayString));
$b=explode(";",$arrayString);
foreach ($b as $k => $m) {

                    if ($k % 2 == 0) {
                        $even[]= $m;
                    }
                    else {
                        $odd[] = $m;
                    }
                }

    $ff=array_combine($even,$odd); 

Upvotes: 0

Related Questions