Jump to content

[SOLVED] Get multiple values from array based on somevalue??


elefant

Recommended Posts

I can't figure the logic of this out... The number of functions for dealing with arrays is just overwhelming.

 

The scenario is this. The first multi-d associative...

$item_list	= array(

someitem1	=> array(	name	=>	"Some Item",
			desc	=>	"Description of the item",
			price     =>	'$$.cc'
),

 

And in another multi-d associative...

$customer_list = array(

someperson	=> array(	fname	=>	"FIRST",
			lname	=>	"LAST",
			more	=>	"more stuff",
			favoriteitem     =>	'someitem1'
),

 

So, in the second multi-d associative array there may be more than one occurence of someitem1 being the favorite item... And what I want to do is generate a list where for every instance of someitem1 I can display more info about that person... But I don't want to see info about people that don't have someitem1 as their favorite item.

 

Sorry if this is stupid question  :'( Not even sure if you needed to know about the first array...

try

<?php
  
$customer_list = array(

    'someperson1'	=> array(	
                    'fname'	=>	"FIRST",
			    'lname'	=>	"LAST",
			    'more'	=>	"more stuff",
			    'favoriteitem'     =>	'someitem1'
                    ),
    'someperson2'	=> array(	
                    'fname'	=>	"FIRST",
			    'lname'	=>	"LAST",
			    'more'	=>	"more stuff",
			    'favoriteitem'     =>	'someitem2'
                    ),
    'someperson3'	=> array(	
                    'fname'	=>	"FIRST",
			    'lname'	=>	"LAST",
			    'more'	=>	"more stuff",
			    'favoriteitem'     =>	'someitem1'
                    )
);

$search_item = 'someitem1';
$selected = array();

foreach ($customer_list as $k => $cdata)
{
    if ($cdata['favoriteitem'] == $search_item) 
        $selected[$k] = $cdata;
}

echo '<pre>', print_r($selected, true), '</pre>';
?>

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.