I have an object called $dbNew that contains multiple arrays with multiple items in each array. Is it possible to put each of these items into one new object so I can run it in a mysql statement that's in a loop. Right now I get a array to string conversion error.
PHP
print "<pre>";
print_r($dbNew);
print "</pre>";
Output
Array
(
[0] => FMServer_Sample
)
Array
(
[0] => GolfAudit4
[1] => iCDS
[2] => rawhide
)
Array
(
[0] => Assets
[1] => PassTracker
[2] => Scanner
)
Array
(
[0] => prima
)
Array
(
[0] => CNAC_db
)
PHP
$closing = "SELECT log_time FROM log_table WHERE database_name = '$dbNew'";
Output
Array to string conversion error.
Desired Output
FMServer_Sample
GolfAudit4
iCDS
rawhide
Assets
PassTracker
Scanner
prima
CNAC_db
Assuming that each of the strings given in your output above is one of the values of database_name that you want to fetch, then you can do this...
foreach($dbNew as $row)
{
foreach($row as $dbName)
{
$closing = "SELECT log_time FROM log_table WHERE database_name = '$dbName'";
// execute this query here and do something with the results
}
}
Related
I am working with php and arrays, I have multiple arrays like following
Array
(
[0] => Array
(
[wallet_address] => 0x127e61982701axxxxxxxxxxxxxxxxxxxxxxxxxxx
)
[1] => Array
(
[wallet_address] => 0xf80a41eE97e3xxxxxxxxxxxxxxxxxxxxxxxxxxxx
)
[2] => Array
(
[wallet_address] => 0x24361F1602bxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
)
and so on....
And i want to make them in single array with comma like following way
$set = array("0x127e61982701axxxxxxxxxxxxxxxxxxxxxxxxxxx","0xf80a41eE97e3xxxxxxxxxxxxxxxxxxxxxxxxxxxx","0x24361F1602bxxxxxxxxxxxxxxxxxxxxxxxxxxxxx");
How can i do this ?Here is my current code but not working,showing me same result(0,1,2 keys),Where i am wrong ?
$GetUserFollower; //contaning multiple array value
$set=array();
foreach($GetUserFollower as $arr)
{
$set[]=$arr;
}
echo "<pre>";print_R($set);
The original array is an Assoc array and therefore the wallet_address needs to be addressed specifically in a loop. Or you could use the array_column() builtin function to achieve the same thing.
$GetUserFollower; //contaning multiple array value
$set=array();
foreach($GetUserFollower as $arr)
{
$set[] = $arr['wallet_address'];
}
echo "<pre>";print_r($set);
Or
$new = array_column($GetUserFollower, 'wallet_address');
print_r($new);
RESULT
Array
(
[0] => 0x127e61982701axxxxxxxxxxxxxxxxxxxxxxxxxxx
[1] => 0xf80a41eE97e3xxxxxxxxxxxxxxxxxxxxxxxxxxxx
[2] => 0x24361F1602bxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
)
Your comments are making me think you want an array without a key, which is impossible. If you do this with the example you show in your comments
$set = array("0x127e61982701axxxxxxxxxxxxxxxxxxxxxxxxxxx","0xf80a41eE97e3xxxxxxxxxxxxxxxxxxxxxxxxxxxx","0x24361F1602bxxxxxxxxxxxxxxxxxxxxxxxxxxxxx");
print_r($set);
You will see
Array
(
[0] => 0x127e61982701axxxxxxxxxxxxxxxxxxxxxxxxxxx
[1] => 0xf80a41eE97e3xxxxxxxxxxxxxxxxxxxxxxxxxxxx
[2] => 0x24361F1602bxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
)
I have the following lines of code that fetches some data from my database. I need to reference some specific contents of the data but I am having an issue with duplicated array keys. i do not want to sort through the data using SQL statements due to the complexity of the project.
Upon checking the pointers of the array, the following was generated.
( [0] => 0 ) Array ( [0] => 0 ) Array ( [0] => 0 ) Array ( [0] => 0 ) Array ( [0] => 0 ) Array ( [0] => 0 ) Array ( [0] => 0 ) Array ( [0] => 0 )
A sample of the printed array :
( [0] => 3371450.18 ) Array ( [0] => 54459051.95 ) Array ( [0] => 210382.52 ) Array ( [0] => 6860440.01 ) Array ( [0] => 13131358.12 )
Since all the array keys are duplicated some PHP array functions like(array_sum, array_max) etc do work on the array and its getting really frustrating.
I wonder if this is because of the while loop. Are array pointers within a while loop always duplicated.
`
if(isset($_SESSION['sess']) && !empty(isset($_SESSION['sess']))){
echo "Session id is ".$_SESSION['sess']." exist";
$currentSession = $_SESSION['sess'];
$sql = "SELECT * FROM `loanbook` WHERE LoanBookSessionId='$currentSession'";
$result = mysqli_query($dbs,$sql);
$teamselection = "SELECT * FROM `Teams`";
$teamresult = mysqli_query($dbs,$teamselection);
while($row = mysqli_fetch_array($result)){
?>
<?php
$Sn[] = $row['Id'];
$Team = $row['Team'];
$CounterClass = $row['CounterPartyClassification'];
$GrossL = $row['GrossLoan'];
$CollType = $row['CollateralType'];
print_r(array_keys(array($GrossL)));
print_r((array($GrossL)));
}
?>`
It looks as if you have a 2d array, where each of the inner arrays contains a string that represents a float.
Mapping each one of the inner arrays to a parsed float value should provide the data structure you need.
<?php
$x = [
["1.43434"],
["1.43434"],
];
echo json_encode($x) . "\n";
echo json_encode(array_map(function($t) {
return floatval($t[0]);
}, $x));
output
▶ php test.php
[["1.43434"],["1.43434"]]
[1.43434,1.43434]
This is the multidimensional array i have. I am new to Php ..i want to input the name,folderid and upload at into an sql table but am stuck at the array part. i need help
Array
(
[status] => 200
[msg] => OK
[result] => Array
(
[folders] => Array
(
[0] => Array
(
[id] => 1861013
[name] => .videothumb
)
)
[files] => Array
(
[0] => Array
(
[name] => wildlife.mp4
[cblock] =>
[sha1] => d5d4e1001f98f70324ef4f84ccd6d683f653c513
[folderid] => 1861011
[upload_at] => 1487016733
[status] => active
[size] => 558253404
[link] => link here
[download_count] => 0
[cstatus] => ok
)
)
)
)
here is the mysql insert code that i tried to use to insert data into the table but all i get in the database are zeros
$st = $dbh->prepare("INSERT INTO movies(name, upload_at, link, folderid)
VALUES (:field1, :field2, :field3, :field4)");
// bind variables to insert query params
$st->bindParam(1, $name);
$st->bindParam(2, $uploadat );
$st->bindParam(3, $link);
$st->bindParam(4, $folderid);
$json = file_get_contents($filename);
//convert json object to php associative array
$data = json_decode($json, true);
// loop through the array
foreach ($data as $item)
{ $name = $item['result']['folders'][0]['id'];
$uploadat = $item['result']['files'][0]['upload_at'];
$link = $item['result']['files'][0]['link'];
$folderid = $item['result']['files'][0]['folderid'];
echo $name;
echo $folderid;
// execute insert query
$st->execute(array(':field1'=>$name,
':field2'=>$uploadat,
':field3'=>$link,
':field4'=>$folderid));
}
Because the array structure doesn't nest the files subarray inside of the folders subarray, I am assuming that folders' indices correspond with files' indices.
foreach($data["result"]["folders"] as $index=>$subarray){
$st->execute(array(':field1'=>$subarray["id"],
':field2'=>$data["result"]["files"][$index]["uploadat"],
':field3'=>$data["result"]["files"][$index]["link"],
':field4'=>$data["result"]["files"][$index]["folderid"]));
}
Suspiciously, $data["result"]["files"][$index]["folderid"] doesn't match $subarray["id"] in your sample array.
This snippet will need to be modified if a array_search() for folderid is required.
I'm trying to adjust a php chart library on my code with some data fetched from a database. I am too close but i think i am confused with all these arrays. I will try to explain as easy as possible. The appropriate form of constructing the data that my library needs to read in order to create a php chart is the following:
//With this value my api displays the chart normally
$p->data = array(array(array("2015/10/10",44),array("2015/10/11",56)));
The way i'm trying to construct the aforementioned array is the following
//Fetch results from database and push values into an array
while($row = mysqli_fetch_array($result)){
$values[] = array($row['Date'] => $row['Total']);
}
$p->data = array(array($values));
But unfortunately the chart is not displaying my values. How can i fix my code in order to achive this format:
$p->data = array(array(array("2015/10/10",44),array("2015/10/11",56)));
You are inserting $row['Date'] as the key and $row['Total'] as the value, which, if you run print_r($p->data) results in this:
Array
(
[0] => Array
(
[0] => Array
(
[0] => Array
(
[2015/10/10] => 44
)
[1] => Array
(
[2015/10/11] => 56
)
)
)
)
You actually need
while($row = mysqli_fetch_array($result)){
$values[] = array($row['Date'], $row['Total']);
}
This way, you will get this array:
Array
(
[0] => Array
(
[0] => Array
(
[0] => Array
(
[0] => 2015/10/10
[1] => 44
)
[1] => Array
(
[0] => 2015/10/11
[1] => 56
)
)
)
)
Exactly what you need
If the array should be the same as the first code example, you don't need the =>. So, you can just do the following:
while($row = mysqli_fetch_array($result)){
$values[] = array($row['Date'], $row['Total']);
}
The => creates a key value pair with the first item as the key and the second as the value. If you use a ,, it creates a flat array with those items as values.
You're wrapping one too many arrays:
$p->data = array(array(array("2015/10/10",44),array("2015/10/11",56)));
^-A ^-B ^-C0 ^-C1
and then
$values[] = array($row['Date'] => $row['Total']);
^-C0,C1,C2,etc...
^--B
}
$p->data = array(array($values));
^--A
^---????
So I've defined the connection, the statement id and executed the query, now I want to return the results of the query into the an array in PHP.
I'm trying this, but I can't seem to use any of the information in $rappels afterwards, what is the proper way to return all of the columns from the query results into this $rappels array and then use them?
$rappels = array();
while($row = oci_fetch_assoc($stid))
{
$rappels[] = $row['operation_id']['operation_type']['date']['aircraft_fullname'];
}
return $rappels;
To retrieve the full resultset from a query you use the oci_fetch_all() function:
oci_fetch_all($stid, $rappels);
This will populate $rappels with an array structured by column, like this:
Array
(
[operation_id] => Array
(
[0] => 1
[1] => 2
...
)
[operation_type] => Array
(
[0] => a
[1] => b
...
)
...
)
To retrieve the resultset structured by row you would use this code (check the function documentation for the meaning of the extra parameters):
oci_fetch_all($stid, $rappels, 0, -1, OCI_FETCHSTATEMENT_BY_ROW + OCI_FETCH_ASSOC);
And $rappels will look like this:
Array
(
[0] => Array
(
[operation_id] => 1
[operation_type] => a
...
)
[1] => Array
(
[operation_id] => 2
[operation_type] => b
...
)
...
)