将数据插入数组php pdo
我有以下代码:将数据插入数组php pdo,php,mysql,arrays,json,Php,Mysql,Arrays,Json,我有以下代码: $rows = array(); $table = array(); foreach($kol as $r) { $temp = array(); // the following line will be used to slice the Pie chart $m = array('label' => (string) $r['naziv'], 'type' => 'string
$rows = array();
$table = array();
foreach($kol as $r) {
$temp = array();
// the following line will be used to slice the Pie chart
$m = array('label' => (string) $r['naziv'], 'type' => 'string');
$rows[] = ($m);
}
$table['cols'] = $rows;
我得到了这个json:
{"cols":[{"label":"Pera Peric","type":"string"},{"label":"IMT 510-td","type":"string"},{"label":"Laza Lazic","type":"string"}
我如何将$m数组中的数据放置到位置0以获得如下json:
{"cols":[{"label":"Datum,"type":"Date"},{"label":"Pera Peric","type":"string"},{"label":"IMT 510-td","type":"string"},{"label":"Laza Lazic","type":"string"}
所以在这里我只想添加以下数据:
{“label”:“Datum”,type”:“Date”}到数组中…
在开始循环之前添加它(我稍微清理了一下):
只需在开始循环之前添加它(我清理了一点):
只需在开始循环之前添加它(我清理了一点):
只需在开始循环之前添加它(我清理了一点): 数组_unshift() 返回到json(如果需要) 数组_unshift() 返回到json(如果需要) 数组_unshift() 返回到json(如果需要) 数组_unshift() 返回到json(如果需要)
$rows = array();
$table = array();
$rows[] = array('label' => 'Datum', 'type' => 'Date')
foreach ($kol as $r) {
$rows[] = array('label' => (string) $r['naziv'], 'type' => 'string');
}
$table['cols'] = $rows;
<?php
// your json
$json = '{"cols":[{"label":"Pera Peric","type":"string"},{"label":"IMT 510-td","type":"string"},{"label":"Laza Lazic","type":"string"}]}';
// json array to php array using json_decode()
$json_decode = json_decode($json, true);
// your $m php array
$m = array(
'label' => 'Datum',
'type' => 'Date'
);
// add you $m to position 0 in index 'cols'
array_unshift($json_decode['cols'], $m);
array(1) {
["cols"]=>
array(4) {
[0]=>
array(2) {
["label"]=>
string(5) "Datum"
["type"]=>
string(4) "Date"
}
[1]=>
array(2) {
["label"]=>
string(10) "Pera Peric"
["type"]=>
string(6) "string"
}
[2]=>
array(2) {
["label"]=>
string(10) "IMT 510-td"
["type"]=>
string(6) "string"
}
[3]=>
array(2) {
["label"]=>
string(10) "Laza Lazic"
["type"]=>
string(6) "string"
}
}
}
$json = json_encode($json_decode);