我的PHP代码是
public function getAllAttributes()
{
$this->dao->select('b_title');
$this->dao->from($this->getTable_buttonsAttr());
$result = $this->dao->get();
if( !$result ) {
return array() ;
}
return $result->result();
}
$details = Modelbuttons::newInstance()->getAllAttributes();
$string = implode(', ', $details);
var_dump ($string) ; ?>
我得到一个看起来像这样的数组:
array (size=6)
0 =>
array (size=1)
'b_title' => string 'test 10' (length=12)
1 =>
array (size=1)
'b_title' => string 'test 11' (length=12)
2 =>
array (size=1)
'b_title' => string 'test 12' (length=13)
3 =>
array (size=1)
'b_title' => string 'test 13' (length=8)
4 =>
array (size=1)
'b_title' => string 'test 14' (length=14)
5 =>
array (size=1)
'b_title' => string 'test 15' (length=32)
如何使用PHP将此数组转换为这样的字符串?
$out = '10, 11, 12, 13, 14,15';
答案 0 :(得分:0)
您可以尝试使用此代码。
$array = [['b_title' => 'test 10'],['b_title' => 'test 11']];
foreach($array as $a) {
$values[] = explode(' ', $a['b_title'])[1];
}
echo implode(', ', $values);
答案 1 :(得分:0)
基本示例:
<?php
// your array
$array = array(
array('b_title'=>'test 10'),
array('b_title'=>'test 11'),
array('b_title'=>'test 12'),
array('b_title'=>'test 13')
);
$newArr = array();
foreach ($array as $key => $value) {
$newVal = explode(" ", $value['b_title']);
$newArr[] = $newVal[1];
}
echo implode(',', $newArr); // 10,11,12,13
?>
<强>解释强>
首先使用explode()
作为字符串值,帮助您根据需要拆分字符串:
$newVal = explode(" ", $value['b_title']);
您可以将索引1存储在数组中:
$newArr[] = $newVal[1]; // array(10,11,12,13)
最后,您只需使用implode()
函数来帮助您获取逗号分隔数据。
echo implode(',', $newArr); // 10,11,12,13
答案 2 :(得分:0)
另一种方法。
$details = Modelbuttons::newInstance()->getAllAttributes(); /*[['b_title' => 'test 10'],['b_title' => 'test 11']];*/
$token = "";
foreach($details as $row1){
$token = $token.$row1['b_title'].", ";
}
echo rtrim(trim($token),",");
输出:测试10,测试11
<强> 解释 强>
通过foreach,所有数组值现在都以逗号分隔。
截至目前,输出将为test 10, test 11,
。因此,最后通过rtrim()删除额外的逗号。
答案 3 :(得分:0)
import { Component, OnInit } from '@angular/core';
import { HttpService } from 'services/http.service'
@Component({
selector: 'my-analytics',
styleUrls: ['../../../node_modules/@telerik/kendo-theme-default/dist/all.css'],
template: require('./analytics.component.html')
})
export class AnalyticsComponent implements OnInit {
private gridData: any[] = [{
"ProductID": 1,
"ProductName": "Chai",
"UnitPrice": 18.0000,
"Discontinued": false,
"Category": {
"CategoryID": 1,
"CategoryName": "Beverages",
"Description": "Soft drinks, coffees, teas, beers, and ales"
}
}, {
"ProductID": 2,
"ProductName": "Chang",
"UnitPrice": 19.0000,
"Discontinued": false,
"Category": {
"CategoryID": 1,
"CategoryName": "Beverages",
"Description": "Soft drinks, coffees, teas, beers, and ales"
}
}, {
"ProductID": 3,
"ProductName": "Aniseed Syrup",
"UnitPrice": 10.0000,
"Discontinued": false,
"Category": {
"CategoryID": 2,
"CategoryName": "Condiments",
"Description": "Sweet and savory sauces, relishes, spreads, and seasonings"
}
}];
constructor(private http_service: HttpService) { }
ngOnInit() {
}
}
这给出了你想要的结果。虽然在操作等之前检查数组的格式是否正确,但它可以在那里做一些条件。
答案 4 :(得分:0)
有很多方法可以给这只猫上皮 - 这是一个:
<?php
$out = '';
foreach ($array as $arrayItem) {
$out .= $arrayItem['b_title'] . ', ';
}
// Remove the extraneous ", " from the $out string.
$out = substr($out, 0, -2);
?>
或者,如果您还想删除&#34;测试&#34; “b_title”的一部分&#39; b_title&#39;关键的价值;然后你可以像这样完成它:
<?php
$out = '';
foreach ($array as $arrayItem) {
$out .= str_replace('test ', '', $arrayItem['b_title']) . ', ';
}
// Remove the extraneous ", " from the $out string.
$out = substr($out, 0, -2);
?>
答案 5 :(得分:0)
$test_array = array(
0 => array('b_title' => 'test 10'),
1 => array('b_title' => 'test 11'),
2 => array('b_title' => 'test 12'),
3 => array('b_title' => 'test 13'),
4 => array('b_title' => 'test 14'),
5 => array('b_title' => 'test 15'),
);
$result = "";
$result1 = array_walk($test_array, function ($value, $key) use(&$result) {
$result .= ",".array_pop(explode(" ", reset($value))).",";
$result = trim($result, ",");
});
输出:$ out =&#39; 10,11,12,13,14,15&#39;;
<强>解释强>
使用PHP的本机array_walk()迭代数组,这将减少forloops,然后使用explode(),array_pop()和trim()来准备数组,这可能是准备所需字符串所必需的。
答案 6 :(得分:0)
你也可以尝试这个
$check = array(array ('b_title' => array('test 10'))
,array('b_title' => array('test 11'))
,array('b_title' => array('test 12'))
,array('b_title' => array('test 13'))
,array('b_title' => array('test 14'))
,array('b_title' => array('test 15')));
$srt = "";
foreach($check as $kye => $val)
{
$title = $val['b_title'][0];
$var = explode(' ',$title);
$srt .= trim($var[1]).', ';
}
$srt = substr(trim($srt),0,-1);
答案 7 :(得分:-1)
您可以使用上述网址解决方案Click here
foreach ($arr as $k=>$val) {
$v = explode (' ', $val['b_title']);
$out .= $v[1].',';
}
$out = rtrim ($out, ',');
echo '<pre>'; print_r($out); die;
答案 8 :(得分:-1)
试试这个,
foreach($test_array as $val)
{
$new_array[] = preg_replace('/\D/', '', $val['b_title']);
}
$out = implode(',', $new_array);
echo $out;
<强>输出强>
10,11,12,13,14,15
<强> DEMO 强>