3

I have a data array containing these values:

Array
(
    [id] => 1
    [myid] => 9
    [date] => 2014-01-30
    [user] => 17
    [reason] => some text here...
)

And this string which contains numbers referring to the data array indexes:

$columns = '(0) as "id",(1) as "myid",(2) as "date",(3) as "user",(4) as "reason"';

Is it possible to change the numbers enclosed in brackets, including brackets to appropriate value from the array?

I know how to work with (string) preg_replace( (array) $patterns, (array) $replacements, (string) $subject) but quite don't know how to solve this problem.

Ideally the result string could look like this:

'1' as "id",'9'  as "myid",'2014-01-30'  as "date",'17'  as "user",'some text here...'  as "reason"
7
  • Have a look at array_walk() or array_reduce() to build a single string. Commented May 16, 2016 at 8:35
  • Why do you want that result? What is it for? Commented May 16, 2016 at 8:45
  • Why you need to change the string, why not new string from array? Commented May 16, 2016 at 8:46
  • Quasimodo's clone: For Sqlite query @Frayne Konok: I could do it so, this would be the simplest way. Commented May 16, 2016 at 9:02
  • @JohnBoe, But why you want to make it complex?? Commented May 16, 2016 at 9:03

5 Answers 5

2

The solution using preg_replace_callback and str_replace functions:

$columns = '(0) as "id",(1) as "myid",(2) as "date",(3) as "user",(4) as "reason"';

// $arr is your initial array
$result = preg_replace_callback(
                "/(\(\d\)) as \"(\w+?)\",?/i",
                function ($maches) use($arr){
                     return str_replace([$maches[1]], "'". $arr[$maches[2]]. "'", $maches[0]);
                },
                $columns);

print_r($result);

The output:

'1' as "id",'9' as "myid",'2014-01-30' as "date",'17' as "user",'some text here...' as "reason"
Sign up to request clarification or add additional context in comments.

3 Comments

Thank you. I accept your question because I think this is best answer. Nevertheless the Frayne's suggestion in comments is my current solution.
yes, the Frayne's suggestion would be quite reasonable if you are dealing with a real code(production). But I've presented the solution as a solution for your current "abstract" test case. Anyway, thanks
That's great, I also make an answer using some PHP library functions.
2

You can do it with a simple foreach loop:

$info = array(
    'id' => 1,
    'myid' => 9,
    'date' => '2014-01-30',
    'user' => 17,
    'reason' => 'some text here...'
);

$columns = '(0) as "id",(1) as "myid",(2) as "date",(3) as "user",(4) as "reason"';

foreach (array_values($info) as $key => $value) {
    $columns = str_replace(
        '(' . $key . ')',
        str_pad($value, strlen($value) + 2, "'", STR_PAD_BOTH),
        $columns
    );
}

echo $columns;

2 Comments

What if the string starts with $columns = '(0) as "myid",(1) as "id", ?
ha ha ha, There is a lot of questions, But the OP's requirement is resolved .
1

You can do it using just some PHP library functions.

$info = array(
    'id' => 1,
    'myid' => 9,
    'date' => '2014-01-30',
    'user' => 17,
    'reason' => 'some text here...'
);

$columns = '(0) as "id",(1) as "myid",(2) as "date",(3) as "user",(4) as "reason"';

$new_Arr = array();
$column_arr = explode(",", $columns);
foreach($column_arr as $values){
    $arr = explode(" as ", $values);
    $new_Arr[] = "'".$info[trim($arr[1], '"')]."' as ".$arr[1];
}

echo implode(",", $new_Arr) //'1' as "id",'9' as "myid",'2014-01-30' as "date",'17' as "user",'some text here...' as "reason"

2 Comments

complex way but you get it. +1
@RaviHirani, Actually one person did it using str_pad and str_replace, Another person preg_replace_callback, So the left is foreach.
0

Just use a loop with str_replace. Iterate through array, use loop index in brackets as a search string and replace it with corrsponding value.

Comments

0

array_flip is your saviour

Straight from docs

<?php
$input = array("oranges", "apples", "pears");
$flipped = array_flip($input);

print_r($flipped);
?>

The above example will output:

Array
(
    [oranges] => 0
    [apples] => 1
    [pears] => 2
)

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.