1

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.

1
  • You can do it in the query only - CONCAT(nama_detail, ' : ', total_detail, ' pcs') and then print it as you need. Commented Feb 1, 2017 at 6:22

4 Answers 4

2

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"; 
Sign up to request clarification or add additional context in comments.

2 Comments

I have tried this way, it success, but too oldiest style.
There is no benefit in prepending an empty string to the other data.
1

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.

Comments

1

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

Comments

1

Try this solution

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

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.