PHP - Dealing with duplicated array keys (internal pointers) - php

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]

Related

Array values to single array using foreach loop in PHP

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
)

How to use array values as Variables in big multidimensional Array php

I have to access to all values stored in a big multidimensional array, here's an example of print_r the array:
Array
(
[Novedad] => Array
(
[#attributes] => Array
(
[CUNENov] => 4545454545
)
)
[Periodo] => Array
(
[#attributes] => Array
(
[FechaIngreso] => 1998-12-12
[FechaRetiro] => 2021-11-12
[FechaLiquidacionInicio] => 2021-05-01
[FechaLiquidacionFin] => 2021-05-30
[TiempoLaborado] => 10829
[FechaGen] => 2021-05-05
)
)
[Devengados] => Array
(
[Basico] => Array
(
[#attributes] => Array
(
[DiasTrabajados] => 30
[SueldoTrabajado] => 1258955.00
)
)
)
)
The thing I want to do is extract the values from that array, I have tried this way:
<?php
$cunenov = $array['Novedad']['#attributes']['CUNENov'];
but doesn't work..
Any suggests?. Thanks in advance.
I wanted you to see the "constructed" array and the outputs .. Stand alone, this php works .. Paste it into a stand alone php file and determine what you're doing differently to not achieve the same result.
Building out the array, and then print_r so you can validate it's the same structure as your array.
<?php
$test = Array();
$test['Novedad'] = array();
$test['Novedad']['#attributes'] = array();
$test['Novedad']['#attributes']['CUNENov'] = 4545454545;
print_r( $test );
Yields:
Array
(
[Novedad] => Array
(
[#attributes] => Array
(
[CUNENov] => 4545454545
)
)
)
Then we echo:
$cunenov = $test['Novedad']['#attributes']['CUNENov'];
echo "Value is $cunenov";
Yields
Value is 4545454545

select min (lowest) or max (bigest) from array float number [duplicate]

This question already has answers here:
Get the minimum and maximum values in an array column
(7 answers)
Closed 4 months ago.
Halo people.
There is possible to select select min (lowest) or max (bigest) from array float number?
Im try min(array) and max(array) but not working?
I can not found on manual.
Can you help me?
The array comes from sql
Array ( [0] => 1.11954 ) Array ( [0] => 1.11983 ) Array ( [0] => 1.11854 ) Array ( [0] => 1.11978 ) Array ( [0] => 1.1198 ) Array ( [0] => 1.12024 ) Array ( [0] => 1.11994 ) Array ( [0] => 1.12055 ) Array ( [0] => 1.12106 ) Array ( [0] => 1.12186 ) Array ( [0] => 1.12191 ) Array ( [0] => 1.1214 ) Array ( [0] => 1.12432 ) Array ( [0] => 1.12398 )
for ($list = 1; $list <= $rezult; $list++)
{
$_array=array($rekord['xxx'])
}
print_r($_array);
$_min=min($_array);
$_max=max($_array);
Your problem is that your array is an array of arrays, not an array of floating point numbers, so to find the min/max values you effectively need to flatten the array, which you can do with array_column:
$flat = array_column($array, 0);
echo min($flat), " ", max($flat);
Output:
1.11854 1.12432
Demo on 3v4l.org
Alternatively you can recode your loop to push values, rather than arrays into it:
for ($list = 1; $list <= $rezult; $list++) {
$_array[] = $rekord['xxx']
}
echo min($_array) . " " . max($_array);

Configure 2d php array

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
^---????

php - place multiple items in multiple arrays into one single object

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
}
}

Categories