1

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!

1
  • 1
    Wouldn't it make more sense for the final result to be a structure of the form array(code => title, code2 => title2, …)? Commented Feb 7, 2014 at 15:13

4 Answers 4

2

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

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

2 Comments

@billonecan great sport with the trim by the way. Does array_map just apply the function in the first parameter to the value in the second?
@Yabsley that's correct - it applies trim to every item in the array :)
1

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

1 Comment

Thanks I tried this and it worked but I think I'm going to use @billyonecan's solution because it fits my needs better. Cheers!
1

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'],
    );
}

Comments

0
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);
}

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.