Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people, just like you, are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
Solved

PHP: array that only contains matching items from larger array

Posted on 2016-09-29
3
66 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 8

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 55

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

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

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 …
This article will explain how to display the first page of your Microsoft Word documents (e.g. .doc, .docx, etc...) as images in a web page programatically. I have scoured the web on a way to do this unsuccessfully. The goal is to produce something …
Learn how to match and substitute tagged data using PHP regular expressions. Demonstrated on Windows 7, but also applies to other operating systems. Demonstrated technique applies to PHP (all versions) and Firefox, but very similar techniques will w…
The viewer will learn how to look for a specific file type in a local or remote server directory using PHP.

856 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