Tomek Buszewski
Tomek Buszewski

Reputation: 7935

Separate 2d array rows by a column value

I have an array like this:

Array (
    0 => Array ( 
        'id' => 1,
        'name' => 'Hack n\' Slash',
        'slug' => 'hack-n-slash',
        'popular' => 0
    ),
    1 => Array ( 
        'id' => 2,
        'name' => 'FPP',
        'slug' => 'fpp',
        'popular' => 1
    ),  
    2 => Array ( 
        'id' => 3,
        'name' => 'RPG',
        'slug' => 'rpg',
        'popular' => 1
    )
)

What I want, is to split it into two arrays by key popular, so I'll have a list like:

Popular:

Non-popular:

I tried using if/else like this:

foreach($genres as $genre) : 
    if($genre['popular'] == '1' : 
        echo $genre['name']; 
    endif; 
endforeach;

But then, when I added another statement (if($genre['popular'] == '0')), I couldn't use it, since it was quite messy.

How can I achieve this?

Upvotes: 1

Views: 1538

Answers (4)

Mark Baker
Mark Baker

Reputation: 212422

A more generic solution, making use of PHP closures

$pages   = array(
        array(
            "id"      => 1,
            "name"    => "Hack n' Slash",
            "slug"    => "hack-n-slash",
            "popular" => 0
        ),
        array(
            "id"      => 2,
            "name"    => "FPP",
            "slug"    => "fpp",
            "popular" => 1
        ),
        array(
            "id"      => 3,
            "name"    => "RPG",
            "slug"    => "rpg",
            "popular" => 1
        )
    );

function myFilter($arrayValues,$key,$value) {
    return array_filter($arrayValues,
                        function($entry) use ($key,$value) {
                            return $entry[$key] == $value;
                        }
            );
}

$popular = myFilter($pages,'popular',1);
var_dump($popular);
$unpopular = myFilter($pages,'popular',0);
var_dump($unpopular);

Upvotes: 0

tucuxi
tucuxi

Reputation: 17945

Build an array for the key you are splitting by (let us call it the key-array). For each entry in the original array, if it's key is not already present in the key-array, create a new Array() for it. Then, just push the entry into its key-array array.

function split_by_key($entries, $key) {
    $results = array();
    foreach ($entries as $entry) {
        $results[$entry[$key]][] = $entry;
    }
    return $results;
}

use as

$by_popularity = split_by_key($pages, 'popular');
$popular = $by_popularity[1];
$not_popular = $by_popularity[0];

Upvotes: 0

Vitalii Lebediev
Vitalii Lebediev

Reputation: 662

Why not doing something like

$popular = array();
$notPopular = array();
foreach($genres as $genre) {
    if($genre['popular'] == '1')
        $popular[] = $genre;
    else
        $notPopular[] = $genre;
}

And using curly brackets in the code ? :)

Upvotes: 0

Madara's Ghost
Madara's Ghost

Reputation: 174977

<?php

    $pages   = array(
        array(
            "id"      => 1,
            "name"    => "Hack n' Slash",
            "slug"    => "hack-n-slash",
            "popular" => 0
        ),
        array(
            "id"      => 2,
            "name"    => "FPP",
            "slug"    => "fpp",
            "popular" => 1
        ),
        array(
            "id"      => 3,
            "name"    => "RPG",
            "slug"    => "rpg",
            "popular" => 1
        )
    );
    $results = array();
    foreach ($pages as $page) {
        $results[$page["popular"]][] = $page["name"];
    }

    print_r($results);

In this case, $results[0] will contain a list of pages which are not popular, and $results[1] will contain a list of pages which are popular.

Upvotes: 1

Related Questions