IT Community - Software Programming, Web Development and Technical Support

Using Arrays in PHP

This is a discussion on Using Arrays in PHP within the PHP Programming forums, part of the Web Development category; Remove an Other Row of Array <? //This is a code remove an other row of array //$mang is an ...


Go Back   IT Community - Software Programming, Web Development and Technical Support > Web Development > PHP Programming

Register FAQ Members List Calendar Mark Forums Read
  #101 (permalink)  
Old 08-28-2007, 08:14 AM
Sabari Sabari is offline
D-Web Genius
 
Join Date: Jul 2007
Posts: 1,008
Sabari is on a distinguished road
Default Re: Remove an Other Row of Array

Remove an Other Row of Array

<?
//This is a code remove an other row of array
//$mang is an array, $id is index of row in array
function remove($mang,$id)
{
$flag = false;
$count = count($mang);
$i = 0;
while ($flag!=true && $i<$count) {
if ($i==$id) {
unset($mang[$i]);
$flag = true;
$index = $i;
}
else {
if ($temp) {
array_push($temp,$mang[$i]);
}
else
{
$temp = array($mang[$i]);
}
}
$i++;
}
if ($flag) {
while ($index<$count)
{
if ($temp) {
array_push($temp,$mang[$index+1]);
}
else
{
$temp = array($mang[$index+1]);
}
$index++;
}
}
array_pop($temp);
return $temp;
}

$arr = array(array("id"=>"1","name"=>"b"),array("id"=>"2" ,"name"=>"d"));
array_push($arr,array("id"=>"3","name"=>"f"));
array_push($arr,array("id"=>4,"name"=>"g"));
$arr = remove($arr,2);
echo "<pre>";
print_r($arr);
?>


OutPut:

Array
(
[0] => Array
(
[id] => 1
[name] => b
)

[1] => Array
(
[id] => 2
[name] => d
)

[2] => Array
(
[id] => 4
[name] => g
)

)
__________________
Thanks & Regards
Sabari...
Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
Sponsored Links
  #102 (permalink)  
Old 08-28-2007, 08:19 AM
Sabari Sabari is offline
D-Web Genius
 
Join Date: Jul 2007
Posts: 1,008
Sabari is on a distinguished road
Default Re: Multidimensional array in a Simple Mono dimensional Array

Multidimensional array in a Simple Mono dimensional Array

<?
function flatten_array($value, $key, &$array)
{
if (!is_array($value))
array_push($array,$value);
else
array_walk($value, 'flatten_array', &$array);

}

$oldarray = array
(
1 => array(1,2),
2 => array(3,4)
);
$newarray = array();
array_walk($oldarray, 'flatten_array', &$newarray);
echo "<pre>";
print_r($oldarray);
print_r($newarray);
echo "</pre>";
?>


OutPut:

Array
(
[1] => Array
(
[0] => 1
[1] => 2
)

[2] => Array
(
[0] => 3
[1] => 4
)

)
Array
(
[0] => 1
[1] => 2
[2] => 3
[3] => 4
)
__________________
Thanks & Regards
Sabari...
Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #103 (permalink)  
Old 08-28-2007, 08:24 AM
Sabari Sabari is offline
D-Web Genius
 
Join Date: Jul 2007
Posts: 1,008
Sabari is on a distinguished road
Post Re: Using Arrays in PHP

if you want to sort arrays like this?

1 --- 2
1 --- 3
1 --- 4
2 --- 3
2 --- 4
3 --- 4

simple just use this below code:
$aInt = array('1','2','3','4');

<?
$aInt = array('1','2','3','4');
$NumInt = count($aInt);
for($j=0; $j<=$NumInt-1; $j++)
{
$first = $aInt[$j];
for ($i=1; $i<=$NumInt-1-$j; $i++)
{
echo $first." --- ".$aInt[$i+$j]."<br>";
}
}
?>


__________________
Thanks & Regards
Sabari...
Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #104 (permalink)  
Old 08-28-2007, 08:58 AM
Sabari Sabari is offline
D-Web Genius
 
Join Date: Jul 2007
Posts: 1,008
Sabari is on a distinguished road
Default Re: Convert Array into Simple XML

Convert Array into Simple XML

<?
function array_xml($array, $num_prefix = "num_")
{
if(!is_array($array)) // text
{
return $array;
}
else
{
foreach($array as $key=>$val) // subnode
{
$key = (is_numeric($key)? $num_prefix.$key : $key);
$return.="<".$key.">".array_xml($val, $num_prefix)."</".$key.">";
}
}

return $return;
}

