Search This Blog

Showing posts with label arrays. Show all posts
Showing posts with label arrays. Show all posts

checking if an element is in array or not using in_array() in php

element_in_array.php :

<?php
// Checking if an Element Is in Array or not

$num=array(1,2,3,4,5,6,7,8,9,10);

$element=$_POST['element'];

if (in_array($element, $num)) {
echo 'element found';
}
else
{
echo 'element not found';
}


?>

============================================================
element_in_array_index.php :

<center><b>here checks data is in array or not</b><br><br>
<center><form action="element_in_array.php" method="POST">
<input type="textbox" name="element">
<input type="submit" name="submit">
</form>

============================================================
input :

1) give the array values in text box

2)  click in button

ex :

array :$num=array(1,2,3,4,5,6,7,8,9,10);

here 0-9 array indexes
and
1-10 array values
 
============================================================
output :

1) element found

2) element not found

check array index or key with user inputs in php

check_array_key.php :

<center>
<?php

$num1=array(1,2,3,4,5,6,7,8,9,10,'a'=>'A');
$key=@$_POST['key'];

if (array_key_exists($key, $num1))
{
echo 'array index/key :<b> '.$key.' </b> found<br>';
}
else
{
echo 'array index/key :<b> '.$key.' </b> not found<br>';
}

?>
==========================================================

check_array_index.php :

<center><b>here checks index is in array or not</b><br><br>
<form action="check_array_key.php" method="POST">
<input type="textbox" name="key">
<input type="submit" name="submit">
</form>

============================================================
input :

1) give the array index value in text box
2) click on button

ex :

$num1=array(1,2,3,4,5,6,7,8,9,10,'a'=>'A');

here "0-9" and "a" are  indexes

"1-10" and "A" are values

============================================================

output :

array index/key : not found

array index/key : 1 found

delete array elements with unset() in php

<?php

$num=array(1,2,3,4,5,6,7,8,9,10);
echo '<b>array before delete elements :</b>';
foreach($num as $num1)
{
echo ' '.$num1;
}
echo '<br><br>';

//delete array element

unset($num[3]);
unset($num[0],$num[1],$num[2]);
echo '<b>array after delete elements (1,2,3,4) :</b>';
foreach($num as $num1)
{
echo ' '.$num1;
}


?>

output :

array before delete elements : 1 2 3 4 5 6 7 8 9 10

array after delete elements (1,2,3,4) : 5 6 7 8 9 10

change array size using array_pad() in php

<?php

$num=array(1,2,3,4,5,6,7,8,9,10);
echo '<b>array before grow elements :</b>';
foreach($num as $num1)
{
echo ' '.$num1;
}
echo '<br><br>';

$num = array_pad($num, 20,'a') ; //only additionally adding elements have 'a' value

// $num = array_pad($num, 20,'') ; additionally adding elements have empty

echo '<b>array after grow elements :</b>';
foreach($num as $num1)
{
echo ' '.$num1;
}


?>


output :

array before grow elements : 1 2 3 4 5 6 7 8 9 10

array after grow elements : 1 2 3 4 5 6 7 8 9 10 a a a a a a a a a a

 note :

1) just modify  " $num = array_pad($num, 20,'a') ;  " as "  $num = array_pad($num, 20,'') ; "  for empty
2) that means array index raise but with empty values

array to string in php

<?php

$num1=array(1,2,3,4,5,6,7,8,9,10);
echo '<b>the array :</b>';
foreach($num1 as $num)
{
echo ' '.$num;
}


$string = join(',', $num1);
echo '<br><b>the string :</b>'.$string;

?>

output :

the array : 1 2 3 4 5 6 7 8 9 10

the string :1,2,3,4,5,6,7,8,9,10

array reduce with array_splice() in php

<?php

$num=array(1,2,3,4,5,6,7,8,9,10);
echo '<b>original array :</b>';
foreach($num as $num1)
{
echo ' '.$num1;
}
$num=array(1,2,3,4,5,6,7,8,9,10);
echo '<br><br><b>array after reduce 1 element :</b>';
$num = array_splice($num, 1) ;
foreach($num as $num1)
{
echo ' '.$num1;
}
$num=array(1,2,3,4,5,6,7,8,9,10);
echo '<br><br><b>array after reduce 2 elements :</b>';
$num = array_splice($num, 2) ;

foreach($num as $num1)
{
echo ' '.$num1;
}

$num=array(1,2,3,4,5,6,7,8,9,10);
echo '<br><br><b>array after reduce 3 elements :</b>';
$num = array_splice($num, 3) ;
foreach($num as $num1)
{
echo ' '.$num1;
}
$num=array(1,2,3,4,5,6,7,8,9,10);
echo '<br><br><b>array after reduce 4 elements :</b>';
$num = array_splice($num, 4) ;

foreach($num as $num1)
{
echo ' '.$num1;
}

$num=array(1,2,3,4,5,6,7,8,9,10);
echo '<br><br><b>array after reduce 5 elements :</b>';
$num = array_splice($num, 5) ;
foreach($num as $num1)
{
echo ' '.$num1;
}
$num=array(1,2,3,4,5,6,7,8,9,10);
echo '<br><br><b>array after reduce 6 elements :</b>';
$num = array_splice($num, 6) ;

foreach($num as $num1)
{
echo ' '.$num1;
}

?>

output :

original array : 1 2 3 4 5 6 7 8 9 10

