2

I'm trying to create an array in PHP that has the structure defined by a string. It will loop through and use the first value as the value and the second value as the quantity. For instance the 1|3 will have the value of 1, 3 times and then loop to the next in the string.

Here is what I have so far -

<?php

$quantity = 10;
$string = '1|3,2|3';
$overall_types = array( );

$types = explode( ',', $string );

for ( $i = 1; $i <= $quantity; $i++ )
{
  $qc = explode( '|', $types[0] );
  $overall_types[$i] = $qc[0];
}

echo '<pre>';
print_r ( $overall_types );
echo '</pre>';

and that gets me

Array
(
    [1] => 1
    [2] => 1
    [3] => 1
    [4] => 1
    [5] => 1
    [6] => 1
    [7] => 1
    [8] => 1
    [9] => 1
    [10] => 1
)

but, I want the result to be

Array
(
    [1] => 1
    [2] => 1
    [3] => 1
    [4] => 2
    [5] => 2
    [6] => 2
    [7] => 1
    [8] => 1
    [9] => 1
    [10] => 2
)

I'm not sure how to easily switch between the exploded values.

Thanks.

4 Answers 4

1

You're not using the repetition count at all from what I see. Achieving this using a straight-forward approach is tricky and probably not necessary when there's a simpler way to do this.

<?php
function buildReps($string) {
    $array = [];
    $overall_types = array( );
    $types = explode( ',', $string );
    foreach ($types as $type) {
         $qc = explode( '|', $type );
         $array = array_merge($array, array_fill(0, $qc[1], $qc[0]));
    }
    return $array;
}

function buildAllReps($string, $quantity) {
     $array = [];

     while (count($array) < $quantity) {
          $array = array_merge($array, buildReps($string));
     } 
     return array_slice($array, 0, $quantity); 
}

$quantity = 10;
$string = '1|3,2|3';

echo '<pre>';
print_r ( buildAllReps($string, $quantity) );
echo '</pre>';    

The first function builds the array once based on the $string defintion. The second one just merges the results from the first one until you reach quantity and then stops and returns the correct quantity of items back.

The above outputs:

Array
(
    [0] => 1
    [1] => 1
    [2] => 1
    [3] => 2
    [4] => 2
    [5] => 2
    [6] => 1
    [7] => 1
    [8] => 1
    [9] => 2
)

Example in https://eval.in/629556

Beware of infinite loops which may occur if the definition supplied by $string does not produce a non-empty array.

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

Comments

0

There's a better way to do string to array in PHP.

You can use either serialize and unserialize or json_encode and json_decode to create strings and then to convert them to array. This is far better than what you would want to implement, because those methods are in the php core, which means they are faster.

http://php.net/serialize

http://php.net/manual/ro/function.json-encode.php

From json string to array, you will need to do something like this:

$json = '{"a":1,"b":2,"c":3,"d":4,"e":5}';

// extra boolean parameter set to true to return array instead of json object
$array = json_decode($json, true);

2 Comments

This either doesn't answer the question or I have really misunderstood what the OP is asking.
He said he needs a way to parse string to array, but you are right, he wanted something more complicated.
0
<?php
$quantity = 10;
$string = '1|3,2|3';
$overall_types = [];

$types = explode( ',', $string );
for($c = 0; $c < $quantity; $c++) {
foreach ($types as $currentType) {
 $qc = explode( '|', $currentType );
 for ($i = 0; $i < $qc[1]; $i++) {
   $overall_types[] = $qc[0];
 }
}
}

$overall_types = array_slice($overall_types,0,$quantity);

echo '<pre>';
print_r ( $overall_types );
echo '</pre>';

This should give you the output that you need.

Comments

0

Not going into micro benchmarking here but you could probably also preg_replace /g the \w|\d+, multipliers by their full string representation and only preg_split the entire string into an array after this step. Like so:

$string = '1|3,2|3';

print_r(str_split(preg_replace_callback('/(\w)\|(\d+),?/', function($a) {
    return str_repeat($a[1], $a[2]);
}, $string)));

Less array handling and potentially faster.

I would however suggest to reconsider the initial string representation and look for a better approach. Why is this format needed if it needs to be converted anyway?

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.