//example:

$array[0][0] = 1;
$array[0]['test'] = "test";
$array['test1']['test2'] = "test";
$array['test'][0] = "test";
$array['test'][1]['test_x'] = $array;

print_r($array);

print"<xml>";

print array_xml($array);

print"</xml>";
?>


Output:

print_r($array) previev:

Array
(
[0] => Array
(
[0] => 1
[test] => test
)
[test1] => Array
(
[test2] => test
)
[test] => Array
(
[0] => test
[1] => Array
(
[test_x] => Array
(
[0] => Array
(
[0] => 1
[test] => test
)
[test1] => Array
(
[test2] => test
)
[test] => Array
(
[0] => test
)
)
)
)
)


result xml in firefox preview:

<xml>
<num_0>
<num_0>1</num_0>
<test>test</test>
</num_0>
<test1>
<test2>test</test2>
</test1>
<test>
<num_0>test</num_0>
<num_1>
<test_x>
<num_0>
<num_0>1</num_0>
<test>test</test>
</num_0>
<test1>
<test2>test</test2>
</test1>
<test>
<num_0>test</num_0>
</test>
</test_x>
</num_1>
</test>
</xml>
__________________
Thanks & Regards
Sabari...
Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #105 (permalink)  
Old 08-28-2007, 11:54 PM
Sabari Sabari is offline
D-Web Genius
 
Join Date: Jul 2007
Posts: 1,008
Sabari is on a distinguished road
Default Re: Count Dimensions in Multi-Array?

To Count Dimensions in Multi-Array


$array = array('index1' => 'val1', 'index2' => 'val2', 'index3' => 'val3');

$vResult = countdim($array);
echo $vResult;

function countdim($array)
{
if (is_array(reset($array)))
$return = countdim(reset($array)) + 1;
else
$return = 1;

return $return;
}


This function will return int number of array dimensions.
__________________
Thanks & Regards
Sabari...
Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #106 (permalink)  
Old 08-29-2007, 12:31 AM
Sabari Sabari is offline
D-Web Genius
 
Join Date: Jul 2007
Posts: 1,008
Sabari is on a distinguished road
Default Re: Types Of Arrays:

Types Of Arrays:
There are three different types of arrays in PHP:

a) Numeric Array: An array with a numeric ID key.
b) Associative Array: An array where each ID key is associated with a value.
c) Multidimensional Array: An array containing one or more arrays.

Now lets discuss different types of arrays in details.
__________________
Thanks & Regards
Sabari...
Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #107 (permalink)  
Old 08-29-2007, 12:32 AM
Sabari Sabari is offline
D-Web Genius
 
Join Date: Jul 2007
Posts: 1,008
Sabari is on a distinguished road
Default Re: Using Arrays in PHP

Numeric Arrays:

Numeric arrays use integer / numbers as their index number to identify each item of the array. The example we discussed above are numeric arrays as they have integer values as index numbers for each item.

<?php
$colours = array("white","black","blue");

print_r($colours);


output will be
Array
(
[0] => white
[1] => black
[2] => blue
)

?>

In the above output you can see the index numbers for white, black and blue are 0,1,2 respectively which are numeric values and hence we call such arrays numeric arrays.
__________________
Thanks & Regards
Sabari...
Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #108 (permalink)  
Old 08-29-2007, 12:33 AM
Sabari Sabari is offline
D-Web Genius
 
Join Date: Jul 2007
Posts: 1,008
Sabari is on a distinguished road
Default Re: Using Arrays in PHP

Associative Arrays:
Sometimes it’s better to use the index name instead of index number for example if you want to save three students' names and numbers so your best option will be to use each student’s name as index value for the array and his numbers as the values, behold on the example below,

<?php

$students['Anna'] = 90;
$students['Maria'] = 60;
$students['Jennifer'] = 40;

?>

When you submit a form using POST or GET method you get a similar associative array on the receiving page that contains the name of each form field as array index and its value as index value. Try to make a HTML form with some fields and post it and on the receiving page print the global arrays like

print_r($_POST);
print_r($_GET);

and you will see the associative array.

Associative Arrays are more easy to handle and to process information especially dealing with complex form submission and dynamic values from database etc.
__________________
Thanks & Regards
Sabari...
Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #109 (permalink)  
Old 08-29-2007, 12:34 AM
Sabari Sabari is offline
D-Web Genius
 
