Array to string conversion in......array [duplicate] - php

This question already has answers here:
Display array values in PHP
(9 answers)
Closed 7 months ago.
I want to get the average of a column in a table,
table: buy
column: c1
when I called the database
with this:
$query="Select AVG(c1) as average FROM buy";
$result_array=mysql_query($query);
$line = mysql_fetch_array($result_array);
and when I called with php like this
<?php echo $line; ?>
it came up error with this message
Array to string conversion in .......... on line 50
Array
what did I do wrong? I think because I treated arrays as a string. but how can i fix this?

Please take a look that $line returns an array. So, you can't echo an array. One thing you can do is
echo "<pre>";
print_r($line);
Check what the array looks like.
Is it a single row that's been returned? In that case you can write
echo $line['average'];
If it's more than one row:
while ($line = mysql_fetch_array($result_array)) {
echo $line['average'];
}
Hope this helps.
Peace! xD

Related

How to dynamically insert JSON data into a PHP array for foreach [duplicate]

This question already has answers here:
How to create a JSON object
(5 answers)
Closed 3 years ago.
I have the following PHP code, which gets executed when I hit the Submit button in a form:
// Save data
if(isset($_POST['save'])){
$newDataArr = array();
foreach ($data_array[0] as $k=>$v){
$newDataArr[] = array($k => $_POST[$k]);
}// ./ foreach
echo json_encode($newDataArr);
}
The echo I get is the following:
[{"ID->id":"esKZSCDfIC"},{"DT->createdAt":"2020-01-26T13:02:42"},{"DT->updatedAt":"2020-01-26T13:02:42"},{"ST->aString":"hey1"},{"NU->number":123},{"GPS->coords":["2.2222","44.4444"]},{"BL->aBool":false},{"AR->theArray":["xx","ww"]},{"FL->theFile":"https:\/\/xscoder.com\/xserver\/uploads\/6dydDtoTt5JjZzFc5L5V_image.jpg"},{"PO->userPointer":"vbN3b0C7bC"}]
Then I'll have to add that array into my JSON file as it follows:
$data = json_encode(array_values($newDataArr), JSON_PRETTY_PRINT | JSON_UNESCAPED_UNICODE | JSON_UNESCAPED_SLASHES);
file_put_contents('Test.json', $data);
But of course the syntax of my $newDataArr is not the right one, each JSON object must not be inside the { }, so this:
{"ID->id":"esKZSCDfIC"},
must become:
"ID->id":"esKZSCDfIC",
Is there a way to dynamically create a valid JSON array and push it to my Test.json file?
I've been through many posts on StackOverflow, but the result I get is always the same. I must use the PHP code above to get keys and values from my HTML inputs.
Instead of creating and pushing a new array on each iteration, set the values this way:
$newDataArr[$k] = $_POST[$k];
Hope this helps.

How to properly convert string form post variables to objects or arrays [duplicate]

This question already has answers here:
Split a comma-delimited string into an array?
(8 answers)
Closed 3 years ago.
I have tried most of the solutions I found, but cannot get it to work.
I have a form parameter that post list of ids separated with commas in a string as per
$list_of_ids = "261,420,788";
Now I need to convert the list of ids into object or arrays so that I can loop through it and insert into database.
My problem is how I can make it look like this:
["261","420","788"]
Here is how I will like to loop it and update database
foreach($list as $id){
echo $id;
// loop and update database
}
$list = explode(",", $list_of_ids);
Should give you an array of the numbers. Take precaution against sql-injections though, if you want to put this into a database!
Just use explode :
$string= "123,234,345";
$array = explode(",",$string);
Output :
Array ( [0] => 123 [1] => 234 [2] => 345 )

How can i Iterate through data returned in Ajax success? [duplicate]

This question already has answers here:
How to loop through PHP object with dynamic keys [duplicate]
(16 answers)
Closed 5 years ago.
How can I iterate to get id value? This is my array:
[{"email_id":"gayatri.dsf#detedu.org","Id":"216"}]
tried
<?php
foreach($faculty as $value)
{
echo $value['Id'];
}
?>
Gives an error
Use of undefined constant Id - assumed Id
This is a json which is basically a string, to be more precise the given json contains a list (currently 1 element):
[{"email_id":"gayatri.dsf#detedu.org","Id":"216"}]
You need to convert it to an array first:
$jsonValues = json_decode($json, true); //here you will have an array of users (1 now)
foreach($jsonValues as $faculty) //for each user do something
{
echo $faculty['Id'];
}
This is JSON format. First you have to decode it. Example:
$a = '[{"email_id":"gayatri.dsf#detedu.org","Id":"216"}]';
$dec = json_decode($a);
echo $dec[0]->Id;
Result: 216
Decoded you have an array, containing exactly one object. You have to access the object properties with -> then.
With JSON [] brackets means an array, while {} brackets mean objects. Learn more: https://en.wikipedia.org/wiki/JSON

function array_push returns empty array [duplicate]

