Hello I would like to store string which comes from json encode to get it store in MySQL, but the thing is that the array returned nothing. Here is my code:
$com = array("1-1","1-2","1-3");
$classcom=array();
for ($i=1; $i<(int)$totaleleve; $i++) {
$eleve=array();
foreach($com as $value)
{
array_push($eleve,'2');
}
$final="'".json_encode($eleve)."'";
array_push($classcom,$final);
echo $final;
echo $classcom;
}
echo $classcom;
For your information, $totaleleve='20', and I do $final="'".json_encode($eleve)."'"; so that I can concentrate strings of the array to be able to insert it into the SQL statement, which looks something like this:
$sql="INSERT INTO table VALUES (".explode(",", $classcom).")"
// so that it looks like something like this:
$sql="INSERT INTO table VALUES ('["2","2","2"]','["2","2","2"]','["2","2","2"]')
the $final gave me '["2","2","2"]' which i wanted, but when i do array_push($classcom,$final);, it just gave me a blank array :
Array
Can someone help me, please? Thank you!
If $totaleleve is greater than 0 the array is not empty. Just use print_r or var_dump to show its values.
print_r($classcom)
Instead of echo $classcom
Just write print_r($classcom); instead of echo $classcom; and you will see that your array isn't empty.
Related
I want to show only 1 data in array.
My code right now:
var_dump($ranked);
In others cases, I have used echo $ranked->tier but in array this won't work.
You can do it like this:
echo $ranked['tier'];
var_dump($ranked['tier']);
or:
echo $ranked['tier'];
I've been searching everywhere for a definitive answer to what seems to be a really simple task - unfortunately all the solutions I can find (and there are a lot) use mysql_fetch_assoc which is deprecated.
All I'm trying to do is to take a two column set of results from a database and echo in JSON format - I've managed everything fine except for one bit - I can't work how to get the values in to a two dimensional array (two column array) using array_push. I just end up with my two column values merged in to one array. Here's a stripped version of what I've done:
header('Content-Type: application/json');
$mostPopularStm = $sparklyGenericPdoObj->prepare("SELECT views, thing FROM X");
$mostPopularStm->execute();
$mostPopularRS = $mostPopularStm->fetchAll();
echo '{data:';
$mostPopularJson = array();
foreach ($mostPopularRS as $mostPopularItem)
{
array_push($mostPopularJson, $mostPopularItem["views"], $mostPopularItem["thing"]);
}
echo json_encode($mostPopularJson);
echo '}';
This is producing output like this:
{data:["Monkeyface","43","Giblets","25","Svelte","22","Biriani","21","Mandibles","20"]}
But what I need is this:
{data:["Monkeyface":"43","Giblets":"25","Svelte":"22","Biriani":"21","Mandibles":"20"]}
I know I can create something manually to do this, using json_encode to format the string on each loop but it seems inefficient.
Any pointers would be hugely appreciated!
Your current array is like
array(0 => 'Monkeyface', 1 => 43, /* ... */);
but you need like
array('Monkeyface' => 43, /* ... */);
Replace
array_push($mostPopularJson, $mostPopularItem["views"], $mostPopularItem["thing"])
By
$mostPopularJson[$mostPopularItem["thing"]] = $mostPopularItem["views"];
And
echo '{data:';
echo json_encode($mostPopularJson)
echo '}';
Better to use:
echo json_encode(array('data' => $mostPopularJson));
As kingkero said, you will never get your expected result because it is invalid:
{data:["Monkeyface":"43" ...
Correct:
{ "data": { "Monkeyface": "43" ...
Compose your array like so:
$mostPopularJson [$mostPopularItem["thing"]] = $mostPopularItem["views"];
foreach($this->galleryPhotoDBInitialArray as $initialArrayElement) {
$photoExists = false;
foreach($this->galleryPhotoDBFinalArray as $finalArrayElement) {
if($initialArrayElement == $finalArrayElement) {
$photoExists = true;
echo $initialArrayElement;
}
}
if(!$photoExists){
echo 'delete............................................';
echo $initialArrayElement;
}
}
I have the above code. I know the arrays have data as I can print_r() it and see the data. the Array data looks like this:
What do I need to do so I can access the $initialArrayElement value - I need to process this further but I get the PHP error 'array to string conversion error'...
thankyou very much :)
echo $initialArrayElement;
use print_r on this and check if this is still an array.. because at what i observe this was still an array..
Please use
print_r($initialArrayElement); insted of echo $initialArrayElement;
that will work.
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 am creating an array in php by assigning values retrieved from database. When I print the array it is displaying array as output and not its contents. However it does retrieve values from mysql.
$resultset=mysql_query("select isbn from tbl_book where publisherid='$publisherid'");
/***Retrieve Books*****/
while($resultISBNArray = mysql_fetch_assoc($resultset))
{
$isbn = $resultISBNArray["isbn"];
$myArr[]=$isbn;
}
echo $myArr
echoing any array always prints "Array". You need to pick individual values in the array (echo $myArr[0]) or use something like print_r().
You can not print an array. You have todo something like var_dump($myArr);