Join Date: Jul 2007
Posts: 1,008
Sabari is on a distinguished road
Default Re: Using Arrays in PHP

Multidimensional Arrays:
A multidimensional array can contain arrays within itself and the sub arrays contain more arrays within them.

Let's move to a real world example to understand the concept of multidimensional arrays:

David has two sons Richie and Mason. Richie has two daugters Sue and Natasha while Mason has three daughters Nichole, Salma and Amber.

Now they family tree is as follows:
mutidimensional arrays
If we want to display David’s family tree with a multidimensional array in PHP then we can define an array as below:

$david = array
(
“richie”=>array
(
“Sue”,
“Natasha”
),

“Mason”=>array
(
“Nichole”,
“Salma”,
“Amber”
)

);

This is how you can use multidimensional arrays to organize data. Try to submit an array of form fields and then print the global array to check the output, you will get the global array as multidimensional array that will contain more sub arrays.
__________________
Thanks & Regards
Sabari...
Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #110 (permalink)  
Old 08-29-2007, 12:44 AM
Sabari Sabari is offline
D-Web Genius
 
Join Date: Jul 2007
Posts: 1,008
Sabari is on a distinguished road
Default Re: Using Arrays in PHP

To anybody wanting a double-sided array_diff - array_diff gives you everything in the first array that isn't in the subsequent arrays.

$array1=array('blue','red','green');
$array2=array('blue','yellow','green');

$Result = array_merge(array_diff($array1, $array2),array_diff($array2, $array1));
print_r($Result);

Result
Array
(
[0] => red
[1] => yellow
)
__________________
Thanks & Regards
Sabari...
Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #111 (permalink)  
Old 08-29-2007, 05:22 AM
P.Sathiya P.Sathiya is offline
D-Web Trainee
 
Join Date: Mar 2007
Posts: 16
P.Sathiya is on a distinguished road
Thumbs up Re: maximum size of variable? and maximum size of array?

hi senraj,

Quote:
Originally Posted by senraj View Post
What is maximum size of a normal variable & array variable? OR is there any default size available? Pls help me. Thanks
The capacity of the array is limited only by the capacity of the user's machine.
Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #112 (permalink)  
Old 08-29-2007, 07:19 AM
venkat_charya venkat_charya is offline
D-Web Sr.Programmer
 
Join Date: Mar 2007
Posts: 118
venkat_charya is on a distinguished road
Question Re: Using Arrays in PHP

Hi friends,

Can any body tell me to solve this problem.

I have two array.

$a = array("A","B","C","D");
$b = array("2","3","0","4");

Just want to check if array $b having 0 value then remove that array key and same key removed in $a. So the output should come like that.

A, B, D
2, 3, 4

I hope problem understandable here. I need to solve this without using any loop.

Thanks in advance
Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #113 (permalink)  
Old 08-29-2007, 07:54 AM
raj raj is offline
D-Web Programmer
 
Join Date: Jul 2007
Posts: 89
raj is on a distinguished road
Smile Re: Using Arrays in PHP

Hi Venkat Charya.....

here i given sample code for your query...

<?$a = array("A","B","C","D","E","I","F","G");
$bvalue = array("2","3","0","4","0","3","4","0");
$bKey = array_keys($bvalue);
echo "Input :<br>";
print_r($a);
$aFinal = array_map("funRemove", $bKey, $bvalue);
echo "Result :<br>";
print_r($a);

function funRemove($pmKey, $pmValue)
{
global $a;
if($pmValue == 0) unset($a[$pmKey]);

}


//# Result:

Input :Array
(
[0] => A
[1] => B
[2] => C
[3] => D
[4] => E
[5] => I
[6] => F
[7] => G
)
Result :Array
(
[0] => A
[1] => B
[3] => D
[5] => I
[6] => F
)



?>
Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #114 (permalink)  
Old 08-29-2007, 08:27 AM
venkat_charya venkat_charya is offline
D-Web Sr.Programmer
 
Join Date: Mar 2007
Posts: 118
venkat_charya is on a distinguished road
Smile Re: Using Arrays in PHP

Thanks Raj it is working absolutely fine. As per my search if there is only single value for which we want to remove we can use this code.

<?php
$input = array("red", "green", "blue", "yellow");
$vKey = array_search("green",$input);
unset($input[$vKey]);
print_r($input);
?>


But repeatition of same value, which we want to remove we can use your code which will work either condition [Single or multiple]. Your sample code giving perfect solution.

