1

I have this function; I'm trying to fill the $records array with multiple entries of each $vendor. Only the first record goes in the $records array 12 times like I want it to. Now I would like to get the rest to do the same.

function vendors()
{
    $vendor = [
        "Chick-Fil-A #03971 Houston TX" => 3,
        "Uber Trip Help.Uber.Com TX" => 8,
        "Starbucks Store 05 Houston TX" => 3,
        "Shell Service S Houston TX" => 3,
        "Ralphs #0 1200 N Centr Houston TX" => 3,
        "Nails Van Nuys TX" => 3,
        "Postmates Ed3F9 to Httpspostmate CA" => 3,
        "Pico Beauty Supply and SA Houston TX" => 3,
        "Big Mamas & Papas 818-679-9976 TX" => 3,
        "CIT-GO Houston TX" => 3,
        "Dollar Tree Houston TX" => 3,
        "McDonald's M6310 O Houston TX" => 5,
        "Jamba Juice - Houston TX" => 3,
        "Cvs/Pharm 10445--125 N Glendale" => 3,
    ];
    $records = array();
    foreach ($vendor as $key => $value)
    {
        $times = $value * 4;
        $records = array_fill(0, $times, $key);
    }
    return $records;
}
2
  • Please check this - 3v4l.org/s6W5S . Do you want such output? Commented Feb 20, 2020 at 5:02
  • ....are you sure you want to use a new $records array on each loop? Commented Feb 27, 2020 at 7:57

2 Answers 2

2

you are over-writing your array variable again and again instead of assigning new data to it.

Do:

$records[] = array_fill(0, $times, $key);//assign data as child-array to your final array

Output:-https://3v4l.org/rVdKg

Or may be you want like this:- https://3v4l.org/2Yb8B

Sign up to request clarification or add additional context in comments.

Comments

0

Every time you call array_fill it overwrites what you did in the previous call.

If you don't absolutely need to use array_fill you could replace the foreach with something like this:

foreach ($vendor as $key => $value){
        $times = $value * 4;
        for($x=0; $x<$times; $x++){
          if(!isset($records[$x])){
            $records[$x]=[];
        }
          array_push($records[$x], $key);
        }
}

Does that do what you are trying to do?

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.