Go Back   CodingForums.com > :: Server side development > PHP

Before you post, read our: Rules & Posting Guidelines

Reply
 
Thread Tools Rate Thread
Enjoy an ad free experience by logging in. Not a member yet? Register.
Old 12-05-2012, 05:15 PM   PM User | #1
frickettz
New Coder

 
Join Date: Jun 2012
Posts: 42
Thanks: 3
Thanked 0 Times in 0 Posts
frickettz is an unknown quantity at this point
Selecting one row of a multidimensional array under condition?

Is it possible to select all the values of one particular row IF and only if the room_id key == 3 in the row's nested array?

How could I do that without knowing the parent key for each row?

PHP Code:
Array ( 
[
0] => Array ( [room_id] => [room] => Living [affiliate_url] => link3 
[
1] => Array ( [room_id] => [room] => Kitchen [affiliate_url] => link1 )
[
2] => Array ( [room_id] => [room] => Bed [affiliate_url] => link7 )

frickettz is offline   Reply With Quote
Old 12-05-2012, 06:56 PM   PM User | #2
Fou-Lu
God Emperor


 
Fou-Lu's Avatar
 
Join Date: Sep 2002
Location: Saskatoon, Saskatchewan
Posts: 15,752
Thanks: 4
Thanked 2,468 Times in 2,437 Posts
Fou-Lu is a name known to allFou-Lu is a name known to allFou-Lu is a name known to allFou-Lu is a name known to allFou-Lu is a name known to allFou-Lu is a name known to all
You can yes. There are a number of ways to determine this from filtering to iteration. Given what you have here though, is there a reason that the room_id isn't specified as the index for the outer array instead of as an item within the inner array?
I'd probably use a filter. The filter's only downside is its signature is not changeable, so you have to use global references in order to modify what you are looking for.
PHP Code:
function filterArray($item)
{
    global 
$iRoomNum;
    
$bResult false;
    
$roomID = (int)$iRoomNum;

    if (
is_array($item) && isset($item['room_id']) && $item['room_id'] == $roomID)
    {
        
$bResult true;
    }
    return 
$bResult;
}

$iRoomNum 3;
$yourArray = Array ( 
    Array ( 
'room_id' => 3'room' => 'Living''affiliate_url' => 'link3' ),
    Array ( 
'room_id' => 1'room' => 'Kitchen''affiliate_url' => 'link1' ),
    Array ( 
'room_id' => 7'room' => 'Bed''affiliate_url' => 'link7' )
) ;
$aFiltered array_filter($yourArray'filterArray');
print_r($aFiltered); 
Fou-Lu is offline   Reply With Quote
Old 12-06-2012, 07:58 PM   PM User | #3
gvre
Regular Coder

 
Join Date: May 2011
Posts: 214
Thanks: 1
Thanked 50 Times in 49 Posts
gvre is an unknown quantity at this point
The best solution is to specify the room_id as the index of the outer array, as Fou-Lu suggested. If this is not possible, the solution depends on the size of the outer array and the uniqueness of the room_id. If the outer array is too big you could use the following function, that returns immediately when room_id matches $roomId.

PHP Code:
function filterArray($array$roomId)
{
        if (
is_array($array))
        {
                foreach(
$array as $room)
                        if (
$room['room_id'] == $roomId)
                                return 
$room;
        }
        return 
null;

If the outer array is not big, you could use the Fou-Lu's solution or the following one (php 5.3 or greater is required).

PHP Code:
$roomId 3;
$aFiltered array_filter($yourArray, function ($room) use ($roomId) { return $room['room_id'] == $roomId; });
if (isset(
$aFiltered[0])) 
        
$aFiltered $aFiltered[0];
print_r($aFiltered); 
gvre is offline   Reply With Quote
Reply

Bookmarks

Tags
array, multidimensional, selecting row

Jump To Top of Thread


Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT +1. The time now is 10:47 PM.


Advertisement
Log in to turn off these ads.