AR
How to assign single value to multiple linear keys?
For example, this is perfectly wrong:
$array = array(
array(range(1, 5) => 'one'), // also: range(1, 5) => 'one',
6 => 'two',
7 => 'three',
);
Im getting the PHP Warning: Illegal offset type
<!DOCTYPE html PUBLIC "-//***WAPFORUM** .**RS** *
AR
But thats the thing which i would like to avoid if is possible. 🙂
<!DOCTYPE html PUBLIC "-//***WAPFORUM** .**RS** *
AR
GumSlone wrote:
make a custom function
like:
function custom_arr($start, $end, $value)
{
for($i=$start;$i<=$end;$i++){$a[$i]=$value;}
return $a;
}
$arr = array(
6 => 'two',
7 => 'three'
);
$arr = array_merge($arr, custom_arr(1, 5, 'one'));
Thanks, good idea. 🙂
CreativityKills wrote:
I don't know what you are trying to achieve but I've always said this, if it feels like you're forcing codes, then you're probably doing something wrong.
Just to try to avoid listing each key for the same value. Like when assigning multiple values for one key only the opposite,
$set = array(
'var' => array(5, 6, 7, 8, 9),
'showshout' => 1,
'csses' => array(
array('wapforum' => 'Theme Black'),
array('wapforums' => 'Theme White'),
array('basicblack' => 'Basic Black'),
array('basicwhite' => 'Basic White')
),
'default_css' => 'wapforum',
);
<!DOCTYPE html PUBLIC "-//***WAPFORUM** .**RS** *
GH
Ghost
Senior · 366 posts
13 years ago
#8
array_fill_keys() is pretty much the same also.
<?php
$keys = array(4, 5, 6, 7);
$a = array_fill_keys($keys, 'banana');
print_r($a);
?>
///
///
///
The above example will output:
Array
(
[4] => banana
[5] => banana
[6] => banana
[7] => banana
)
PHP: array_fill_keys - Manual
<?php
include ('Ghost ');
if ($Post == true ) {
echo '

alt='coding-talk.com!! ' />';
echo 'Sharing Is Caring!';
} else {
echo '

alt='the username GHOST has been comprimised! ' />';
echo 'OMG SOMEBODY HELP ME!!';
}
?>
AR
Thanks guys.
First i toke Gum's function just instead array_merge i use + because it messed keys, now its like this.
$array = array_fill_keys(range(1, 5), 'one');
$array += array(
6 => 'two',
7 => 'three',
9 => 'four'
);
<!DOCTYPE html PUBLIC "-//***WAPFORUM** .**RS** *
GH
Ghost
Senior · 366 posts
13 years ago
#10
your welcome buddy, code looks slicker too 😉
<?php
include ('Ghost ');
if ($Post == true ) {
echo '

alt='coding-talk.com!! ' />';
echo 'Sharing Is Caring!';
} else {
echo '

alt='the username GHOST has been comprimised! ' />';
echo 'OMG SOMEBODY HELP ME!!';
}
?>
AR
Yep. 🙂 Or it can be written like this also.
$array = array_fill_keys(range(1, 5), 'one')
+ array(
6 => 'two',
7 => 'three',
9 => 'four'
);
<!DOCTYPE html PUBLIC "-//***WAPFORUM** .**RS** *