Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 3 of 3
  1. #1
    New Coder
    Join Date
    Jun 2012
    Posts
    42
    Thanks
    3
    Thanked 0 Times in 0 Posts

    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 )


  • #2
    God Emperor Fou-Lu's Avatar
    Join Date
    Sep 2002
    Location
    Saskatoon, Saskatchewan
    Posts
    16,994
    Thanks
    4
    Thanked 2,662 Times in 2,631 Posts
    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); 

  • #3
    Regular Coder
    Join Date
    May 2011
    Posts
    240
    Thanks
    1
    Thanked 56 Times in 55 Posts
    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); 


  •  

    Tags for this Thread

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •