shin
shin

Reputation: 32721

How to add a string in front of each array

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?

Upvotes: 1

Views: 1964

Answers (3)

mickmackusa
mickmackusa

Reputation: 47900

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

Upvotes: 0

Pascal MARTIN
Pascal MARTIN

Reputation: 401022

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)

Upvotes: 2

vbence
vbence

Reputation: 20333

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

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

Upvotes: 1

Related Questions