0

i have following function where i have brands info in an array. i should get array containing those info when i pass brand name to this function.

function brand_info($brand)
{
    $brands_list=array ( 
    'lg'=>
    array(
    'name'           => 'LG Phone Company',
    'country'        =>  'country',
    'founded_year'   =>  '2001'
    ),
    'nokia'=>
    array(
    'name'           => 'Nokia Phone Company',
    'country'        =>  'country',
    'founded_year'   =>  '2001'
    )
    );


    if(in_array($brand,$brands_list))
    {
        // return array containg company info
    }
}

this should return an array by which i can show these info.

$brand_info=brand_info($brand_name);
echo $brand_info['name'];

what may be the best way to do this?

6
  • Do you mean return $brands_list[$brand];? Apparently you already know how to work with arrays, so what is the problem? Commented May 18, 2012 at 10:18
  • @felix yes exactly, but its returning Null, i don't know what i am missing Commented May 18, 2012 at 10:26
  • Well, if the brand is not in the array, it does not return anything... Commented May 18, 2012 at 10:27
  • @FelixKling i am passing 'nokia' to this function. you can see array contain 'nokia', but its not returning array. Commented May 18, 2012 at 10:33
  • Ah, in_array tests whether the value exists in the array. You are testing for a key, so it should be if(isset($brands_list[$brand])). Commented May 18, 2012 at 10:37

4 Answers 4

3

If you're passing in the brandname then this would suffice:

function brand_info($brand)
{
    $brands_list=array ( 
    'lg'=>
    array(
    'name'           => 'LG Phone Company',
    'country'        =>  'country',
    'founded_year'   =>  '2001'
    ),
    'Nokia'=>
    array(
    'name'           => 'Nokia Phone Company',
    'country'        =>  'country',
    'founded_year'   =>  '2001'
    )
    );

    if (array_key_exists($brand,$brands_list)) {
      return $brands_list[$brand];
    } else {
      return false;
    }
}

$brandinfo = brand_info('Nokia');
echo $brandinfo['name']; // will print "Nokia Phone Company"
Sign up to request clarification or add additional context in comments.

6 Comments

but its returning Null, i used var_dump($brandinfo); to show.
The above returns correctly for me, and it's not using anything that would depend on your PHP config, so it should be working for you.
Make sure you've typed all variables correctly @NaeemX2. This code is valid.
you are right... i am missing. its working now. thanks for your help.
You may have problems if you do not test weather the array key exists.
|
1

May seem trivial but...

return $brands_list[$brand]

Comments

1
function brand_info($brand)
{
    $brands_list=array ( 
    'lg'=>
    array(
    'name'           => 'LG Phone Company',
    'country'        =>  'country',
    'founded_year'   =>  '2001'
    ),
    'nokia'=>
    array(
    'name'           => 'Nokia Phone Company',
    'country'        =>  'country',
    'founded_year'   =>  '2001'
    )
    );


    if(in_array($brand,$brands_list))
    {
        return $brand_list[$brand];
    }else{
        return null;
    }
}

and then

$info = brand_info($my_brand);
if(!is_null($info)){ echo $info['name']; }

Comments

1
function brand_info($brand) {
    $brands_list=array (
        'lg'=>
            array(
                    'name'           => 'LG Phone Company',
                    'country'        =>  'country',
                    'founded_year'   =>  '2001'
            ),
        'Nokia'=>
            array(
                    'name'           => 'Nokia Phone Company',
                    'country'        =>  'country',
                    'founded_year'   =>  '2001'
            )
    );


    foreach ($brands_list as $brandname=>$info) {
        if($brandname==$brand) {
            return $info;
        }
    }
    return array();
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.