Header menu

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

Saturday, 19 October 2013

Assign a php array to a jquery variable

To assign php array to jquery variable use

var jArray= <?php echo json_encode($phpArray ); ?>;

jArray  is the jquery variable which hold array in jquery and
$phpArray contains my php array ...
----------------------------------------------------------------------------------------------
For more details run below example:

<html>
<head>

</head>
<body>

<b> Hello .. Today we are assigning a php array to jquery variable <b>
<?php $phpArray = array(
          0 => "Mon",
          1 => "Tue",
          2 => "Wed",
          3 => "Thu",
          4 => "Fri",
          5 => "Sat",
          6 => "Sun",

    )?>
</body>
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.10.2/jquery.min.js"></script>
<script type="text/javascript" >
$(document).ready(function(){
     var jArray= <?php echo json_encode($phpArray ); ?>;
     for(var i=0;i<6;i++){
     alert(jArray[i]);
    }
    alert("Refresh again to see Array-Data    ")
});
</script>
</html>


Thursday, 19 September 2013

Implode and Explode function of Php

Implode and Explode function of Php


<?php
// function 1:  implode()
echo "<h1>IMPLODE</h1>";
$arr=array("test1","test2","test3","test4","test5","test6","test7","test7");
echo "<pre>";  print_r($arr); echo "</pre>";

//implode(glue, pieces)

echo "Array with glue=none";
echo "</br>";echo "</br>";
$str=implode('', $arr);
echo $str;

echo "</br>";echo "</br>";
echo "Array with  ',' as glue to get comma separarted value from array ";
echo "</br>";echo "</br>";
$str=implode(',', $arr);
echo $str;
echo "</br>";echo "</br>";
// implode() function is used to join array elements and return string

// function 2:  explode(delimiter, string)
echo "<h1>EXPLODE</h1>";
echo $str="This is my string i am going to explode";
echo "</br>";echo "</br>";
echo "First using space as delimiter";
echo "<pre>";  print_r($arr); echo "</pre>";
$arr=explode(' ',$str);

echo "</br>";echo "</br>";
echo "Now using t as delimiter";
$arr=explode('t',$str);
echo "<pre>";  print_r($arr); echo "</pre>";
// explode breaks string into array where delimiter comes

?>

Array() / Array_fill() / Array_Keys() in php

Array() / Array_fill() / Array_Keys() in php

<?php
// function 1:  array()
$arr=array();
echo "Empty array created";
echo "<pre>";  print_r($arr); echo "</pre>";
// array() create an empty array

// function 2:  array_fill(start_index, num, value)
$arr=array_fill(0, 5, "test");
echo "Array filled with value";
echo "<pre>";  print_r($arr); echo "</pre>";
$arr[1]="failed";
$arr[3]="failed";
echo "Array after changes";
echo "<pre>";  print_r($arr); echo "</pre>";
// array_fill() fill the array with value given in parameter

// function 3:  arrays_keys(array,value)
echo "Keys where value is test in array";
$keyz=array_keys($arr,"test");
echo "<pre>";  print_r($keyz); echo "</pre>";
// arrays_keys() return the array of keys where value is same as parameter

?>

Wednesday, 21 August 2013

How to send an array in url in php ?

File.php  (This file contain array )

<?php

$data = array('foo'=>'bar',
              'baz'=>'boom',
              'cow'=>'milk',
              'php'=>'script');

$arr=http_build_query($data);

?>
<a href="nextpage.php?<?php echo $arr; ?>">Sendarray</a>


--------------------------------------------------------------------------------------------------------------------------------

nextpage.php (This file will receive array and print it)


<?php
echo "<pre>";
print_r($_GET);
echo "</pre>";
?>

Tuesday, 20 August 2013

How to preview array in Php ?

Array

<?php

$arr=array(1,2,3,4,5);
echo "<pre>";
print_r($arr);
echo "</pre>";

?>