OsomA
OsomA

Reputation: 147

PHP Transformation string

Let's say that i have an array with this structure

$array = Array(Array('a' => 'a', 'x' => $something));

The variable $something can be like that: 1-3 or 1,3 or 1-3, 2-5. I what to transform the variable $array in:

Case 1. $something = 1-3

$array = Array(Array('a' => 'a', 'x' => Array(1,2,3)));

Case 2. $something = 1,3

$array = Array(Array('a' => 'a', 'x' => Array(1,3)));

Case 3.. $something = 1-3, 2-5

$array = Array(Array('a' => 'a', 'x' => Array(1,2,3,4,5)));

I tried to use preg_match but it doesn't work. Can you give me hint to use something else?

Upvotes: 1

Views: 71

Answers (4)

geoandri
geoandri

Reputation: 2428

If the values inside $something are always sorted as your example suggests you can use the following code

$newomething = range(substr($something, 0, 1),substr($something, -1));

UPDATE working solution with not sorted inputs. The following code will work on inputs like

  $something= '4,5,1-3,13,7-9';

    $something = explode(',',$something);
    $result= array();
   foreach($something as $value) 
   {
      if (strpos($value,'-') !== false)
       {
         $r = explode('-',$value);
         $rad = range($r[0],$r[1]);
         $result = array_merge($result,$rad);
        }
       else
       {
        array_push($result,$value);
        }   
    }
    sort($result);

Upvotes: 0

Elias
Elias

Reputation: 1562

You are looking for explode to split your input string, range to generate the array its contents and array_merge to merge the result.

Something like this works:

<?php
$something = '1-3, 2-5';
$array = array(array('a' => 'a', 'x' => array()));

$result = array();

foreach (explode(', ', $something) as $input) {
    $rangeParts = explode('-', $input);
    $result = array_merge($result, range($rangeParts[0], $rangeParts[1]));    
}

// contains duplicate entries because 1-3 and 2-5 overlap - use array_unique to remove duplicates or change input
$array[0]['x'] = $result;

In the last assignment you can either wrap $result in array_unique to 1:1 match your example or edit the input ($something) to properly reflect the inclusive/exclusive-rules of the range function.

Upvotes: 1

KodeFor.Me
KodeFor.Me

Reputation: 13511

This is my sugestion, I hope to work for you:

$something = "1,2,3,4";

$array = Array(Array('a' => 'a', 'x' => $something));

if(preg_match('/(\d+\-\d+(\,?))+/', $array[0]['x'], $m))
{
    $x_values   =   Array();
    $sets       =   explode(',', $m[0]);

    foreach($sets as $set)
    {
        preg_match('/(\d+)\-(\d+)/', $set, $m);
        $start      =   $m[1];
        $end        =   $m[2];

        for($i = $start; $i <= $end; $i++)
        {
            if(in_array($i, $x_values))
            {
                continue;
            }

            array_push($x_values, $i);
        }
    }

    $array[0]['x'] = $x_values;
}
else if(preg_match('/(\d+)\-(\d+)/', $array[0]['x'], $m))
{
    $x_values   =   Array();
    $start      =   $m[1];
    $end        =   $m[2];

    for($i = $start; $i <= $end; $i++)
    {
        array_push($x_values, $i);
    }

    $array[0]['x'] = $x_values;
}
else if(preg_match('/\d+\,\d+/', $array[0]['x'], $m))
{
    $x_values   =   explode(',', $array[0]['x']);
    $array[0]['x'] = $x_values;
}

Upvotes: 0

John Ballinger
John Ballinger

Reputation: 7540

You have

$array = Array(Array('a' => 'a', 'x' => $something));

To modify the contents of 'x' inside this array. (you don't modify $something).

$array[0]['x'] = Array(1,2,3);  

or

$array[0]['x'] = Array(1,2,3,4,5);

Upvotes: 0

Related Questions