0

I have the array in php. I tried to convert some result

My code

for($la=0;$la < count($finaldata); $la++)
     { 
     
     ------------------
     -------------------
        $data_final2=array();
               foreach ($data_final1 as $line123){ 
                    $data_final2[] = $line123['answer_name'];
                }  
             $finaldata1[] = array("fullname"=>$fullname,"email"=>$email,"type"=>$type,"name"=>$name,"data_final2"=>$data_final2 ); 
    
     }     

Using above code i got below response

Inside the array i have the array, i tried array of array to norml key value response

 Array
            (
                [fullname] => John
                [email] => [email protected]
                [type] => Admin
                [name] => 
                [data_final2] => Array
                    (
                        [0] => IOS 8
                        [1] => Expert 
                        [2] => Sachin
                    )
    
            )
        
        

But i try to expected below, i tried some other ways it sowing issues or error in php

Expected array

Array 

(       
    [fullname] => John
    [email] => [email protected]
    [type] => Admin
    [name] => 
    [IOS 8] => IOS 8
    [Expert] =>Expert 
    [Sachin] => Sachin
    )
2
  • Can you show us the error you are getting? Also, please try being a bit more specific. What other ways have you tried? Commented Feb 28, 2021 at 19:04
  • See here: stackoverflow.com/questions/2289475/… Commented Feb 28, 2021 at 19:05

4 Answers 4

1
// Instead of:
$data_final2[] = $line123['answer_name']; ❌

// Use this:
$data_final2[$line123['answer_name']] = $line123['answer_name']; ✅
// Instead of:
 $finaldata1[] = array("fullname"=>$fullname,"email"=>$email,"type"=>$type,"name"=>$name,"data_final2"=>$data_final2 ); ❌

// Use this:
 $finaldata1[] = array_merge(array("fullname"=>$fullname,"email"=>$email,"type"=>$type,"name"=>$name), $data_final2); ✅

Sign up to request clarification or add additional context in comments.

Comments

0

You can use something like this

$finaldata = [
    'fullname' => 'John',
    'email' => '[email protected]',
    'type' => 'Admin',
    'name' => '',
    'data_final1' => [
        'IOS 8','Expert','Sachin',
    ],
];

$data_final2 = [];
foreach($finaldata['data_final1'] as $value){
    $data_final2[$value] = $value;
}
unset($finaldata['data_final1']);
$finaldata = array_merge($finaldata , $data_final2);

print_r($finaldata);

The output from this code is

Array
(
    [fullname] => John
    [email] => [email protected]
    [type] => Admin
    [name] => 
    [IOS 8] => IOS 8
    [Expert] => Expert
    [Sachin] => Sachin
)

Note: if you're using old php version use array() instead of []

Comments

0

It looks like fullname, email, type, and name can be created before the loop and can be assigned to $finaldata1.

$finaldata1 = compact('fullname', 'email', 'type', 'name');

Then looping through $data_final1 and add both key and value directly to $finaldata1

foreach ($data_final1 as $line123) {
    $finaldata1[$line123['answer_name']] = $line123['answer_name'];
}

So the final code in the loop should look like this.

for ($la = 0; $la < count($finaldata); $la++) {
    ...
    ...
    $finaldata1 = compact('fullname', 'email', 'type', 'name');

    foreach ($data_final1 as $line123) {
        $finaldata1[$line123['answer_name']] = $line123['answer_name'];
    }
}

Comments

0

This solution uses array_combine() and array_merge().

$data_final1 = $finaldata['data_final1'];
unset($finaldata['data_final1']);
$finaldata = array_merge($finaldata, array_combine($data_final1,$data_final1));

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.