1

can I ask for your help? I have values in the array and I want to combine them to become one value. I want a result that 'gendesc' 'dmdnost' 'stredesc' 'formdesc' 'rtedesc' will be in one column. Thanks.

 foreach($data as $item){
        $tmp = array();

        $tmp_item = (array) $item;

        $tmp['description'] = $tmp_item['gendesc'];
        $tmp['m1'] = $tmp_item['dmdnost'];
        $tmp['m2'] = $tmp_item['stredesc'];
        $tmp['m3'] = $tmp_item['formdesc'];
        $tmp['m4'] = $tmp_item['rtedesc'];


        $final_data [] = $tmp;

    }
    print_r($final_data);
2
  • one column with name? What structure you want? Commented Sep 30, 2019 at 3:16
  • First, let us know the structure of the original $data. then the structure of the expected stucture. Commented Sep 30, 2019 at 4:06

3 Answers 3

1

Can you try

foreach($data as $item){
    $tmp_item = (array) $item;

    $tmp = $tmp_item['gendesc']
    . ' ' . $tmp_item['dmdnost']
    . ' ' . $tmp_item['stredesc']
    . ' ' . $tmp_item['formdesc']
    . ' ' . $tmp_item['rtedesc'];


    $final_data[] = array('description' => $tmp);

}
print_r($final_data);
Sign up to request clarification or add additional context in comments.

Comments

1

You want this?

foreach($data as $item){
    $tmp = array();

    // ver.1
    $tmp['description'] = $item['gendesc']
      . $item['dmdnost']
      . $item['stredesc']
      . $item['formdesc']
      . $item['rtedesc'];

    // ver.2 also you can use one-line shortcut with implode instead of ver.1
    $tmp['description'] = trim(implode(" , ", $item));

    $final_data[] = $tmp;

}
print_r($final_data);

Comments

1

Try this,

$i =0;
foreach($data as $item)
{
$tmp = [];
$tmp[$i]['description'] = $item['gendesc'];
$tmp[$i]['m1'] = $item['dmdnost'];
$tmp[$i]['m2'] = $item['stredesc'];
$tmp[$i]['m3'] = $item['formdesc'];
$tmp[$i]['m4'] = $item['rtedesc'];
$i++;
}
print_r($tmp);

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.