I would like to use array variable in PostgreSQL array_append(). If I use array directly then its works but not when using variable.
$name= {1,2,3};
$Name_key_array={4};
I would like to find result by using following way-
$name='array_append(name, $Name_key_array)';
or
"SELECT array_append($Books->name, $Name_key_array) as b";
Waiting to see some great ideas.
Thanks to all
personally, I use json.
<?php $json_var = json_encode($array)
$sql="insert into blah (arr) values (select array_agg(a::text) from json_array_elements(:json_var)";
Related
This is the data that i need to extract like example profile_contact_numbers
so the output will be +639466276715
how can i do it in php code??
any help will do regards
a:2:
{s:23:"profile_contact_numbers";s:13:"+639466276715";s:16:"profile_position";s:7:"Courier";}
I'm not sure 100% this can go into an array but try the unserialize function
$json_resp = {your values};
$array[] = unserialize($json_resp);
To check if it has gone into an array print_r on $array.
Read this link if the code above doesn't work
http://php.net/manual/en/function.unserialize.php
I have managed to fix it
$serialized = array(unserialize('a:2:{s:23:"profile_contact_numbers";s:13:"+639466276715";s:16:"profile_position";s:7:"Courier";}'));
var_dump($serialized);
use code :
$var = preg_split('["]','{s:23:"profile_contact_numbers";s:13:"+639466276715";s:16:"profile_position";s:7:"Courier";}');
echo $var[1].'='.$var[3]; // profile_contact_numbers=+639466276715
echo $var[5].'='.$var[7]; // profile_position=Courier
I have retrieved some data from db using:
$filtered = DB::table('tmp_table')->distinct()->select('type','description','note')->get();
and I would like to insert what I have retrieved in another table like:
DB::table('tmp_other')->insert($filtered);
but I receive thie error:
Type error: Argument 1 passed to Illuminate\Database\Query\Builder::insert() must be of the type array, object given
which is the best way to do this?
The SQL way to do blunk inserts of this is something more native like :
DB::insert(
"INSERT INTO tmp_other (type,description,note)
SELECT DISTINCT type,description,note FROM tmp_table"
);
This will avoid the whole transfer to webserver/transfer back to the SQL server process.
this will do the trick for you try this :)
$filtered = DB::table('tmp_table')->distinct()->select('type','description','note')->get();
$filtered->toArray();
DB::table('tmp_other')->insert($filtered);
This will do the trick :)
$filtered = DB::table('tmp_table')->distinct()->select('type','description','note')->get()->toArray();
$to_fill = [];
foreach($filtered as $record) {
$to_fill[] = (array)$record;
}
DB::table('tmp_other')->insert($to_fill);
You can do it one line, like this:
Region::insert($tempCollection->toArray());
I have found that:
Region::insert($tempCollection->toArray());
Doesn't work when you have appends in the model. This does not happen if you use getAttributes on each model. Therefore, this should be safe:
Region::insert($tempCollection->map(fn($x) => $x->getAttributes())->toArray()
I'm breaking my brain on a array from a query in MYSQL that i want to pass to a javascript array.
In the query i select the array with a GROUP CONCAT and the outcome looks like:
1358121600,1,1,0,0,0,0,0,0,1358380800,2,2,0,0,0,0,0,0,1358640000,1,1,0,0,0,0,0,0,1360454400,3,3,0,0,0,0,0,0,1360972800,1,1,0,0,0,0,0,0
But if i use JSON_Encode like this:
<?php echo 'var prijzen = new Array('.json_encode($array_prijzen).');'; ?>
I looks like the array is filled and i can also alert the array, but if i alert prijzen[0] it gives "undefined".
The following code should fix your problem:
<?php echo 'var prijzen = ['.$array_prijzen.'];'; ?>
Building on #datasages 's answer. If $array_prijzen is a genuine php array, then the following will work. I think datasages's answer is based on the fact that your variable named $array_prijzen is actually a string (which seems to be the case). But if it's an array, then do the following (i created a five element array as an example):
<?php $array_prijzen = array(1358121600,1,1,0,0); echo 'var prijzen = ['.implode(",",$array_prijzen).'];'; ?>
I want to capture all posted data in one single line like name=asd&age=12&city=something and as an array while the data were posted by using form. (I don't wanna capture values like "$name=$_POST['name']")
(Ques-1: as a single line.
Ques-2: as an array.)
How can I do that ?
-Thanks.
I don't quite understand what you want, but I think you're looking for the following:
On a single line:
$raw_data = file_get_contents("php://input");
in an array
$array_data = $_POST // this is already an array?
$postAsLine = file_get_contents("php://input");
$postAsArray = $_POST;
echo $_SERVER['QUERY_STRING'] for current GET query
http_build_query for any array
in your case http_build_query($_POST)
You can use http_build_query($yourKeyValArray) which creates a query string from an array.
it sound like it:
$getAll = serialize($_POST);
$getAsArray = $_POST;
GOOD LUCK
I have a script that is using the google charts API and the gChart wrapper.
I have an array that when dumped looks like this:
$values = implode(',', array_values($backup));
var_dump($values);
string(12) "8526,567,833"
I want to use the array like this:
$piChart = new gPieChart();
$piChart->addDataSet(array($values));
I would have thought this would have looked like this:
$piChart->addDataSet(array(8526,567,833));
Howerver when I run the code it creates a chart with only the first value.
Now when I hardcode the values in instead I get each value in the chart.
Does anyone know why it's acting this way?
Jonesy
I think
$piChart->addDataSet(array_values($backup));
// or just: $piChart->addDataSet($backup); depends on $backup
should do it.
$values only contains a string. So if you do array($values), you create an array with one element:
$values = "8526,567,833";
print_r(array($values));
gives
Array
(
[0] => 8526,567,833
)
array(8526,567,833) would be the same as array_values($backup) or maybe even just $backup, that depends on the $backup array.
Looks like you want to use $backup instead of $values as $values is the imploded string... and since 8526,567,833 isn't a valid number, it parses 8526 and leaves the rest alone.