Mcg
Mcg

Reputation: 31

adding array into another array with key value in codeigniter

In CodeIgniter,adding array into another array with key value pairs.

and I have an array in this format

Array 
    ( [0] => Array ( 
        [memberName] => Ram
        [address] => Abc 
        [phoneNo] =>456422313
        [email] => [email protected]
        [age] => 25 
        )
    ) 

I have to insert this array into $data['memberInfo'] with key values. So when I echo $memberInfo->memberName in my view, I could get the correct value of memberName;

Upvotes: 0

Views: 2074

Answers (4)

Dulanjana K
Dulanjana K

Reputation: 31

$data['memberInfo'] = $Youarray[0];

Upvotes: 0

Mani Ratna
Mani Ratna

Reputation: 911

You can try this:

 $data= array('mem_info' => array());
    foreach($val as $key => $value){
           $mem_info= array(
                        array(
                            'memberName' => $value->name,
                            'address' => $value->address, 
                            'phoneNo' => $value->phone',
                            'email' => $value->email,
                            'age' => $value->age
                        )
                    );

            array_push($data['mem_info'], $mem_info[0]);
    }
    var_dump($data['mem_info']);

Here $val is another array.

Upvotes: 1

Nick
Nick

Reputation: 561

Try this, Here i use simple index() method and pass data to view file as an example.

You can use below code and test in your codeigniter.

Hope this will work for you.

Welcome.php (Controller)

public function index(){
    $array = array 
        ( array ( 
            'memberName' => 'Ram',
            'address' => 'Abc', 
            'phoneNo' => '456422313',
            'email' => '[email protected]',
            'age' => 25 
            )
        );

    $data['memberInfo'] = $array[0]; 
    $this->load->view('welcome_message', $data);
}

welcome_message.php (View)

<?php
    echo $memberInfo['memberName'];
    echo '<br>';
    echo $memberInfo['address'];
    echo '<br>';
    echo $memberInfo['phoneNo'];
    echo '<br>';
    echo $memberInfo['email'];
    echo '<br>';
    echo $memberInfo['age'];
    echo '<br>';
?>

Output

enter image description here

Upvotes: 0

user11487450
user11487450

Reputation:

// Controller
$data = array(
    'memberInfo' => array()
    // ...
);
$memberInfo = array(
    array(
        'memberName' => 'Ram',
        'address' => 'Abc',
        'phoneNo' => 456422313,
        'email' => '[email protected]',
        'age' => 25
    )
);
$data['memberInfo'] = array_merge($data['memberInfo'], $memberInfo[0]);

// View
echo $data['memberInfo']['memberName'];

Upvotes: 2

Related Questions