Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/231.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/arrays/13.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/laravel/10.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php 如何从多维数组中获取值_Php_Arrays_Codeigniter - Fatal编程技术网

Php 如何从多维数组中获取值

Php 如何从多维数组中获取值,php,arrays,codeigniter,Php,Arrays,Codeigniter,我想从包含所有数据字段的数组中获取数据库的一个字段,这里是m代码: public function show($id) { $data['product'] = $this->products_model->get_product_by_id($id); foreach ($data['product'] as $row) { $seller = $row['seller_id']; } $

我想从包含所有数据字段的数组中获取数据库的一个字段,这里是m代码:

public function show($id)
    {

        $data['product'] = $this->products_model->get_product_by_id($id);

        foreach ($data['product'] as $row) {
        $seller = $row['seller_id'];
        }

        $data['seller_name'] = $this->members_model->get_members_by_id($seller);

        if (empty($data['product']))
        {
                show_404();
        }

        $data['main_content'] = 'pages/show';
        $this->load->view('templates/template', $data);
    }
我认为代码是不言自明的。我想要的是从产品数据库中获取卖方id字段,然后使用它从成员数据库中获取卖方名称字段。我认为它不起作用的代码是这一部分:

foreach ($data['product'] as $row) {
        $seller = $row['seller_id'];
    }
它给了我这个错误:

严重性:通知


消息:数组到字符串转换


使用
array\u column
在一个数组中获取所有
seller\u id
。就像
var\u dump array\u column($data,'seller\u id')
一样,使用
array\u column
将所有
seller\u id
获取到一个数组中。就像
var\u dump array\u column($data,'seller\u id')
一样

此消息意味着您将获得一个数组,并且您将其视为字符串,因为您将其作为字符串进行传递

所以这里有一个条件,要么您在
$data['product']
中得到多个值,要么是正确的,因为您在那里应用了foreach。所以,以这样的方式进行查询,它将从products表中只返回一行。这将是解决方案

此消息意味着您将获得一个数组,并且您将其视为字符串,因为您将其作为字符串进行传递


所以这里有一个条件,要么你在
$data['product']
中得到多个值,是的,这是正确的,因为你在那里应用了foreach。因此,让你的查询只返回products表中的一行。这将是一个解决方案,这就是我在查看页面中使用数组的方式,它确实起了作用:

foreach($seller_name as $row){
    echo'<p class="product-price">'.$row['user_name'].'</p>'; 
    echo'<p class="product-price">'.$row["phone_number"].'</p>';
}
foreach($seller\u名称为$row){
echo'

。$row['user\u name']。

; echo'

。$row[“电话号码”]。

; }
对不起,伙计们,问题出在查看页面上,这就是我在查看页面中使用数组的方式,它确实起了作用:

foreach($seller_name as $row){
    echo'<p class="product-price">'.$row['user_name'].'</p>'; 
    echo'<p class="product-price">'.$row["phone_number"].'</p>';
}
foreach($seller\u名称为$row){
echo'

。$row['user\u name']。

; echo'

。$row[“电话号码”]。

; }
您不需要foreach循环,只需检查
!空($data['product'])
并使用
$data['product']['seler\u id']
$data['product'][0]['seller\u id']
取决于您的型号代码。因此,由于您希望通过id获得确切的产品,您的代码应该如下所示:

public function show($id)
{
    $data['product'] = $this->products_model->get_product_by_id($id);

    if (empty($data['product']))
    {
        show_404();
    }
    else
    {
        $seller_id = $data['product']['seller_id'];
        $data['seller_name'] = $this->members_model->get_members_by_id($seller);
        $data['main_content'] = 'pages/show';
        $this->load->view('templates/template', $data);
    }
}

这里需要注意的一点是,由于表之间存在关系,所以模型中的查询应该包含JOIN部分,并且您可以在一个查询中获得卖方详细信息。基本上,您的$data['product']将在同一时间或在一个查询中包含名称(以及来自sellers表的其他详细信息)。检查。

您不需要foreach循环,只要检查
!空($data['product'])
并使用
$data['product']['seler\u id']
$data['product'][0]['seller\u id']
取决于您的型号代码。因此,由于您希望通过id获得确切的产品,您的代码应该如下所示:

public function show($id)
{
    $data['product'] = $this->products_model->get_product_by_id($id);

    if (empty($data['product']))
    {
        show_404();
    }
    else
    {
        $seller_id = $data['product']['seller_id'];
        $data['seller_name'] = $this->members_model->get_members_by_id($seller);
        $data['main_content'] = 'pages/show';
        $this->load->view('templates/template', $data);
    }
}


这里需要注意的一点是,由于表之间存在关系,所以模型中的查询应该包含JOIN部分,并且您可以在一个查询中获得卖方详细信息。基本上,您的$data['product']将在同一时间或在一个查询中包含名称(以及来自sellers表的其他详细信息)。检查。

代码的作用是什么?您是否收到任何错误消息?不知道$this->products\u model->get\u product\u by\u id($id)返回什么,但是如果它以数组形式从数据库返回一行数据,那么您应该能够设置$seller=$data['product']['seller\u id'];在那一行之后试试这个:
print\r($data['product'])
。我认为数组结构不是你想象的那样。我只是在ckimbrell上面添加了错误消息。这行代码运行良好。它从数据库返回一个数组,该数组包含基于给定id的所有字段。代码做什么?您是否收到任何错误消息?不知道$this->products\u model->get\u product\u by\u id($id)返回什么,但是如果它以数组形式从数据库返回一行数据,那么您应该能够设置$seller=$data['product']['seller\u id'];在那一行之后试试这个:
print\r($data['product'])
。我认为数组结构不是你想象的那样。我只是在ckimbrell上面添加了一条错误消息。这行代码工作得很好。它从数据库返回一个数组,其中包含基于给定id的所有字段。我不知道你的意思。你能再解释一下吗?你想检索
卖方id
,因此,我帮助您在一个单独的数组中检索所有
seller\u id
,如果您有条件地想要它,您需要通过循环。感谢我搜索了array\u列,我尝试了这个,
$seller=array\u列($product,'seller\u id')但它给了我相同的错误消息:数组到字符串的转换我不知道我是否正确使用了您的代码,但它给出了相同的错误
$seller=var\u dump(数组列($data,'seller\u id'))
对不起,我不知道你的意思,你能再解释一下吗?你想检索
卖家id
,所以我帮你在一个单独的数组中检索所有
卖家id
,如果你有条件地想要它,你需要通过循环。谢谢我搜索了array\u列,我尝试了这个,
$seller=array\u列($product,'seller\u id'));但它给了我相同的错误消息:数组到字符串的转换我不知道我是否正确使用了您的代码,但它给出了相同的错误
$seller=var\u dump(数组列($data,'seller\u id'))谢谢,我确信它只能得到一行,因为正如你在上面的代码中看到的那样,函数有$id参数,所以它只能得到一行并进行了测试,这就是为什么我对此非常确定。是的,
$id
一次作为一个参数传递,但我说的是
$data['product']=$this->products\u model->get\u product\u by\u id($id);这意味着从该模型通信