如何 implode 和 foreach get
How to implode and foreach get
我尝试获取每个“名称”并将其发送到我的数据库进行验证
问题是我不能把每一个都拿走并通过 foreach
$name = $this->UserSkills->where('id', session()->get('id'))->first();
print_r($name) // return Array (
[id] => 5
[user] => 15
[name] => name,name1,name2,name3,name4
)
$name = implode(",", $name['name']);
foreach($name as $row) {
$setname[] = $this->checkName->getIDbyName($row['name']);
}
$setname = implode(",", $setname);
$data['id_name'] = $setname;
终于要他回来了
echo $data['id_name'] // return: 1,4,8,367, etc...
你第一次使用 implode
是不正确的,你需要 explode
把一个字符串做成一个数组。
<?php
$name = $this->UserSkills->where('id', session()->get('id'))->first();
$name = explode(",", $name['name']);
foreach($name as $row) {
$setname[] = $this->checkName->getIDbyName($row);
}
$setname = implode(",", $setname);
$data['id_name'] = $setname;
我认为你的 $name['name']
return 是 string
而不是 array
,所以你需要 explode()
而不是 implode()
.
尝试这样的事情:
$name = $this->UserSkills->where('id', session()->get('id'))->first();
print_r($name); // return Array ([id] => 5, [user] => 15, [name] => name,name1,name2,name3,name4)
$names = explode(',', $name['name']); // return Array (0 => name, 1 => name1, 2 => name2, 3 => name3, 4 => name4)
foreach ($names as $row) {
$setname[] = $this->checkName->getIDbyName($row['name']);
}
$setname = implode(",", $setname); // return 1,4,8,367, etc
$data['id_name'] = $setname; // return 1,4,8,367, etc
echo $data['id_name']; // return 1,4,8,367, etc
假设您的 getIDbyName()
return ID
为 integer
我尝试获取每个“名称”并将其发送到我的数据库进行验证 问题是我不能把每一个都拿走并通过 foreach
$name = $this->UserSkills->where('id', session()->get('id'))->first();
print_r($name) // return Array (
[id] => 5
[user] => 15
[name] => name,name1,name2,name3,name4
)
$name = implode(",", $name['name']);
foreach($name as $row) {
$setname[] = $this->checkName->getIDbyName($row['name']);
}
$setname = implode(",", $setname);
$data['id_name'] = $setname;
终于要他回来了
echo $data['id_name'] // return: 1,4,8,367, etc...
你第一次使用 implode
是不正确的,你需要 explode
把一个字符串做成一个数组。
<?php
$name = $this->UserSkills->where('id', session()->get('id'))->first();
$name = explode(",", $name['name']);
foreach($name as $row) {
$setname[] = $this->checkName->getIDbyName($row);
}
$setname = implode(",", $setname);
$data['id_name'] = $setname;
我认为你的 $name['name']
return 是 string
而不是 array
,所以你需要 explode()
而不是 implode()
.
尝试这样的事情:
$name = $this->UserSkills->where('id', session()->get('id'))->first();
print_r($name); // return Array ([id] => 5, [user] => 15, [name] => name,name1,name2,name3,name4)
$names = explode(',', $name['name']); // return Array (0 => name, 1 => name1, 2 => name2, 3 => name3, 4 => name4)
foreach ($names as $row) {
$setname[] = $this->checkName->getIDbyName($row['name']);
}
$setname = implode(",", $setname); // return 1,4,8,367, etc
$data['id_name'] = $setname; // return 1,4,8,367, etc
echo $data['id_name']; // return 1,4,8,367, etc
假设您的 getIDbyName()
return ID
为 integer