简体   繁体   English

在codeigniter中将数组添加到具有键值的另一个数组

[英]adding array into another array with key value in codeigniter

In CodeIgniter,adding array into another array with key value pairs. 在CodeIgniter中,将数组添加到具有键值对的另一个数组中。

and I have an array in this format 我有这种格式的数组

Array 
    ( [0] => Array ( 
        [memberName] => Ram
        [address] => Abc 
        [phoneNo] =>456422313
        [email] => abc@abc.com
        [age] => 25 
        )
    ) 

I have to insert this array into $data['memberInfo'] with key values. 我必须将此数组插入具有键值的$data['memberInfo'] So when I echo $memberInfo->memberName in my view, I could get the correct value of memberName; 因此,当我在视图中回显$memberInfo->memberName时,可以获得正确的memberName;memberName;

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

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

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. $ val是另一个数组。

Try this, Here i use simple index() method and pass data to view file as an example. 试试这个,这里我以简单的index()方法为例,将数据传递给查看文件。

You can use below code and test in your codeigniter. 您可以使用下面的代码并在您的codeigniter中进行测试。

Hope this will work for you. 希望这对您有用。

Welcome.php (Controller) Welcome.php(控制器)

public function index(){
    $array = array 
        ( array ( 
            'memberName' => 'Ram',
            'address' => 'Abc', 
            'phoneNo' => '456422313',
            'email' => 'abc@abc.com',
            'age' => 25 
            )
        );

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

welcome_message.php (View) welcome_message.php(查看)

<?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 产量

在此处输入图片说明

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM