Solved

PHP: array that only contains matching items from larger array

Posted on 2016-09-29
3
72 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 56

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

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

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…
This article discusses four methods for overlaying images in a container on a web page
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…
The viewer will learn how to create a basic form using some HTML5 and PHP for later processing. Set up your basic HTML file. Open your form tag and set the method and action attributes.: (CODE) Set up your first few inputs one for the name and …

713 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