Fadly Dzil
Fadly Dzil

Reputation: 2206

Implode array using two array_column

I have a result from a query like this : (IN PHP, I assigned to $arr_permasalahan)

+--------------+---------------------+
| total_detail | nama_detail         |
+--------------+---------------------+
|            2 | Create Email Baru   |
|            1 | Create Login Novell |
+--------------+---------------------+

Now I want to implode them, like this :

Create Email Baru  : 2 pcs,
Create Login Novel : 1 pcs

So, I decided it to use array_column like this :

 $output =  implode("\n", array_column($arr_permasalahan, 'nama_detail'));

I just get

"Create Email Baru\nCreate Login Novell"

Please advise.

Upvotes: 1

Views: 384

Answers (4)

B. Desai
B. Desai

Reputation: 16436

Try this solution

foreach($arr_permasalahan as $permasalahan)    
{
      echo $permasalahan['nama_detail']." : ".$permasalahan['total_detail']." pcs"."<br>";     
}

Upvotes: 1

Krishna Gupta
Krishna Gupta

Reputation: 685

You can impload array values using impload()

Below mrentioned code will be full fill your requirement

$arr = array(1 => array('total_detail' => 2, 'nama_detail' => 'Create Email Baru'), 2 => array('total_detail' => 1, 'nama_detail' => 'Create Login Novell'));

  foreach ($arr as $i => $v) {
    $ss['nama_detail'] = $v['nama_detail'];
    $ss['total_detail'] = $v['total_detail'].' pcs';
    echo implode("':'",$ss)."' pcs<br>";
  }

Actually your result array is multi level array (2d-array) and impload() works for single level array so you must need to put a foreach loop. See your required result here

Upvotes: 1

Suchit kumar
Suchit kumar

Reputation: 11859

you can try this:

$elemet=implode("<br>",array_map(function($x){
    return $x['nama_detail'].":".$x['total_detail']." pcs";
},$arr_permasalahan));
echo $elemet;

NOTE: what @Sougata Bose suggested in comment will be the best option.

Upvotes: 1

Prudhvi Raj Kumar
Prudhvi Raj Kumar

Reputation: 81

Try to use like this, it may help..! I think there is not need to use implode in this

     $line = "";
     foreach($arr_permasalahan as $line)
     echo "".$line[' nama_detail']." : ".$line['total_detail']." pcs"; 

Upvotes: 2

Related Questions