array after reduce 1 element : 2 3 4 5 6 7 8 9 10

array after reduce 2 elements : 3 4 5 6 7 8 9 10

array after reduce 3 elements : 4 5 6 7 8 9 10

array after reduce 4 elements : 5 6 7 8 9 10

array after reduce 5 elements : 6 7 8 9 10

array after reduce 6 elements : 7 8 9 10

add one arry to another array or append array in php

<?php
echo '<b>the first array :</b>';
$num1=array(1,2,3,4,5,6,7,8,9,10);
foreach($num1 as $num)
{
echo ' '.$num;
}
echo '<br><b>the second array :</b>';
$num2=array(11,12,13,14,15,16,17,18,19,20);
foreach($num2 as $num)
{
echo ' '.$num;
}
echo '<br><br><b>append the first array to the second array : </b>';

$num3 = array_merge($num1, $num2);

foreach($num3 as $num)
{
echo ' '.$num;
}

?>

output :

the first array : 1 2 3 4 5 6 7 8 9 10
the second array : 11 12 13 14 15 16 17 18 19 20

append the first array to the second array : 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20

for each statements with arrays in php

<?php


$birds= array('Parrots' =>
array('Kakapo','Kea ','Vernal Hanging Parrot'),

'Cuckoos and Turacos'=>
array('Purple-crested Turaco','Grey Go-away-bird','Great Blue Turaco'));
//display all
print_r($birds);


//for new line
echo '<br><br><br>';

// with foreach() display only main array
foreach($birds as $category => $sub){

echo '<b>'.$category.'<br></b>';
}

//for new line
echo '<br><br>';

// with foreach() display main array and sub arrays
foreach($birds as $category => $sub1){
echo '<b>'.$category.'<br></b>';
foreach($sub1 as $sub2)
echo $sub2.'<br>';
}
?>

output ;

Array ( [Parrots] => Array ( [0] => Kakapo [1] => Kea [2] => Vernal Hanging Parrot ) [Cuckoos and Turacos] => Array ( [0] => Purple-crested Turaco [1] => Grey Go-away-bird [2] => Great Blue Turaco ) )


Parrots
Cuckoos and Turacos


Parrots
Kakapo
Kea
Vernal Hanging Parrot
Cuckoos and Turacos
Purple-crested Turaco
Grey Go-away-bird
Great Blue Turaco

multi dimensional arrays in php

<?php


$birds= array('Parrots' =>
array('Kakapo','Kea ','Vernal Hanging Parrot'),

'Cuckoos and Turacos'=>
array('Purple-crested Turaco','Grey Go-away-bird','Great Blue Turaco'));

print_r($birds);

echo '<br><br><br>';

echo $birds['Parrots'][0].'<br>';
echo $birds['Parrots'][1].'<br>';
echo $birds['Parrots'][2].'<br>';


echo '<br><br><br>';

echo $birds['Cuckoos and Turacos'][0].'<br>';
echo $birds['Cuckoos and Turacos'][1].'<br>';
echo $birds['Cuckoos and Turacos'][2].'<br>';


?>


output :
Array ( [Parrots] => Array ( [0] => Kakapo [1] => Kea [2] => Vernal Hanging Parrot ) [Cuckoos and Turacos] => Array ( [0] => Purple-crested Turaco [1] => Grey Go-away-bird [2] => Great Blue Turaco ) )


Kakapo
Kea
Vernal Hanging Parrot



Purple-crested Turaco
Grey Go-away-bird
Great Blue Turaco


associative arrays in php

<?php

$alphabate=array('A'=>'APPLE','a'=>'apple','B'=>'BALL','b'=>'ball','C'=>'CAT','c'=>'cat','D'=>'DOLL','d'=>'doll','E'=>'ELEPHANT','e'=>'elephant');


print_r($alphabate);

// '<BR>' USE FOR NEW LINE
// 'A' is not equal to 'a' in arrays index
// '.' use for concatenation

echo '<br><br><br>'.
$alphabate['A'].'<br>';
echo $alphabate['a'].'<br>';
echo $alphabate['B'].'<br>';
echo $alphabate['b'].'<br>';
echo $alphabate['C'].'<br>';
echo $alphabate['c'].'<br>';
echo $alphabate['D'].'<br>';
echo $alphabate['d'].'<br>';
echo $alphabate['E'].'<br>';
echo $alphabate['e'].'<br>';

?>

output :

Array ( [A] => APPLE [a] => apple [B] => BALL [b] => ball [C] => CAT [c] => cat [D] => DOLL [d] => doll [E] => ELEPHANT [e] => elephant )


APPLE
apple
BALL
ball
CAT
cat
DOLL
doll
ELEPHANT
elephant

arrays display in php

<?php

$alphabate=array('A','B','C','D','E');

print_r($alphabate);
// '<BR>' USE FOR NEW LINE
echo '<br>'.$alphabate[0].'<br>';
echo $alphabate[1].'<br>';
echo $alphabate[2].'<br>';
echo $alphabate[3].'<br>';
echo $alphabate[4];

?>

output :
Array ( [0] => A [1] => B [2] => C [3] => D [4] => E )
A
B
C
D
E

array() in php

<?php

$food=array("pizza","rice");

echo "$food[0]".'<br>';

echo "$food[1]";

?>

output :

pizza
rice

Share Links

Related Posts Plugin for WordPress, Blogger...