Compare element of string array in php

Fadly Dzil

I have two arrays both having elements in string type. Example :

First Array

$default_complaint = array("Login", "Printer", "Monitor", "Computer", 
                           "Network", "Other");                      

Second Array

$selected_complaint = explode(" ", $s['kindof_request']);
// Ex : it return like this => array ("Login", "Printer", "Monitor");

Now, how can I create the checkboxes that ticked in html by comparing that two arrays given above. So, I create like this:

<?php 
$default_complaint = array("Login", "Printer", "Monitor", "Computer", "Network", "Lain-lain");                      
$selected_complaint = explode(" ", $s['kindof_request']);

foreach ($default_complaint as $dc) {
    foreach ($selected_complaint as $sc) {

    $check = strcmp($dc, $sc) ;
    if ($check == 0) { //True
          echo '<input type="checkbox" checked="checked">'. "$sc" ."<br />";
        } else{ //false
          echo '<input type="checkbox">'. "$dc"."<br />";
        }

    }
}
?>

My code still give me weird result. So, How to create like this, => (0) meaning checked.

(0)Login   (0)Printer   (0)Monitor   ()Computer   ()Network   ()Others   
nlu

You have two loops, but you only want to loop over the first array. The second one is only used for checking.

One possibility for the loop is:

foreach ($default_complaint as $dc) {

    if (array_search($dc, $selected_complaint) !== FALSE) {
        echo '<input type="checkbox" checked="checked">'. "$dc" ."<br />\n";
    } else{
        echo '<input type="checkbox">'. "$dc"."<br />\n";
    }
}

이 기사는 인터넷에서 수집됩니다. 재 인쇄 할 때 출처를 알려주십시오.

침해가 발생한 경우 연락 주시기 바랍니다[email protected] 삭제

에서 수정
0

몇 마디 만하겠습니다

0리뷰
로그인참여 후 검토

관련 기사

분류에서Dev

PHP - Compare a string to all in array and only echo once (unique)

분류에서Dev

Create an Array and get Element, or part of String, in One Line - PHP

분류에서Dev

PHP sort array of associative arrays by element size / string size

분류에서Dev

PHP - Format Date Array and Compare

분류에서Dev

Compare array values with same key in PHP

분류에서Dev

Finding an index of an element in a string array

분류에서Dev

SQL comment with array element PHP

분류에서Dev

php array_diff compare arrays with new array

분류에서Dev

Checking if a string contains any part of an array element

분류에서Dev

PHP: Check if string is part of an array

분류에서Dev

Compare one array with another and replace missing values php

분류에서Dev

PHP foreach previous element with string key

분류에서Dev

How to delete an element inside a multidimensional array in PHP?

분류에서Dev

QUERY String to php array and log to file

분류에서Dev

PHP in_array 및 String 포함

분류에서Dev

PHP in_array 및 String 포함

분류에서Dev

PHP array, C# (string, class)

분류에서Dev

PHP array, C# (string, class)

분류에서Dev

Slice each string-valued element of an array in Javascript

분류에서Dev

Generate an array in PHP of random number not close to the X previous element

분류에서Dev

Adding multiple returned MySQL rows to the same array key element in PHP

분류에서Dev

How to compare nextLine() to a String

분류에서Dev

Compare objects in an array

분류에서Dev

PHP array into individual php string, from SQL data

분류에서Dev

JavaScript compare string using Regex

분류에서Dev

How to compare two string arrays?

분류에서Dev

Swift CoreData compare string with entities

분류에서Dev

How to check if an array contains entry that starts with a string in PHP

분류에서Dev

check ping (in Milliseconds) with php and compare

Related 관련 기사

뜨겁다태그

보관