I am working on a project.In this i need to assign values in array with different location of code.first i used the $bd=array('1','420'); $bd=array('2','520'); etc.but when i echo $bd[1]; and echo $bd[2]; only last inserted value(520) is displayed.do you have any solution for it?

Hi,

You may want to try using array_push() function.

example of usage...

<?php
    $bd= array('1','420'); 

    array_push($bd,'2','520');

    echo $bd[0].'<br/>';
    echo $bd[2].'<br/>';
    echo $bd[1].'<br/>';
    echo $bd[3].'<br/>';

You way is not proper to achieve your aim.

Use any of the following 2 ways

Way 1

<?php
$bd=array('1'=>'420','2'=>'520'); 
echo $bd[1]."<br>"; 
echo $bd[2]; 

?>

Way 2

<?php
$bd[1]=420;
$bd[2]=520;

echo $bd[1]."<br>";  
echo $bd[2]; 

?>

now the array is working correctly but now i am having problem with sorting the array .i am usig $bsd2=sort($bsd); $bed2=sort($bed); to sort two array $bsd and $bed. and displaying via $bsd2[0]; bsd2[1] etc.but it is not working.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.