2

I want to replace $reqcolaborate to $namepar, I have this controller:

$reqcolaborate        =   DB::table("pra_kpis")
                          ->distinct()
                          ->pluck("kpi_parameters_id");
$namepar              = DB::table('kpi_parameters')->pluck('name');
$idpar                = DB::table('kpi_parameters')->pluck('id');

@foreach ($reqcolaborate as $parreq)                         
         <option value="{{$parreq}}">
         {{$parreq = str_replace($idpar, $namepar, $parreq)}}
         </option>
@endforeach

for example data

$namepar = array ('MFS active user', '#4g active users','VOLTE');
$idpar   = array (1,2,12);
$reqcolaborate = array (1,2,12);

but, why the result in dropdown like this

<option>MFS active user</option>
<option>#4g active users</option>
<option>MFS active user#4g active users</option> 

i want the result in dropdown like this

<option>MFS active user</option>
<option>#4g active users</option>
<option>VOLTE</option>
8
  • 1
    (I don't use Laravel) what happens when you remove $parreq = in your foreach()? You don't need the variable declaration right? Commented Nov 22, 2017 at 3:05
  • You could define a relationship to access it. laravel.com/docs/5.5/eloquent-relationships. str_replace is replacing "12" with "1" & "2" Commented Nov 22, 2017 at 3:25
  • i am not getting your question can you tell what is your expected output Commented Nov 22, 2017 at 6:34
  • the result in dropdown Commented Nov 22, 2017 at 6:35
  • what should be the value of option and text of option can you post? Commented Nov 22, 2017 at 6:36

1 Answer 1

1

As commented by Matts under the question, str_replace() will be mangling your intended replacements by targetting substrings instead of fullstrings. When you want to replace whole strings without using regular expressions with anchors, you can write your own associative "lookup" array using $idpar and $namepar.

Here's a demonstration:

$namepar = array ('MFS active user', '#4g active users','VOLTE');
$idpar   = array (1,2,12);
$reqcolaborate = array (1,2,12);
$assocpar=array_combine($idpar,$namepar);  // write an associative array to relate $idpar with $namepar

foreach ($reqcolaborate as $parreq){
     echo $assocpar[$parreq],"\n";
}

Output:

MFS active user
#4g active users
VOLTE

In your controller (again, I don't use Laravel), you can write something like this:

$reqcolaborate = DB::table("pra_kpis")
                     ->distinct()
                     ->pluck("kpi_parameters_id");
$lookup        = array_combine(
                     DB::table('kpi_parameters')->pluck('id'),
                     DB::table('kpi_parameters')->pluck('name')
                 );

@foreach ($reqcolaborate as $parreq)                         
    <option value="{{$parreq}}">
        {{ $lookup[$parreq] }}
    </option>
@endforeach

Or if there is a chance that an id won't exist in the lookup array, perhaps:

@foreach ($reqcolaborate as $parreq)                         
    <option value="{{$parreq}}">
        {{ isset($lookup[$parreq]) ? $lookup[$parreq] : 'Whoops, replacement not found in lookup array -- do something!' }}
    </option>
@endforeach
Sign up to request clarification or add additional context in comments.

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.