0

I have the following array:

Array
(
   [id] => 1043847
   [company] => Array 
       ( 
          [businesstype] => Array 
                ( 
                   [0] => Array 
                       ( 
                          [id] => 6 
                          [name] => Business Service Provider 
                       )
                   [1] => Array
                       (
                          [id] => 8
                          [name] => Retailer 
                       ) 
                 )
         ) 
  )

I would like to be able to get the value of company -> businesstype -> name for all the arrays inside businesstype so it would display like this:

Business Type(s): Business Service Provider, Retailer

Right now if I do this:

<?php echo($chamber_member['company']['businesstype']['name']); ?>

Nothing happens, if I do this:

<?php echo($chamber_member['company']['businesstype'][0]['name']); ?>

I get only the one for array [0]. How can I get both values separated by a comma like on the example above? Any help is appreciated, thank you in advance!!

7 Answers7

5

This should work for you:

First get the column into an array with array_column(), then you can simply implode() this array ,e.g.

echo implode(",", array_column($chamber_member["company"]["businesstype"], "name"));
Rizier123
  • 58,877
  • 16
  • 101
  • 156
1
foreach($chamber_member['company']['businesstype'] as $a)
    $b[] = $a['name'];
$result = implode(", ", $b);
Mahdyfo
  • 1,155
  • 7
  • 18
0

Take names by array_column and cobine by implode

<?php echo implode(',', array_column($chamber_member['company']['businesstype'], 'name')); ?>
splash58
  • 26,043
  • 3
  • 22
  • 34
0

Try to loop into the array position with the 'businesstype' values:

for($i = 0; $i < count($chamber_member['company']['businesstype']); $i++){

    echo $chamber_member['company']['businesstype'][$i]['name'] . ', ';
}
0

I like array_column() but for completeness, just foreach():

foreach($chamber_member['company']['businesstype'] as $array) {
    echo $array['name'];
}

Or add to an array and implode().

AbraCadaver
  • 78,200
  • 7
  • 66
  • 87
0

Another solution using array_map():-

$arr=array
(
   'id' => 1043847,
   'company' => array 
       ( 
          'businesstype' => array 
                ( 
                   array 
                       ( 
                          'id'=> 6 ,
                          'name' => 'Business Service Provider' 
                       ),
                   array
                       (
                          'id' => 8,
                          'name' => 'Retailer'
                       ) 
                 )
         ) 
  );

  echo $names = implode(',',array_map( function($person) { return $person['name']; },$arr['company']['businesstype'])); 
Rajeev Ranjan
  • 4,152
  • 3
  • 28
  • 41
0

Use this. best answer for this..

$result=$chamber_member['company']['businesstype'];
$last=end($result);
for($i=0;$i<count($result);$i++)
{
$b[$i] = $result[$i]['name']; echo $b[$i]; if($last!=$result[$i]['name']){ echo ","; }

}