Solved

PHP: array that only contains matching items from larger array

Posted on 2016-09-29
3
48 Views
Last Modified: 2016-09-30
Using PHP, how can I get an array that only contains matching items from a larger array?
<?php

$oRSList = array();
$oRSList[] = array('Name'=>'Apple','NumericID'=>'63');
$oRSList[] = array('Name'=>'Peach','NumericID'=>'237');
$oRSList[] = array('Name'=>'Grape','NumericID'=>'163');
$oRSList[] = array('Name'=>'Prune','NumericID'=>'168');
$oRSList[] = array('Name'=>'Mango','NumericID'=>'639');

print_r($oRSList);
?>

Open in new window


I want my new array to only contain items with a NumericID that contains '63'.

These three items contain '63' in their NumericID:
$oRSList[] = array('Name'=>'Apple','NumericID'=>'63');
$oRSList[] = array('Name'=>'Grape','NumericID'=>'163');
$oRSList[] = array('Name'=>'Mango','NumericID'=>'639');

because those items contain '63' in the NumericID.
0
Comment
Question by:skij
3 Comments
 
LVL 22

Accepted Solution

by:
Ferruccio Accalai earned 500 total points
ID: 41821910
Try with this
<?php

$oRSList = array();
$oRSList[] = array('Name'=>'Apple','NumericID'=>'63');
$oRSList[] = array('Name'=>'Peach','NumericID'=>'237');
$oRSList[] = array('Name'=>'Grape','NumericID'=>'163');
$oRSList[] = array('Name'=>'Prune','NumericID'=>'168');
$oRSList[] = array('Name'=>'Mango','NumericID'=>'639');

print_r($oRSList);

$arr63 = array();

foreach ($oRSList as $person) {
	if (strpos($person['NumericID'], '63') !== false)
	{
    $arr63[] = $person['Name'];
	}
}
print_r($arr63);
?>

Open in new window

1
 
LVL 6

Expert Comment

by:Mukesh Yadav
ID: 41821924
Try this ;)

Let suppose your array with all values is: $oRSList then to check value NumericID we need to iterate this array

$itemsWith63 = array();
foreach($oRSList as $item){
    if(strpos('63', $item['NumericID']) !== false){
        $itemsWith63[] = $item; 
    }
}

Open in new window

0
 
LVL 51

Expert Comment

by:Julian Hansen
ID: 41823072
array_filter()
<?php
$oRSList = array();
$oRSList[] = array('Name'=>'Apple','NumericID'=>'63');
$oRSList[] = array('Name'=>'Peach','NumericID'=>'237');
$oRSList[] = array('Name'=>'Grape','NumericID'=>'163');
$oRSList[] = array('Name'=>'Prune','NumericID'=>'168');
$oRSList[] = array('Name'=>'Mango','NumericID'=>'639');

$result = array_filter($oRSList, function($var) {
  return (strpos($var['NumericID'], '63') !== false);
});
echo "<pre>" . print_r($result, true) . "</pre>";

Open in new window

0

Featured Post

IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

Popularity Can Be Measured Sometimes we deal with questions of popularity, and we need a way to collect opinions from our clients.  This article shows a simple teaching example of how we might elect a favorite color by letting our clients vote for …
Foreword (July, 2015) Since I first wrote this article, years ago, a great many more people have begun using the internet.  They are coming online from every part of the globe, learning, reading, shopping and spending money at an ever-increasing ra…
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…
The viewer will learn how to look for a specific file type in a local or remote server directory using PHP.

747 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

12 Experts available now in Live!

Get 1:1 Help Now