0

I have an array structure like the following:

enter image description here

I would like to have the elements keys, bi, media and offer under biz_id. Like this:

biz_id
 bi
 media
 offer
biz_id
...

My code is the following:

    foreach ($premiumContent as $targets=>$target)
{

    $finalResult[]["biz_id"] = $target->biz_id;

    foreach ($target->media as $media)
     {
        switch ($media->type_id)
        {
            case '1':
                $finalResult[]["info"] = $media->text;
            break;
            case '6':
                $finalResult[]["bi"] = $media->url;
            break; 
            case '4':
                $mediaGroup[] = array('link' => $media->url, 'descript' => $media->text);
            break;
            case '3':
                $offerGroup[] = array('link' => $media->url, 'descript' => $media->text);
            break; 
            default:
                echo '';
        }
    }
    $finalResult[]["media"] = $mediaGroup;
    $finalResult[]["offer"] = $offerGroup;
}

return $finalResult;

What can I do to group the keys underneath biz_id?

Thanks for the recommendations.

1
  • Please dp take into consideration that targets contains various biz_id and that each media can have various type_id. With the answers provided i am getting items repeated. Commented Sep 5, 2011 at 21:58

4 Answers 4

1

Simple: make the biz_id the key of the array:

foreach ($premiumContent as $targets=>$target)
{

    foreach ($target->media as $media)
     {
        switch ($media->type_id)
        {
            case '1':
                $finalResult[$target->biz_id]["info"] = $media->text;
            break;
            case '6':
                $finalResult[$target->biz_id]["bi"] = $media->url;
            break; 
            case '4':
                $mediaGroup[] = array('link' => $media->url, 'descript' => $media->text);
            break;
            case '3':
                $offerGroup[] = array('link' => $media->url, 'descript' => $media->text);
            break; 
            default:
                echo '';
        }
    }
    $finalResult[$target->biz_id]["media"] = $mediaGroup;
    $finalResult[$target->biz_id]["offer"] = $offerGroup;
}

return $finalResult;
Sign up to request clarification or add additional context in comments.

2 Comments

@view, I claimed victory too early. Now it is multiplying the entries for the sub arrays info, bi, mediaGroup and offerGroup based on the number of elements in targets. Currently there are two elements in target with 2 elements in media each. I am getting 4 media in each target.
@ldj: I did a quick copy and paste, and added in a key where it shouldn't have been. Take a look at the most recent edit, where I fixed that. Hopefully the important thing is that you pick up on the fact that you can force a key to be whatever you want it to be. All php keys are associative. If you omit a key using the [] operator, it simply creates for you a numeric associative key, but this simply points out, that when that is not convenient you can assign the key to be what you want it to be ... [mykey].
1

Create each parent array using the biz_id as the array key. For example, your first switch case would look like $finalResult[$target->biz_id]["info"] = $media->text;. Then you could simply remove this line -> $finalResult[]["biz_id"] = $target->biz_id;

Comments

0

I am Lazy

foreach ($premiumContent as $targets=>$target)
{

$finalResult[]["biz_id"] = $target->biz_id;
//<<my changes
foreach ($finalResult as $key => $val) 
{
}
$key;
//my changes>>

foreach ($target->media as $media)
{
    switch ($media->type_id)
    {
    case '1':
        $finalResult[]["info"] = $media->text;
    break;
    case '6':

//<<my changes
        $finalResult[$key]["bi"] = $media->url;

//my changes>>

    break; 
    case '4':
        $mediaGroup[] = array('link' => $media->url, 'descript' => $media->text);
    break;
    case '3':
        $offerGroup[] = array('link' => $media->url, 'descript' => $media->text);
    break; 
    default:
        echo '';
    }
}
//<<my changes
$finalResult[$key]["media"] = $mediaGroup;
$finalResult[$key]["offer"] = $offerGroup;
//my changes>>
}

return $finalResult;

Comments

0

You need to create a sub-array with elements info, bi and media which are assigned to the biz_id key of the parent array. This code creates an intermediary result which is then assigned to another element of your $finalResult array at the end (assuming there is more than 1 biz_id 'group' of values returned, as hinted by your variable dump).

foreach ($premiumContent as $targets=>$target) {
    $interResult['biz_id'] = $target->biz_id;

    foreach ($target->media as $media) {
        switch ($media->type_id) {
            case '1':
                $interResult['biz_id']['info'] = $media->text;
                break;
            case '6':
                $interResult['biz_id']['bi'] = $media->url;
                break; 
            case '4':
                $interResult['biz_id']['media'] = array(
                    'link' => $media->url,
                    'descript' => $media->text,
                );
                break;
            case '3':
                $interResult['biz_id']['offer'] = array(
                    'link' => $media->url,
                    'descript' => $media->text,
                );
                break; 
            default:
                echo '';
        }
    }
    $finalResult[] = $interResult;
}

return $finalResult;

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.