Assign single value to multiple keys?

Started on the old forum in April 2013 · view the original in the archive · old links redirect here

10 replies · 801 views · started 13 years ago · 1 min read

6 participants
arnage Lead · 811 posts #1

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***
kevk3v Senior · 332 posts #2

I dont think you can do that? I would do

$val = 'one';
$arr = array(
  '1' => $val,
  '2' => $val
);

Mobile chat, iphone chat, android chat, chat, rooms http://www.aiochat.com

arnage Lead · 811 posts #3

But thats the thing which i would like to avoid if is possible. 🙂

<!DOCTYPE html PUBLIC "-//***WAPFORUM**.**RS***
GumSlone Administrator Principal · 1,690 posts #4

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'));

Advertise your mobile site for FREE with AdTwirl

External image (may be gone)

CreativityKills Lead · 833 posts #5

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.

arnage Lead · 811 posts #6

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***
DRX Junior · 22 posts #7

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'));

your custom function custom_arr() is similar to default array_fill(startkey, num, value) PHP: array_fill - Manual

It`s working..

Ghost Senior · 366 posts #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 ' ![](http://destrukt.dk/ico/phpdeveloper.jpg) alt='coding-talk.com!!' />'; echo 'Sharing Is Caring!'; } else { echo ' ![](http://img98.imageshack.us/img98/6476/av3ro4.jpg) alt='the username GHOST has been comprimised!' />'; echo 'OMG SOMEBODY HELP ME!!'; } ?>
arnage Lead · 811 posts #9

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***
Ghost Senior · 366 posts #10

your welcome buddy, code looks slicker too 😉

<?php include ('Ghost'); if ($Post == true) { echo ' ![](http://destrukt.dk/ico/phpdeveloper.jpg) alt='coding-talk.com!!' />'; echo 'Sharing Is Caring!'; } else { echo ' ![](http://img98.imageshack.us/img98/6476/av3ro4.jpg) alt='the username GHOST has been comprimised!' />'; echo 'OMG SOMEBODY HELP ME!!'; } ?>
arnage Lead · 811 posts #11

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***

Sign in to reply.

Similar topics

Keyboard shortcuts

g f
Go to forums
g h
Go to your feed
g m
Go to messages
r
Reply (in a topic)
/
Search
j
Next post
k
Previous post
?
This list
Ctrl/⌘ + Enter
Send what you are writing
Ctrl/⌘ + B / I
Bold / italic while writing
Esc
Close