php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Request #55505 Request for array subtraction operator.
Submitted: 2011-08-25 09:48 UTC Modified: 2011-08-28 01:16 UTC
Votes:1
Avg. Score:1.0 ± 0.0
Reproduced:0 of 1 (0.0%)
From: simon at spudley dot com Assigned:
Status: Wont fix Package: Arrays related
PHP Version: 5.3.8 OS:
Private report: No CVE-ID: None
View Developer Edit
Welcome! If you don't have a Git account, you can't do anything here.
If you reported this bug, you can edit this bug over here.
(description)
Block user comment
Status: Assign to:
Package:
Bug Type:
Summary:
From: simon at spudley dot com
New email:
PHP Version: OS:

 

 [2011-08-25 09:48 UTC] simon at spudley dot com
Description:
------------
The manual page http://www.php.net/manual/en/language.operators.array.php describes several operators available which work on array variables. This includes the addition (+) operator, which allows us to merge two arrays.

I would suggest that a subtraction operator would also be useful.

For example, let us say we have two arrays, one of which is a full list of available items, and the other is a list of items which have been used, and I want to find the list of items which haven't been used. An array subtraction operator would help with this.

Test script:
---------------
<?php
//suggested use for array subtraction operator.

$full_list = array('de'=>'some data', 'gr'=>'some data', 'tt'=>'some data'); //etc...
$used_list = array('gr'=>'some data'); //etc...

$unused_list = $full_list - $used_list; //would contain 'de' and 'tt', but not 'gr'.
?>

Expected result:
----------------
$unused_list would contain the array elements from $full_list minus those present in $used_list. In the example code, this would mean $unused_list would contain 'de' and 'tt', but not 'gr'.


Actual result:
--------------
This is a feature suggestion, so the above code does not currently run.

Patches

Pull Requests

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2011-08-28 01:16 UTC] aharvey@php.net
-Status: Open +Status: Wont fix
 [2011-08-28 01:16 UTC] aharvey@php.net
array_diff_key() already provides this functionality, and - is more ambiguous for PHP style arrays than a simple union: should it be the difference based on keys, values, or key/value pairs?

I'm going to won't fix this here -- an RFC would be a more appropriate path forward for this, although I personally wouldn't support it even there.
 
PHP Copyright © 2001-2024 The PHP Group
All rights reserved.
Last updated: Sat Dec 21 15:01:29 2024 UTC