Reputation: 31
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
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
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
Upvotes: 0
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