Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8463459
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 10, 20262026-06-10T14:27:37+00:00 2026-06-10T14:27:37+00:00

I would like to split an array: $o = json_decode(‘[{id:1,color:green},{id:2,color:green},{id:3,color:yellow},{id:4,color:green}]’); based on the color

  • 0

I would like to split an array:

$o = json_decode('[{"id":"1","color":"green"},{"id":"2","color":"green"},{"id":"3","color":"yellow"},{"id":"4","color":"green"}]');

based on the color attribute of each item, and fill corresponding sub arrays

$a = array("green", "yellow", "blue");

function isGreen($var){
    return($var->color == "green");
}

$greens = array_filter($o, "isGreen");
$yellows = array_filter($o, "isYellow");
// and all possible categories in $a..

my $a has a length > 20, and could increase more, so I need a general way instead of writing functions by hand

There doesn’t seem to exist a function array_split to generate all filtered arrays
or else I need a sort of lambda function maybe

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-10T14:27:39+00:00Added an answer on June 10, 2026 at 2:27 pm

    You could do something like:

    $o = json_decode('[{"id":"1","color":"green"},{"id":"2","color":"green"},{"id":"3","color":"yellow"},{"id":"4","color":"green"}]');
    
    $greens = array_filter($o, function($item) {
        if ($item->color == 'green') {
            return true;
        }
    
        return false;
    });
    

    Or if you want to create something really generic you could do something like the following:

    function filterArray($array, $type, $value)
    {
        $result = array();
        foreach($array as $item) {
            if ($item->{$type} == $value) {
                $result[] = $item;
            }
        }
    
        return $result;
    }
    
    $o = json_decode('[{"id":"1","color":"green"},{"id":"2","color":"green"},{"id":"3","color":"yellow"},{"id":"4","color":"green"}]');
    $greens = filterArray($o, 'color', 'green');
    $yellows = filterArray($o, 'color', 'yellow');
    

    In my second example you could just pass the array and tell the function what to filter (e.g. color or some other future property) on based on what value.

    Note that I have not done any error checking whether properties really exist

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I would like to split a string into an array according to a regular
I have an array I would like to split in half. So I can
In JS if you would like to split user entry into an array what
I would like to split a string by an array of characters, how would
I would like to split the string: Hello[You]All to the following array: H,e,l,l,o,[You],A,l,l I
I would like to split a string by an array of delimiters, and also
I would like to split the following string generated from a database resultset row.
I would like to split some content from an a html tag. I was
I would like to split a line into words. I know this can be
I would like to split a form across several tab panels to avoid a

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.