Search code examples
phpsmartysmarty2

in_array() expects parameter 2 to be array, string given in


Got a warning like in_array() expects parameter 2 to be array, string given in

<div class="row">
    <div class="col-sm-6">
        <div class="form-group">
            <label for="kids_age_range">AGe  Range</label>
            <div class="checkbox">
                <label class="control-label" for="k_1_5">
                    <input type="checkbox" name="kids_age_range[]" value="1-5" id="k_1_5" {if in_array('1-5', $job.kids_age_range)} checked{/if} /> 1-5 
                </label>                                    
            </div>
        </div>
    </div>
</div>

Solution

  • If $job.kids_age_range is a single dimensional array you will need to cast the object into an array before you can use it.

    // Cast to an array
    $array = (array) $job.kids_age_range;