$arr = array(1 => "rambow", 2 => "rambow", 3 => "popcorn", 4 => "rambow");

how do i make it in simpler format so i dont have to keep typing "rambow" for every number that => "rambow"

? cause theres a lot of "rambow" for a whole bunch of numbers. isnt there any easier way to do this?

Recommended Answers

All 5 Replies

$arr = array_fill_keys(range(1,4), "rambow");
$arr[3] = "popcorn";
$arr = array(1 => "rambow", 2 => "rambow", 3 => "popcorn", 4 => "rambow");

how do i make it in simpler format so i dont have to keep typing "rambow" for every number that => "rambow"

? cause theres a lot of "rambow" for a whole bunch of numbers. isnt there any easier way to do this?

How many "rambow" do you want to add on your array? Is it (rambow) inserted on any value on the array? Is "rambow" in the array inserted randomly?

Member Avatar for diafol
$arr = array_fill_keys(range(1,4), "rambow");
$arr[3] = "popcorn";

Nice.

<?php
$arrayFill = array();
$arrayFill["rambow"] = ":0:1:3:5:6:7:8:9:11:12:13:15:16:18:21:22:";
$arrayFill["popcorn"] = ":2:4:10:14:17:19:20:";
$default = "default";

$arr = array();
$totalLen = 22;
for($i = 0; $i < $totalLen; $i++)
{
    $fillvalue = "";
    foreach($arrayFill as $key=>$value)
    {
        if(strpos($value, ":" . $i . ":") === false) continue;

        $fillvalue = $key;
    }

    $arr[$i] = trim($fillvalue) == ""?$default:$fillvalue;
}
?>

or better yet

<?php
$arrayFill = array();
$arrayFill["popcorn"] = ":2:4:10:14:17:19:20:";
$default = "rambow";

$arr = array();
$totalLen = 22;
for($i = 0; $i < $totalLen; $i++)
{
    $fillvalue = "";
    foreach($arrayFill as $key=>$value)
    {
        if(strpos($value, ":" . $i . ":") === false) continue;

        $fillvalue = $key;
    }

    $arr[$i] = trim($fillvalue) == ""?$default:$fillvalue;
}
?>
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.