Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

PHP: array that only contains matching items from larger array

Posted on 2016-09-29
3
Medium Priority
?
89 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 23

Accepted Solution

by:
Ferruccio Accalai earned 2000 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 59

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

How to Use the Help Bell

Need to boost the visibility of your question for solutions? Use the Experts Exchange Help Bell to confirm priority levels and contact subject-matter experts for question attention.  Check out this how-to article for more information.

Question has a verified solution.

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

I imagine that there are some, like me, who require a way of getting currency exchange rates for implementation in web project from time to time, so I thought I would share a solution that I have developed for this purpose. It turns out that Yaho…
Build an array called $myWeek which will hold the array elements Today, Yesterday and then builds up the rest of the week by the name of the day going back 1 week.   (CODE) (CODE) Then you just need to pass your date to the function. If i…
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…
This tutorial will teach you the core code needed to finalize the addition of a watermark to your image. The viewer will use a small PHP class to learn and create a watermark.

705 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