0

a quick answer about how to get the value (and pass it in a php variable) from an array:

i've this array:

$array = array(
            array('name' => 'infotel', 'value' => '+39080123456' ), 
            array('name' => 'location', 'value' => 'Bari')
            );

I need to pass the "value" of "infotel" in a $telephone variable, and the "value" of "location" in a $city variable.

how can i solve this trouble? thanks

1
  • Try $telephone = $array[0]['name']; $location = $array[1]['name']; Commented Jul 23, 2014 at 6:17

6 Answers 6

3

you can create a function for that.

function custom_search($search_for='',$search_in=array())
{
  if($search_for=='' OR empty($search_in)) return '';
    foreach($search_in as $val)
    {
       if($val['name']==$search_for) {return $val['value'];}
    }
  return '';
}
$telephone=custom_search("infotel",$array);
Sign up to request clarification or add additional context in comments.

1 Comment

Don't forget the location part of his question as-well ;-)
1

I might do it using the new array_column()(requires PHP >= 5.5) and array_walk().

The good 'ol foreach loop should be fine, or you could just pull them out directly assuming you know what is in the array all the time. But here's something I think is a little fancier ;) ...

$arr = array_column($array, 'value', 'name');

array_walk($arr, function(&$v, $k) use (&$telephone, &$city){
    if ($k == 'infotel') $telephone = $v;
    elseif ($k == 'location') $city = $v;
});

echo $telephone; //+39080123456
echo $city;      //Bari

See Demo - updated updated updated

3 Comments

prettier answer i should have up voted too but when no results are matched in array it will give Undefined index: telephone and Undefined index: city. initializing like this can solve the issues $result = array('telephone'=>'','city'=>''); :)
@karanthakkar Your observation sparked a whole series of changes, from which I think I have improved the function greatly. Thanks!
glad to help mark :) i guess i need to upgrade to >5.5 :) +1 for a better edit
0

If you have only this array, use:

$tel = $array[0]['value'];
$loc = $array[1]['value'];

1 Comment

thanks, but array was dinamically generated, and I can't know if the offset [0] is always tel.
0
$array = array(
    array('name' => 'infotel', 'value' => '+39080123456' ),
    array('name' => 'location', 'value' => 'Bari')
);

$telephone = $array[0]['value'];
$city = $array[1]['value'];
echo $telephone;

echo $city;

1 Comment

Hiya, this may well solve the problem... but it'd be good if you could provide a little explanation about how and why it works :) Don't forget - there are heaps of newbies on Stack overflow, and they could learn a thing or two from your expertise - what's obvious to you might not be so to them.
0
$telephone='';
$location='';
foreach($array as $k=>$v)
{
   if($v['name']=="infotel") 
   {
     $telephone=$v['value'];
     break;
   }
   if($v['name']=="location") 
   {
     $location=$v['value'];
     break;
   }
}
echo $telephone; 
echo $location;

1 Comment

Hiya, this may well solve the problem... but it'd be good if you could provide a little explanation about how and why it works :) Don't forget - there are heaps of newbies on Stack overflow, and they could learn a thing or two from your expertise - what's obvious to you might not be so to them.
0

You can get your desire result by using this code. you need not to worry about array keys. but last key will replace your location and telephone value.

$array = array(
            array('name' => 'infotel', 'value' => '+39080123456' ), 
            array('name' => 'location', 'value' => 'Bari')
            );
$key1 = '';
$key2 = '';
foreach($array as $k => $a){
    if(array_search('infotel', $a) != false){
        $key1 = $k;
    }
    if(array_search('location', $a) != false){
        $key2 = $k;
    }
}
echo 'telephone = '.$array[$key1]['value'];
echo 'location = '.$array[$key2]['value'];

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.