Sunday, November 17, 2013

Create new Small Array from Big Array (Array Push method)

Hiiiii, if you are a php developer than sometimes you encounter a problem of making a small array out of large array on some conditions basis or you want to insert some elements into the array. PHP is having many easy method to do the above defined work.You can insert the elements in array in various conditional statements like for loop, foreach loop , while loop etc. In this example I'll be showing two array insertion method in two different loops.



CODE BEGINS :

<?php
$link = mysql_connect('localhost', 'root', '');
if (!$link) {
    die('Not connected : ' . mysql_error());
}

$db_selected = mysql_select_db('records', $link);
if (!$db_selected) {
    die ('Can\'t use records : ' . mysql_error());
}

$smallarray1=array(); // first small array
$query="select * from users";
$line=mysql_query($query);
while($result=mysql_fetch_array($line))
{
if(1==1)
{
array_push($smallarray1,$result['userID']);       // insert elements in array 
$smallarray2[]=$result['userName'];     //second small array
}
}

foreach ($smallarray2 as $newarray)
{
$smallarray3[]=$newarray; // third new array
}

// TO print the above two developed small arrays
echo "<pre>";
print_r($smallarray1);
print_r($smallarray2);
print_r($smallarray3);
echo "</pre>"; 
?>


Hope you like this post……..Please Comment…………!!!!!!!!!


Tags:

0 Responses to “Create new Small Array from Big Array (Array Push method)”

Post a Comment

© 2013 MyCodeStock. All rights reserved.
Designed by SpicyTricks