1

I want to match string with array. I am retrieving the status code from status table in the database...I have a json file with code and name...Based on the status code returned from db i need to match this code in json file and display the status name in foreach

Please anyone help

Thanks

statuscodes array

[
 {
     "code":"0",
     "name":"AB"
   },
   {
     "code":"1",
     "name":"CD"
   },
   {
     "code":"2",
     "name":"XY"
   },
   {
     "code":"10",
     "name":"EF"
   },
   {
     "code":"12",
     "name":"FG"
   }
 ]

 <?php $findproducts = \DB::table('status')->select('id', 'status_name', 'ab', 'status_code','created_at')->where('ab', $track->ab)->orderBy('id', 'DESC')->get(); ?>
                                         

String Pos....

 @foreach($findproducts as $pr) 
  @if($statuscodes != null)
   @foreach ($statuscodes as $stcode) 
     @if (strpos($pr->status_code, $stcode['code']) !== FALSE) 
       {{ $stcode['name']." " .$stcode['code'] }}                                                             
     @endif
   @endforeach
@endif
@endforeach

The output which i am getting from above is
CD 1 XY 2 FG 12
CD 1 AB 0 EF 10
XY 2
CD 1
AB 0
Expected Output should be
FG 12
EF 10
XY 2
CD 1
AB 0

6
  • 1
    You can just change orderBy('id', 'DESC') to orderBy('code', 'DESC')? Commented Apr 1, 2021 at 20:24
  • @Johannes i tried but i am getting same output Commented Apr 1, 2021 at 20:28
  • Your outer loop is looping through all products, so if first product has status codes 1, 2 and 12 it will output these. If I got your expected output correctly you want to output the statuscodes array elements and not the products, correct? Commented Apr 1, 2021 at 20:38
  • @Johannes yes exactly...if the code is 12 i just wanna print status name FG thats it....but its looping through 1 and next 2 then 12 which is wrong Commented Apr 1, 2021 at 20:45
  • I am absolutely confused why are you doing strpos($pr->status_code, $stcode['code']). Can you explain more what you want to do as your example is not understandable by me... What do you really want to do ? Also what version of PHP are you using ? Commented Apr 1, 2021 at 21:06

2 Answers 2

1

Why are you doing:

@if (strpos($pr->status_code, $stcode['code']) !== FALSE)
    {{ $stcode['name']." ".$stcode['code'] }}
@endif

Instead of:

@if ($pr->status_code === $stcode['code'])
    {{ $stcode['name']." ".$stcode['code'] }}
@endif

I think your problem is strpos (I am not sure why you are using it). As the documentation says: Prior to PHP 8.0.0, if needle is not a string, it is converted to an integer and applied as the ordinal value of a character. This behavior is deprecated as of PHP 7.3.0, and relying on it is highly discouraged. Depending on the intended behavior, the needle should either be explicitly cast to string, or an explicit call to chr() should be performed.

I am not sure if your needle is being used as a number so it is messing things up...

Also, when the if is true and you print what you want, you can break the foreach so you don't waste time nor effort on iterating something that you know will never be true again until something changes. You can do so using @break.

@if($statuscodes != null)
    @foreach($findproducts as $pr)
        @foreach ($statuscodes as $stcode)
            @if ($pr->status_code === $stcode['code'])
                {{ $stcode['name']." ".$stcode['code'] }}
                @break
            @endif
        @endforeach
    @endforeach
@endif

See also that I have switched the foreach with the first if, because if you do not have $statuscodes, why are you going to iterate $findproducts and skip everything ? Don't even iterate it...

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

1 Comment

yeah i tried with this finally got the desired result tq
1

try this , it is more cleaner

first convert status code to collection and convert it so collection of key=> value pairs

this should be the status code collection enter image description here

then look up your key using simple foreach loop.

Note I am not sure what is returned from database so you can change the code to suite the desired output.

also try to avoid nested loops as much as you can

$status_code = collect(json_decode('[
 {
     "code":"0",
     "name":"AB"
   },
   {
     "code":"1",
     "name":"CD"
   },
   {
     "code":"2",
     "name":"XY"
   },
   {
     "code":"10",
     "name":"EF"
   },
   {
     "code":"12",
     "name":"FG"
   }]'
 ,true))->pluck('code','name');



@foreach($findproducts as $pr)
{{$pr->status_code ." ". $status_code[$pr->status_code]}}
@endforeach

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.