PHP: Get string from within Array? - php

I'm using facebook SDK and I'm trying to get a string from an array which is the facebook php sdk response.
Basically, I get the age_range from facebook and I need to get the value of age_range.
The age range always is an array for some strange reason.
It looks like this:
[age_range] => Array
(
[min] => 21
)
so, in my php I have this:
$age_range = $me['age_range'];
but when i insert this $age_range into mysql database, I get the word Array inserted into mysql database!
But all i need is the 21 or any other number that returns back from age range.
could someone please advise on this issue?
Thanks in advance.

As stated in my comment, simply change:
$age_range = $me['age_range'];
to
$age_range = $me['age_range']['min'];

Related

how get the value from assoc array using php

Array([0] => stdClass Object([max(Transaction_id)] => 10251))
My php sql query returns the answer in this format.
How can i display the only value from this format.
Thanks in advance
In your query add the as then the name of the column that you want to retreive.
SELECT MAX(COLUMNNAME) AS example example FROM TABLENAME
And then in your php file
echo $result[0]['example'];
if for some reasons you are unable to convert the sql query you can access to this variable in the following way
echo $obj->{"max(Transaction_id)"};

Extract specific data from a php string

I have a url stored in a database in the following format
index.php?main_page=product_info&cPath=1_11&products_id=568
I want to be able to extract the cPath data, 1_11 in this case, and the products id '568' to two separate variables. Note that the cPath value could vary from being a single number such as 23 to a series of numbers and underscores such as 17_25_31. If extracting the cPath is too difficult I could use the products_id once it's extracted and query the database again, but this isn't ideal as I want to avoid additional requests as much as possible.
I really don't know the best (correct) way to go about this.
A more refined approach as suggested by Robbie Averill
//first lets the the query string alone
$string=parse_url('index.php?main_page=product_info&cPath=1_11&products_id=568', PHP_URL_QUERY);
parse_str($string,$moo);
print_r($moo);
Output:
Array
(
[main_page] => product_info
[cPath] => 1_11
[products_id] => 568
)
My original suggestion.
parse_str('index.php?main_page=product_info&cPath=1_11&products_id=568',$moo);
print_r($moo);
output:
Array
(
[index_php?main_page] => product_info
[cPath] => 1_11
[products_id] => 568
)

How to store data in db in array form

i want to store my PHP values in db(mysql) in the form of array...
for example
$a=array{10,20,30,40};
i want to store this variable $a in to db in the array form like how it's storing in array using index.
why i want to do this because in future i may have to perform update or delete operation on the array values..
i know that it's possible to do this thing... but i don't know how to implement this..
i searched about this topic but i didn't get proper answer....
Please suggest me how to do this things...
Why don't use json_encode in PHP and store it on your database. It's the best way.
The array will be converted to a string and will be stored.
Retrieve the data and make use of json_decode and then start working as per your needs.
Example:
<?php
$arr = array('a' => 1, 'b' => 2, 'c' => 3, 'd' => 4, 'e' => 5);
echo json_encode($arr);
?>
OUTPUT: {"a":1,"b":2,"c":3,"d":4,"e":5}
You should create a distinct TABLE to store this kind of data.
Consists of 2 columns, corresponding record ID and the actual data.
So, your record will be looks like
rid value
1 10
1 20
1 30
1 40
2 10
2 40
...
this way you will be able to perform update or delete operation on the array values using conventional SQL routines, as well as selecting data based on the array values.
This is how the things done oin the real world, not in PHP sandbox.
All othe answers here are plainly wrong
I would use serialize/unserialize for this. You can use it like this:
Send to MySQL
<?php
$a = array{10,20,30,40};
$a = serialize($a);
// your code here to send it to the mysql
?>
Get from MySQL
<?php
// your code here to collect it from mysql
$a = unserialize($mysql->str);
?>
The field in the MySQL should be TEXT or VARCHAR.
Regards
BlackBonjour
You can always serialize your array then store the result in a VARCHAR or TEXT field and after fetching you can unserialize the field.

Mongo not sorting when using EMongoCriteria with MongoDate

I'm having some difficulties to select the last entry I posted into my Mongo Collection. This is an example of what an object in my collection looks like:
{"category":1,"date:
{"sec":1356521350,"usec":0},"content":"Test Content","_id":
{"$id":"50dadf8639f992c83f000003"}}
Now, I want to sort on the field date and I am trying to do so by using the following functionality (by using the Yii-MongoDB-Suite):
$oCriteria = new EMongoCriteria;
$oCriteria->sort('date', EMongoCriteria::SORT_DESC);
$oOjbect = ObjectModel::model()->find($oCriteria);
Now, instead of returning the object which has the lastest date, it returns me the first object I entered in the collection.
I literally have no clue about what might be going wrong. Any clues?
I believe you need to pass a PHP array to sort(), so what you actually need is:
$oCriteria = new EMongoCriteria;
$oCriteria->sort(array('date', EMongoCriteria::SORT_DESC));
$oOjbect = ObjectModel::model()->find($oCriteria);

How do i properly format a Mysqli query result in PHP? And is there an equivalent to mysql_field_table() for mysqli?

So I am writing a small PHP framework for fun, and I have come to a point where I need to format an arbitrary query result into this format:
Array(<br>
[tablename]<br>
[fieldname] => value<br>
[fieldname] => value<br>
[tablename]<br>
[fieldname] => value);<br>
<br>
If I was doing this in mysql, I would simply do it this way: $tmp[mysql_field_table][mysql_field_name] = $value. But I chose to do this project using mysqli, and I can't seem to find an equivalent for mysql_field_table() so I can determine the table for each individual value. What is the ideal method for gaining this result?
Look at the example here in the manual; the return value of the function documented is an object with a member called 'table'.
This gives you what you need.

Categories