Tag

types of php arrays

Browsing

Function to find particular value or key in the array for php – In_array functions

Function to find particular value or key in the array for php – In_array

In_array Functions is used to find the value in the array list

using this function you can find the value is present or not in array queue.

syntax: In_array();

for example

have to find “Developerdesks” in the given array

<?php

$var = array(‘raj’,’sekar’,’developer’,’desks’,'php','html','css','developerdesks','designer');

if(in_array('developerdesks', $var, ))
{
echo "found Developerdesks";
}
else
{
echo "not found";

}

?>

Explanation:

If you want find a particular key or value in the very long array for any action..

<?php

$var = array(‘raj'=>'1233','sekar'=>'434','developer'=>'asd','desks'=>'ded','developerdesks'=>'free','designer'=>'dsa');

?>

here ‘raj’ is ‘key’ and  ‘1233’ is ‘value’

you can search anything value or key using IN_ARRAY();

Similar to In_array();

2. array_search() 

If we have a very large array values in Indexed or Associative array, we have to search or filter the particular array value or key.

Array_Search function is used to find the value.

This  function used to  search an array for a value and returns the key.

You can see Explanation Here

How can we print array with in array using for-each loop – Three dimensional array

A multidimensional array is an array containing one or more arrays.

Three-Dimensional array

For three dimensional array u have to use two for loop condition
find 0 indexed array value and then 0 indexed array value

<?php

$developedesks = array(
"Skills" => array
(
"php" => 80,
"css" => 85,
"html" => 80
),
"design" => array
(
"photoshop" => 70,
"flash" => 75,
"indesign" => 72
),
"framework" => array
(
"wordpress" => 85,
"drupal" => 75,
"joomla" => 70
)
);

$count = count($developedesks);
echo "$count<br/>";
foreach($developedesks  as $develop => $desks)
{

foreach($desks as $key =>$value)
{
echo "$develop &nbsp; $key &nbsp; $value<br/>";
}

}

?>

Explain

First foreach consider skill as $develop and php =>80 as $desks

Second For each consider $key as php and $ value as 80

so the $develop –> indexed as skill, $key–> indexed as php and $value indexed as 80

simple foreach concept worked

Output

Skills  php  80
Skills  css    85
Skills  html   80
design  photoshop 70
design  flash    75
design  indesign   72
framework wordpress 85
framework drupal   75
framework joomla   70

 

Similar like three dimensional array

Multidimensional Array concept in Php

A multidimensional array is an array containing one or more arrays.

Its is solution for to know how to handle a array with in array
Example
1.Two-dimensional array
2.Three-dimensional array

Here You can see Exaplaination

How We used Multidimensional Array concept in Php?

Multidimensional Array concept in Php

A multidimensional array is an array containing one or more arrays.

Its is solution for to know how to handle a array with in array
Example
1.Two-dimensional array
2.Three-dimensional array

1.Two-dimensional array

<?php

$develop= array(
array("developer",php,html),
array("desks",php5,html5),
array("raja",css,ajax),
array("sekar",jquery,drupal)
);

$count= count($develop);
echo "$count";

 for ($row = 0; $row < $carss; $row++) {
echo "<p><b>Level $row</b></p>";
echo "<ul>"; 
for ($col = 0; $col < 3; $col++) {
echo "<li>".$cars[$row][$col]."</li>";
}
echo "</ul>";
}

?>

Output

Indexed Level 0
developer
php
html

Indexed Level 1
desks
php5
html5

Indexed Level 2
raja
css
ajax

Indexed Level 3
sekar
jquery
drupal

Explanation:

First For loop consider all array values inside the array  –> array(“developer”,php,html) as 0 indexed

Second For loop consider inside values –> Developer as 0 indexed

For two dimensional array u have to use two for loop condition

find 0 indexed array value and then 0 indexed array value

simple example

<?php

echo $develop[0][0].": langauge 1: ".$develop[0][1].", language 2: ".$develop[0][2].".";
echo $develop[1][0].": langauge 1: ".$develop[1][1].", langauge 2: ".$develop[1][2].".";
echo $develop[2][0].": langauge 1: ".$develop[2][1].", langauge 2: ".$develop[2][2].".";
echo $develop[3][0].": langauge 1: ".$develop[3][1].", langauge 2: ".$develop[3][2].".";

?>

Example For Three-Dimensional array