Yabsley
Yabsley

Reputation: 380

New PHP array from multiple arrays inside a loop

To begin with I have an array (courses) like this:

array(2)
{
    [0] => array(2)
    {
        ["title"] => string "course1"
        ["code"] => string "001, 002, 003"
    }
    [1] => array(2)
    {
        ["title"] => string "course2"
        ["ps_course_code"] => string "004"
    }
}

Sometimes the 'code' will contain multiple codes as a comma separated string, other times 'code' will contain a single code.

To get individual codes I loop through the courses array and use explode to separate out the codes:

foreach($courses as $course) {
    $courseInfo['code'] = explode(",",$course["code"]);
    $courseInfo['title'] = $course["title"];
    var_dump($courseInfo);
}

This gives me a new array for each course:

array(2)
{
    ["code"] => array(3)
    {
        [0] => string "001",
        [1] => string "002",
        [2] => string "003",
    }
    ["title"] => string "course1"
}

array(2)
{
    ["code"] =>  array(1)
    {
        [0] => string "004"
    }
    ["title"] => string "course2"
}

What I need though is a new array that contains every code and its title. So for some courses this means there will be multiple codes with the same title. E.g:

array(4)
{
    [0] => array (2)
    {
        ["code"] => string "001",
        ["title"] => string "course1"
    }
    [1] => array (2)
    {
        ["code"] => string "002",
        ["title"] => string "course1"
    }
    [2] => array (2)
    {
        ["code"] => string "003",
        ["title"] => string "course1"
    }
    [3] => array (2)
    {
        ["code"] => string "004",
        ["title"] => string "course1"
    }
}

I'm really struggling with this. Do I need another loop inside the first in order to create the final array?

foreach($courses as $course) {
    $courseInfo['code'] = explode(",",$course["code"]);
    $courseInfo['title'] = $course["title"];

    // Another loop to create final array?
    foreach($courseInfo as $value) {
        $final['code'] = $value['code']; // I know this doesn't work!
        $final['title'] = $value['title'];
    }
}
var_dump($final);

I know this is long and I probably haven't explained this very well, so apologies!

Upvotes: 1

Views: 872

Answers (4)

billyonecan
billyonecan

Reputation: 20250

You can get the desired output by looping over your first array:

$final = array();
foreach ($courses as $course) {
    $codes = array_map('trim', explode(',', $course['code']));
    foreach ($codes as $c) {
        $final[] = array('code' => $c, 'title' => $course['title']);
    }
}

Demo

Upvotes: 2

Ruben
Ruben

Reputation: 343

Easiest way to create array from the array value code is to loop through the array and explode the values, like you did.

$new_array = array(); //optionally for adding to new array
foreach($courses as $course)
{
    $code = explode(',', $course['code']);
    $trimmed_code = array_walk($code, function($value)
    {
        //trim the value to remove spaces before and after value
        return trim($value); 
    });

    $course['code'] = $trimmed_code;

    //code for optionally add to new array
    $new_array[] = array(
        'code' => $code,
        'title' => $course['title'],
    );
}

Upvotes: 1

Nick
Nick

Reputation: 6346

You'd need to loop around the array of courses, not the full array.

i.e. something like this:

$i = 0;
foreach($courses as $course) {
    $codes = explode(",",$course["code"]);

    foreach($codes as $code) {
        $final[$i]['code'] = $code;
        $final[$i]['title'] = $course['title'];
        $i++;
    }
}
var_dump($final);

Upvotes: 1

Naner
Naner

Reputation: 1292

foreach($courses as $course) {
    $codes = explode(",",$course["code"]);
    for($i=0;$i<count($codes); $i++){
        $courseInfo['code'] = $codes[i];
        $courseInfo['title'] = $course["title"];
    } 
    var_dump($courseInfo);
}

Upvotes: 0

Related Questions