/    Sign up×
Community /Pin to ProfileBookmark

Numeric Arrays

Using a Numeric Array, how I add and delete data?

to post a comment
PHP

2 Comments(s)

Copy linkTweet thisAlerts:
@JDM71488Nov 21.2007 — I usually populate numerically indexed arrays like so:

[code=php]for($i = 0; $i < 3; $i++)
{
$array[] = $i * $i;
}[/code]


PHP.net has a complete guide on manipulating arrays. Scroll down to the function list. http://us2.php.net/manual/en/ref.array.php
Copy linkTweet thisAlerts:
@jasonahouleNov 21.2007 — There are many ways to populate arrays. One good thing about arrays in PHP is that you don't have to define their size when you create it.

If you know all the values you want to put into an array then you can use the method above. There are also array_merge() and array_push() functions which will add values to your array.

Here are four different ways to populate an array and they all result in the same thing...
[code=php]
<?php
$myArray1 = array(1,2,3,4,5);

$myArray2 = array();
$myArray2[] = 1;
$myArray2[] = 2;
$myArray2[] = 3;
$myArray2[] = 4;
$myArray2[] = 5;

$myArray3 = array(1);
array_push($myArray3, 2);
array_push($myArray3, 3);
array_push($myArray3, 4);
array_push($myArray3, 5);

$myArray4 = array_merge(array(1,2,3), array(4,5));

echo 'array 1: ';
print_r($myArray1);
echo '<br/><br/>';

echo 'array 2: ';
print_r($myArray2);
echo '<br/><br/>';

echo 'array 3: ';
print_r($myArray3);
echo '<br/><br/>';

echo 'array 4: ';
print_r($myArray4);
echo '<br/><br/>';
?>
[/code]
×

Success!

Help @Dysan spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 5.19,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @AriseFacilitySolutions09,
tipped: article
amount: 1000 SATS,

tipper: @Yussuf4331,
tipped: article
amount: 1000 SATS,

tipper: @darkwebsites540,
tipped: article
amount: 10 SATS,
)...