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);
Related
I have a script that loops through and retrieves some specified values and adds them to a php array. I then have it return the value to this script:
//Returns the php array to loop through
$test_list= $db->DatabaseRequest($testing);
//Loops through the $test_list array and retrieves a row for each value
foreach ($test_list as $id => $test) {
$getList = $db->getTest($test['id']);
$id_export[] = $getList ;
}
print(json_encode($id_export));
This returns a JSON value of:
[[{"id":1,"amount":2,"type":"0"}], [{"id":2,"amount":25,"type":"0"}]]
This is causing problems when I try to parse the data onto my android App. The result needs to be something like this:
[{"id":1,"amount":2,"type":"0"}, {"id":2,"amount":25,"type":"0"}]
I realize that the loop is adding the array into another array. My question is how can I loop through a php array and put or keep all of those values into an array and output them in the JSON format above?
of course I think $getList contains an array you database's columns,
use
$id_export[] = $getList[0]
Maybe can do some checks to verify if your $getList array is effectively 1 size
$db->getTest() seems to be returning an array of a single object, maybe more, which you are then adding to a new array. Try one of the following:
If there will only ever be one row, just get the 0 index (the simplest):
$id_export[] = $db->getTest($test['id'])[0];
Or get the current array item:
$getList = $db->getTest($test['id']);
$id_export[] = current($getList); //optionally reset()
If there may be more than one row, merge them (probably a better and safer idea regardless):
$getList = $db->getTest($test['id']);
$id_export = array_merge((array)$id_export, $getList);
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.
My database query is below.
$beneficiary_id = DB::select('select Telephone from item ');
This returns a json array looks like this
[{"Telephone":"0111222333"},{"Telephone":"0112211223"},{"Telephone":"012345557"},{"Telephone":"0225545455"}]
For another database insertion operation I need only the telephone numbers.
I have used json_decode() function, it is working only if we enter the array manually.
How to get only those values to another array?
Thanks in advance.
Use the pluck function
If you would like to retrieve an array containing the values of a single column, you may use the pluck method.
$titles = DB::table('roles')->pluck('title');
foreach ($titles as $title) {
echo $title;
}
use $beneficiary_id->column_name to get the value from your object.
I have an array of elements in PHP called...
$completeArray
...and I'm trying to store a randomized version of this array in my session called...
$_SESSION['videoArray']
...so I'm trying something like this...
$_SESSION['videoArray'] = shuffle($completeArray);
...but when I try to echo the first element of this randomized array like this...
$videoid = $_SESSION['videoArray'];
echo $videoid[0];
...all it's returning is the 'key' of the element. How do I randomize the array and be able to echo the actual elements of the new array?
shuffle take a reference of an array and Returns TRUE on success or FALSE on failure.
You should do:
shuffle($completeArray);
$_SESSION['videoArray'] = $completeArray;
You can try somethin like this:
$_SESSION['videoArray'] = $completeArray;
shuffle($_SESSION['videoArray']);
I've created the following code but for some reason it is echoing Array instead of the result:
<?php
include("../config.php");
include("functions.php");
$count = "SELECT `monthly_slots` FROM users WHERE `username` = 'Bill'";
$count = mysql_query($count);
$data = mysql_fetch_assoc($count);
echo "$data";
?>
Any ideas?
I have no idea why it is outputting Array because there should only be one result from that query.
mysql_fetch_assoc() returns an array, you need to use print_r($data) instead, to dump out the array's contents.
try this
print_r($data);
This will output the contents of the array.
The return type of mysql_fetch_assoc is array. So you should use print_r to see the result.
Returns an associative array that corresponds to the fetched row and moves the internal data
pointer ahead. mysql_fetch_assoc() is equivalent to calling mysql_fetch_array() with MYSQL_ASSOC
for the optional second parameter. It only returns an associative array.
As the array manual page explains, when you output an array variable in string context (as echo does here) it will become just "Array".
To see the array contents use print_r or var_dump instead:
print_r($data);
Or better yet just access the content you wanted:
print($data["monthly_slots"]);
use print_r($data) to print the $data array, or $data['column_name'] for a perticular row. because mysql_fetch_assoc() aloways returns an array.
Solutions
You could simply print out the whole contents of the array with
print_r($data)
however I wouldn't usually recommend doing it like that considering you're only looking for a specific item soo..
You could also try referencing the output as the first item (assuming there is only one result) with
echo "$data[0]";.
you can also simply reference the specific item in the array you are looking for with
echo $data['monthly_slots'].
References:
php arrays
mysql_fetch_assoc
print_r
It is expected. You should try print_r($data)