Showing posts with label php array. Show all posts
Showing posts with label php array. Show all posts

Friday, August 25, 2017

jQuery push multiple values in input tag


Just set value to input store it in some temporary variable, append the value you want to store and set the value of input to this temporary variable eg.

<input id="user" type="text" name="user" class="form-control" />
<input id="user_id" type="hidden" name="user_id" value="" />

var oldValue = $("#user_id").val();
var arr = oldValue === "" ? [] : oldValue.split(',');
arr.push(yardval);
var newValue = arr.join(',');
$("#user_id").val(newValue);
 
 

Wednesday, July 31, 2013

Quickly Removing Empty Array Elements in PHP

Different ways of removing empty array slots in PHP.

Removing empty array slots in PHP, leaving holes in the array.

A quick way to remove empty elements from an array is using array_filter without a callback function. This will also remove 0s (zeroes) though.

$myArray = array_filter( $myArray );

Alternatively, array_diff allows you to decide which elements to keep. The following example will only remove empty strings, but keep 0

$myArray = array_diff($myArray, array(''));

Removing empty array slots in PHP, and compacting the array
Both functions leave ‘gaps’ where the empty entries used to be:

$myArray = array( 0, 'red', '', 'blue');

print_r(array_filter($myArray));
Array
(
  [1] => 'red'
  [3] => 'blue'
)

print_r(array_diff($myArray, array('')));
Array
(
  [0] => 0
  [1] => 'red'
  [3] => 'blue'
)

array_slice can remove those gaps:

$myArray = array(0, 'red', '', 'blue');
$myArray = array_filter($myArray);
print_r(array_slice($myArray, 0));
Array
(
  [0] => 'red'
  [1] => 'blue'
)

see whether it is faster than a loop.

One more example

    $data = array(42, "foo", 96, "", 100.96, "php");
   
    // Filter each value of $data through the function is_numeric
    $numeric_data = array_filter($data, "is_numeric");
   
    // Re-key the array so the keys are sequential and numeric (starting at 0)
    $numeric_data = array_values($numeric_data);
   
    // Print out the new, filtered data
    print_r($numeric_data);

The output from above is:

Array
(
    [0] => 42
    [1] => 96
    [2] => 100.96
)

Wednesday, June 19, 2013

Remove duplicate array keys and values with case insensitive.

We have two array.
First array contains some key and values.
Second array contains some keys and values.
I want to merge this two array if duplicate values occurred remove first array duplicate value only.
I want to check key and value is case insensitive.

Please use below code for these requirements.

Code :

<?php
$alname = array(
    "CHE" => "Chennai" ,
    "JKT" => "Jakarta" ,
    "KOL" => "Kolkata" ,
    "MDU" => "Madurai" ,
    "MUM" => "Mumbai" ,
    "SHG" => "Shanghai" );

$alname1 =  array(
    "TKY" => "Tokyo" ,
    "MXI" => "Mexico City" ,
    "DHI" => "Delhi" ,
    "KAR" => "Karachi" ,
    "CHN" => "CheNnai" ,
    "ShG" => "ShaNghai" ,
    "ISB" => "Istanbul" );
   
    echo "<pre>";
    echo "<br/>First Array : <br/>";
    print_r($alname);
    echo "<br/>Second Array : <br/>";
    print_r($alname1);
   
    $arr2 = array_merge($alname,$alname1);
    echo "<br/>Merge first and second array Before remove duplicate values : <br/>";
    print_r($arr2);
   
    foreach($alname as $alkey => $alval)
        {
           $slow_key = strtolower($alkey);
           $slow_val = strtolower($alval);
           $slow_alname1 = unserialize(strtolower(serialize($alname1)));
           $slow_alname1 = array_change_key_case($slow_alname1,CASE_LOWER);
           if(isset($slow_alname1[$slow_key]))
           {
             unset($alname[$alkey]);
           }
          
           if(in_array($slow_val,$slow_alname1))
           {
              unset($alname[$alkey]);
           }
          
        }
        $arr2 = array_merge($alname,$alname1);
        echo "<br/><br/>Merge first and second array After remove duplicate values : <br/><br/>";
        print_r($arr2);
        echo "</pre>";
       
?>


OUTPUT :



Tuesday, May 28, 2013

Convert Array To Object

Use json_encode - convert array to json.
Json_decode - Convert json to PHP array or Object.
If set json_decode  json value , option TRUE comes to array value.
If set json_decode  json value , option FALSE comes to php Object.

<?php
$array = array("UID" => "615371-9834293-3232","code" => "AEPVC1");
$object = json_decode(json_encode($array), FALSE);
echo "<pre>";
print_r($array);
print_r($object);
echo "</pre>";
?>

Output :
 
Array
(
    [UID] => 615371-9834293-3232
    [code] => AEPVC1
)
stdClass Object
(
    [UID] => 615371-9834293-3232
    [code] => AEPVC1
)

Sunday, May 26, 2013

Generate URL-encoded query string (http_build_query) :



If you passed associative array and return result is query string with encoded.

Code:

<?php
$data = array('foo'=>'bar',
              'baz'=>'boom',
              'cow'=>'milk',
              'php'=>'hypertext processor');

echo http_build_query($data) . "\n";
echo http_build_query($data, '', '&amp;');

?>

OutPut:

foo=bar&baz=boom&cow=milk&php=hypertext+processor
foo=bar&amp;baz=boom&amp;cow=milk&amp;php=hypertext+processor