Solved

PHP: array that only contains matching items from larger array

Posted on 2016-09-29
3
78 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
[X]
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
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 9

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 57

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

Salesforce Has Never Been Easier

Improve and reinforce salesforce training & adoption using WalkMe's digital adoption platform. Start saving on costly employee training by creating fast intuitive Walk-Thrus for Salesforce. Claim your Free Account Now

Question has a verified solution.

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

Suggested Solutions

Developers of all skill levels should learn to use current best practices when developing websites. However many developers, new and old, fall into the trap of using deprecated features because this is what so many tutorials and books tell them to u…
This article discusses four methods for overlaying images in a container on a web page
The viewer will learn how to count occurrences of each item in an array.
The viewer will learn how to create and use a small PHP class to apply a watermark to an image. This video shows the viewer the setup for the PHP watermark as well as important coding language. Continue to Part 2 to learn the core code used in creat…

752 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