View Full Version : Inserting Values In Array
03-08-2007, 11:36 AM
Does anyone know how to insert values into an array? For example i have an array called $array with orange green blue Black in that order how do i insert red in between green and blue?
03-08-2007, 12:19 PM
This is one way...I'm assuming your using numbered indices:
$my_array = array("orange","green","blue","black");
foreach ($my_array as $val)
if ($val == "blue")
$my_array2 = "red";
$my_array2 = $val;
$my_array = $my_array2;
03-08-2007, 07:40 PM
This is another way you can do it:
$search = 'green';
$after = 'red';
$my_array = array('orange','green','blue','black');
array_splice($my_array, array_search($search, $my_array), 1, array($search, $after));
03-08-2007, 10:05 PM
Thanks for responding. Any idea as to how i can apply that to a user supplied number and when i get 3 place values it inserts a comma?
03-09-2007, 02:42 AM
If I understand you right, instead of splitting up your multi-digit, user-supplied number into an array of one-digit numbers, just use number_format (http://us3.php.net/manual/en/function.number-format.php)() to format your numbers.
03-09-2007, 03:10 AM
Thats perfect!!! Thanks!!!
Powered by vBulletin® Version 4.2.2 Copyright © 2015 vBulletin Solutions, Inc. All rights reserved.