0

How to split up the value to an array. I have the whmcs api data it looks like 1,2,3,4,5. I need to split up the value to an array value suggest any solution

My api data

Array
(
[result] => success
[totalresults] => 2
[promotions] => Array
    (
        [promotion] => Array
            (
                [0] => Array
                    (
                        [id] => 6
                        [code] => 0UP3NU5J7J
                        [type] => Percentage
                        [recurring] => 0
                        [value] => 15.00
                        [cycles] => One Time,Monthly,Quarterly,Semi-Annually,Annually,Biennially,Triennially,1Years
                        [appliesto] => 1,2,3
                        [requires] => 1,2,3
                        [requiresexisting] => 0
                        [startdate] => 2018-10-22
                        [expirationdate] => 2018-10-25
                        [maxuses] => 0
                        [uses] => 0
                        [lifetimepromo] => 0
                        [applyonce] => 0
                        [newsignups] => 0
                        [existingclient] => 0
                        [onceperclient] => 0
                        [recurfor] => 0
                        [upgrades] => 0
                        [upgradeconfig] => a:4:{s:5:"value";s:4:"0.00";s:4:"type";s:0:"";s:12:"discounttype";s:10:"Percentage";s:13:"configoptions";s:0:"";}
                        [notes] => 
                    )
     )
  )
)

My code is

   @foreach($response['promotions']['promotion'][0] as $key => $value)
     @if($key == 'appliesto')
            {{$var=$value}}
     @endif
  @endforeach

I need to split the "appliesto" value as an array.Please suggest any solution

Sanu0786
  • 571
  • 10
  • 15
sharmila
  • 175
  • 2
  • 19
  • 2
    use the `explode` function. http://php.net/manual/en/function.explode.php – Wreigh Oct 23 '18 at 08:24
  • Also, you shouldn't place logic like that into the template. The much cleaner approach would be to have a Promotion object that holds the data, with methods such as `getCycles() :array`, ie methods that will return the value in the required form to make things more structured and reusable. – Bananaapple Oct 23 '18 at 08:27

2 Answers2

2

just use the explode function like :

$apelistoArray = explode(',',$value);

This will return an array of the values that the appliesto field has so you need to parse them as an array.

This new array has to be parsed inside a loop before you use the curly brackets to echo the values in the blade.

So you have to do something like below:

@foreach($apelistoArray as $data)
{{$data}}
@endforeach
pr1nc3
  • 8,108
  • 3
  • 23
  • 36
2

No need of loop:-

$response['promotions']['promotion'][0]['appliesto'] = explode(',',$response['promotions']['promotion'][0]['appliesto']);

But if promotions can have multiple child array then do:-

@foreach($response['promotions']['promotion'] as &$value)
   $value['appliesto'] = explode(',',$value['appliesto']);
@endforeach

Reference:- explode()

Alive to die - Anant
  • 70,531
  • 10
  • 51
  • 98
  • @foreach($response['promotions']['promotion'] as $value) $value['appliesto'] = explode(',',$value['appliesto']); {{$value['appliesto']}} @endforeach will you please suggest what i made a mistake – sharmila Oct 23 '18 at 08:31
  • Based on what you do there `$value['appliesto']` is an array and you try to echo it as a string. You should parse it before you use the {{ }}, those are for echoing a string – pr1nc3 Oct 23 '18 at 08:41
  • will you please suggest or send any example – sharmila Oct 23 '18 at 11:36
  • @sharmila I am unable to understand what you finally trying to do?do you want to show `appliesto` all values one-by-one on your view? – Alive to die - Anant Oct 23 '18 at 12:24