1

I want to produce a string like this from an array:

omc_playroom.id,omc_playroom.name,omc_playroom.parentid,
omc_playroom.status,omc_playroom.table_id,omc_playroom.lang_id

I tried this, but it adds omc_playroom only for the first one.

$fields = array('id, name, parentid, status, table_id, lang_id');
$module ='playroom';
$playrooms = getAll($fields, $module);
echo $playrooms;

function getAll($fields, $module){
    $string = '';
    $module_table = 'omc_' . $module;
    foreach ($fields as $field){
       $string .= ",$module_table.$field";
    }
    $string = substr($string, 1); // Remove leading ","
    return $string;
}

This produces:

 omc_playroom.id,name,parentid,status,table_id,lang_id

How can I add omc_playroom in front of each item?

3 Answers 3

2

Well, first, you don't really have an array of items : all you items are in a single string, which is the only element of your $fields array.

Let's extract that item, and remove the spaces that are sometimes present after the ',' separator :

$fields = array('id, name,parentid,status,table_id,lang_id');
$str = $fields[0];
$str = str_replace(' ', '', $str);

Now, we split the $str string, using ',' as a separator :

$items = explode(',', $str);

And loop over the resulting array, adding the prefix to each element :

foreach ($items as & $item) {
    $item = 'omc_' . $item;
}

Finally, we can implode those elements back together, and echo the result :

$new_str = implode(',', $items);
echo $new_str;

And we get :

omc_id,omc_name,omc_parentid,omc_status,omc_table_id,omc_lang_id

Which seems to be what you expected.



But, of couse, if your $fields variable did indeed contain a real proper array :

$fields = array('id', 'name', 'parentid', 'status', 'table_id', 'lang_id');

Things would have been so much easier : you'd just have to loop over that array :

$fields = array('id', 'name', 'parentid', 'status', 'table_id', 'lang_id');
foreach ($fields as & $field) {
    $field = 'omc_' . $field;
}
var_dump($fields);

And you'd get :

array
  0 => string 'omc_id' (length=6)
  1 => string 'omc_name' (length=8)
  2 => string 'omc_parentid' (length=12)
  3 => string 'omc_status' (length=10)
  4 => string 'omc_table_id' (length=12)
  5 => &string 'omc_lang_id' (length=11)
Sign up to request clarification or add additional context in comments.

2 Comments

Also note that probably not experienced developer could have issues with working with $field variable that still points to the last item after the loop.
true ; adding unset($field); after the foreach loop might be a good idea, to avoid any problem later.
1

Try this, as you currntly have an array with only ONE element.

$fields = array('id', 'name', 'parentid', 'status', 'table_id', 'lang_id');

Comments

0

Since you have a "variable delimiter", preg_replace() can do this in just one step.

Code: (Demo)

$fields=['id, name,parentid,status,table_id,lang_id'];
$module='playroom';
$module_table='omc_'.$module;

echo preg_replace('/^|,\K ?/',"$module_table.",$fields[0]);
//  start of string-^
//              OR---^
//                    ^^^^^-match comma, restart match, then match optional space

Output:

omc_playroom.id,omc_playroom.name,omc_playroom.parentid,omc_playroom.status,omc_playroom.table_id,omc_playroom.lang_id

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.