This question already has an answer here:
PHP json_encode returns null value from unicode character
(1 answer)
Closed 7 years ago.
I try to save the rows of a table in an array but I am getting an empty array.
My code is:
$a = array();
$b = array();
if ($result->num_rows > 0){
while ($row = $result->fetch_assoc()) {
$b["id"] = $row["id"];
$b["title"] = $row["title"];
$b["description"] = $row["description"];
$b["lat"] = $row["lat"];
$b["lng"] = $row["lng"];
$b["walkId"] = $row["walkId"];
echo json_encode($b);
array_push($a,$b);
}
echo json_encode($a);
}
Using echo json_encode($b); I see the results one by one but the array a is empty.
EDIT 1
After Random's help, using the echo print_r($a,true); I am getting the below screenshot:
And the screenshot from other tables in which I get the data correctly is:
EDIT 2
problem with json_encode. I get the below
[{"id":"55","title":"\u00ce\u203a\u00ce\u00bf\u00cf\u2026\u00ce\u00bb\u00ce\u00bf\u00cf\u2026\u00ce\u00b4\u00ce\u00ac\u00ce\u00b4\u00ce\u00b9\u00ce\u00ba\u00ce\u00b1","description":"\u00ce\u201c\u00ce\u00b5\u00ce\u00bd\u00ce\u00b9\u00ce\u00ba\u00ce\u00ae \u00ce\u00b1\u00ce\u00bd\u00ce\u00b1\u00cf\u2020\u00ce\u00bf\u00cf\u0081\u00ce\u00ac \u00cf\u0192\u00cf\u201e\u00ce\u00b1 x\u00ce\u00b1\u00cf\u0081\u00ce\u00b1\u00ce\u00ba\u00cf\u201e\u00ce\u00b7\u00cf\u0081\u00ce\u00b9\u00cf\u0192\u00cf\u201e\u00ce\u00b9\u00ce\u00ba\u00ce\u00ac \u00cf\u201e\u00ce\u00b7\u00cf\u201a \u00ce\u00b3\u00ce\u00b1\u00cf\u0192\u00cf\u201e\u00cf\u0081\u00ce\u00bf\u00ce\u00bd\u00ce\u00bf\u00ce\u00bc\u00ce\u00af\u00ce\u00b1\u00cf\u201a \u00cf\u201e\u00ce\u00b7\u00cf","lat":"40.634356","lng":"22.940716","walkId":"92"},{"id":"56","title":"\u00ce\u2018\u00ce\u00b3\u00ce\u00bf\u00cf\u0081\u00ce\u00ac \u00ce\u0153\u00ce\u00bf\u00ce\u00b4\u00ce\u00b9\u00ce\u00ac\u00ce\u00bd\u00ce\u00bf","description":"H \u00ce\u00b9\u00cf\u0192\u00cf\u201e\u00ce\u00bf\u00cf\u0081\u00ce\u00af\u00ce\u00b1 \u00ce\u00bc\u00ce\u00b9\u00ce\u00b1\u00cf\u201a \u00ce\u00b9\u00cf\u0192\u00cf\u201e\u00ce\u00bf\u00cf\u0081\u00ce\u00b9\u00ce\u00ba\u00ce\u00ae\u00cf\u201a \u00ce\u00b1\u00ce\u00b3\u00ce\u00bf\u00cf\u0081\u00ce\u00ac\u00cf\u201a. \u00cf\u201e\u00ce\u00b1 X\u00ce\u00b1\u00cf\u0081\u00ce\u00b1\u00ce\u00ba\u00cf\u201e\u00ce\u00b7\u00cf\u0081\u00ce\u00b9\u00cf\u0192\u00cf\u201e\u00ce\u00b9\u00ce\u00ba\u00ce\u00ac \u00cf\u201e\u00ce\u00b7\u00cf\u201a \u00ce","lat":"40.634609","lng":"22.941081","walkId":"92"}
First thing I note is the special ? that appears 3 time. The problem must come from here.
Try to edit the 3 descriptions that contain the special ? so it doesn't appear again in the print_r.
That must turn array_push into an error.
You could also try to encode it in latin to make it safe. (please follow #deceze link to understand what to do)

How to echo an array [duplicate]

This question already has answers here:
How can I echo or print an array in PHP?
(14 answers)
Closed 2 years ago.
It's not like I've never done it before, but for some reason it won't work this time... I'm just returnin an array from a function
//Call Function to create the result array
$specs = giveData();
and I'm trying to output the data with echo this way:
<b>lenght:</b><?php echo $specs[0]['lenght']; ?>
I already tried var_dump and it shows me the data in the array, also with print_r works.
EDIT: I updated the code the way it works for me.
Print all values of an array
<?php
echo '<pre>';
print_r($specs);
// OR var_dump to get variable type (string / int / etc)
var_dump($specs);
echo '</pre>';
?>
The echo of the pre tags are for formatting reasons in HTML since the pre tag will show linebreaks (\n) as a visible new line inside of HTML.
As for echoing a single value from an array, all you have to do is refernce the key like you were doing.
echo $specs['length'];
You can make sure the key exists by using the function isset.
if(isset($specs['length'])) {
echo $specs['length'];
}else{
echo 'Error, Length not found';
}
The functions used in this answer can be found on the PHP.net website var_dump(), print_r() and isset()
not sure if you want the number of elements of the array :
echo count($specs);
or iterate over your array :
foreach($specs as $key => $value){
echo "$key : $value<br/>";
}

Categories