Thanks once again for your fast reply
Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #115 (permalink)  
Old 09-18-2007, 09:44 AM
Sabari Sabari is offline
D-Web Genius
 
Join Date: Jul 2007
Posts: 1,008
Sabari is on a distinguished road
Thumbs up Re: Using Arrays in PHP

Thanks for you questions Mr. venkat_charya, we hope, all of your questions are mostly satisfied with our answers, we also leared lot of things while try to answer your queries, we are eagerly waiting for your more questions, please keep on posting....
__________________
Thanks & Regards
Sabari...
Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #116 (permalink)  
Old 10-17-2007, 11:11 PM
senraj senraj is offline
D-Web Master
 
Join Date: Jul 2007
Posts: 418
senraj is on a distinguished road
Post Re: Using Arrays in PHP

hi,

array 1 match up to the correct values in array 2

array 1 array 2
1 4
2 1
3 3
4 5
5 2

<?php
$a1 = array(1, 2, 3, 4, 5);
$a2 = array(4, 1, 3, 5, 2);

var_dump(! (bool) array_diff($a1, $a2));
?>

Will print true if both arrays have the same values.
Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #117 (permalink)  
Old 10-27-2007, 12:14 AM
senraj senraj is offline
D-Web Master
 
Join Date: Jul 2007
Posts: 418
senraj is on a distinguished road
Post Re: Using Arrays in PHP

hi,

How to reverse a array in php?

$my_array = array("a" => "Dog", "b" => "Cat", "c" => "Horse");

krsort($my_array);
echo "<pre>";
print_r($my_array);
echo"</pre>";


output:
-------

Array
(
[c] => Horse
[b] => Cat
[a] => Dog
)
Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #118 (permalink)  
Old 10-27-2007, 12:26 AM
senraj senraj is offline
D-Web Master
 
Join Date: Jul 2007
Posts: 418
senraj is on a distinguished road
Post Re: Using Arrays in PHP

hi,

previous post reverse an array in php with using built in function and example.

now, am asking question for...

How to reverse an array in php with out using any built in functions?

Last edited by senraj : 10-28-2007 at 11:08 PM.
Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #119 (permalink)  
Old 10-29-2007, 05:50 AM
senraj senraj is offline
D-Web Master
 
Join Date: Jul 2007
Posts: 418
senraj is on a distinguished road
Post Re: Using Arrays in PHP

$friend = array("Justin","Lloyd","Alexa","Devron");
$age = array(45,32,26,"Sen => 24");

$res = array_merge($friend,$age);
echo implode(",",$res);

echo "</br>";

$res1 = array_merge_recursive($friend,$age);
echo implode(",",$res1);

output:
---------
Justin,Lloyd,Alexa,Devron,45,32,26,Sen => 24

Justin,Lloyd,Alexa,Devron,45,32,26,Sen => 24


What is difference between array_merge and array_merge_recursive ?
Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #120 (permalink)  
Old 11-02-2007, 03:59 AM
Sabari Sabari is offline
D-Web Genius
 
Join Date: Jul 2007
Posts: 1,008
Sabari is on a distinguished road
Default Re: Using Arrays in PHP

Difference for these two functions

array_merge()
===========

It merges the elements of one or more arrays. The elements and values are appended at the end of the previous array.

array_merge_recursive:
=================
It merges the two or more arrays recurssively.
__________________
Thanks & Regards
Sabari...
Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
Reply


Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On

LinkBacks (?)
LinkBack to this Thread: http://www.discussweb.com/php-programming/3298-using-arrays-php.html
Posted By For Type Date
mrajendhran's bookmarks tagged with This thread Refback 08-30-2007 01:04 PM
DiscussWeb IT Community - Technical Support and Technology Discussions This thread Refback 08-23-2007 10:11 AM

Similar Threads
Thread Thread Starter Forum Replies Last Post
Jagged Arrays in C# vigneshgets C# Programming 4 09-30-2008 04:48 AM
Classic asp arrays and recordset ramesh123 ASP and ASP.NET Programming 1 12-02-2007 08:44 PM
Using arrays in stored procedures oxygen Database Support 1 11-26-2007 08:01 AM
Arrays in Java leoraja8 Java Programming 7 11-19-2007 01:23 AM
Java:Tutorial - Arrays pranky Java Programming 0 02-24-2007 12:54 AM


All times are GMT -7. The time now is 06:03 PM.