Jump to content

creating an array with value as empty string


Recommended Posts

Hello

      i need help making an array with these parameters but i dont know the syntax to use

$x = 30

 

i need $x to be the 30'th value in the array called lets say $array1 , but i need the first 29 values to be empty strings like " " .

so basically everytime i have different values for $x the array should get constructed accordingly with $x-1 number of " " values .

would appreciate help with this construct ..thanks

 

 

Link to comment
Share on other sites

Sounds pretty inefficient to fill up an array with empty values, what are you using it for? There should be a better solution.

 

This will do it nonetheless:

 

<?php
$x = 30;
$array1 = array_fill(1, $x - 1, '');
$array1[$x] = $x;
echo '<pre>';
print_r($array1);
echo '</pre>';
?>

 

Output:

Array
(
    [1] => 
    [2] => 
    [3] => 
    [4] => 
    [5] => 
    [6] => 
    [7] => 
    [8] => 
    [9] => 
    [10] => 
    [11] => 
    [12] => 
    [13] => 
    [14] => 
    [15] => 
    [16] => 
    [17] => 
    [18] => 
    [19] => 
    [20] => 
    [21] => 
    [22] => 
    [23] => 
    [24] => 
    [25] => 
    [26] => 
    [27] => 
    [28] => 
    [29] => 
    [30] => 30
)

Link to comment
Share on other sites

Hello ,

      Thanks for the reply ... i'm planning to use jpgraph for a application the following is example code

include ("../jpgraph.php");

include ("../jpgraph_scatter.php");

 

$datay = array(30,40,50);

 

$graph = new Graph(300,200,"auto");

$graph->SetScale("textlin");

 

$graph->SetShadow();

$graph->img->SetMargin(40,40,40,40);       

 

$graph->title->Set("Simple mpuls plot");

$graph->title->SetFont(FF_FONT1,FS_BOLD);

 

$sp1 = new ScatterPlot($datay);

$sp1->mark->SetType(MARK_SQUARE);

$sp1->SetImpuls();

 

$graph->Add($sp1);

$graph->Stroke()

 

i need only 3 values on the x-axis ..but if i use the above  ...the values of 30,40,50 (y-scale) ...will be plotted at positions 1,2 and 3

i need them to be plotted at notch 30,40,50 Respectively ...i'll try and be as clear as possible..i hope i am. If i add 30 empty values in the array then my "bar" on my bar plot gets plotted like i want on the 30'th point on the x-scale

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.