extract specific data from array in mysql database using php - php

This is the data that i need to extract like example profile_contact_numbers
so the output will be +639466276715
how can i do it in php code??
any help will do regards
a:2:
{s:23:"profile_contact_numbers";s:13:"+639466276715";s:16:"profile_position";s:7:"Courier";}

I'm not sure 100% this can go into an array but try the unserialize function
$json_resp = {your values};
$array[] = unserialize($json_resp);
To check if it has gone into an array print_r on $array.
Read this link if the code above doesn't work
http://php.net/manual/en/function.unserialize.php
I have managed to fix it
$serialized = array(unserialize('a:2:{s:23:"profile_contact_numbers";s:13:"+639466276715";s:16:"profile_position";s:7:"Courier";}'));
var_dump($serialized);

use code :
$var = preg_split('["]','{s:23:"profile_contact_numbers";s:13:"+639466276715";s:16:"profile_position";s:7:"Courier";}');
echo $var[1].'='.$var[3]; // profile_contact_numbers=+639466276715
echo $var[5].'='.$var[7]; // profile_position=Courier

Related

create an empty object in JSON with PHP?

i have create json from an array but how we can make an empty json
$jsonrows['paymentmethods']['CC']=array()
json_encode($jsonrows['paymentmethods']['CC']=array())
currently output is like this
"CC":[]
what i need is
"CC":{}
please help me with this
Use a class instead of an array:
var_dump(json_encode(new StdClass()));
Try This
$cc['CC'] = new stdClass ;
echo json_encode($jsonrows['paymentmethods']=$cc);
Output is
{"CC":{}}
For code readability, I use $emptyObject = (object)[];
In the context of your example:
$jsonrows['paymentmethods']['CC']=array();
echo json_encode($jsonrows['paymentmethods']);
yields the undesired: {"CC":[]}
$jsonrows['paymentmethods']['CC']=(object)array();
echo json_encode($jsonrows['paymentmethods']);
gives what you want: {"CC":{}}

Using json response in PHP

I am trying to use one object from the many provided in my json request.
Trying to obtain only the country name from the data that is given.
$location = file_get_contents('http://freegeoip.net/json/'.$_SERVER['REMOTE_ADDR']);
echo $location;
The above code gives me the following string:
{"ip":"x.xx.xx.x","country_code":"FR","country_name":"France","region_code":"A2","region_name":"Bretagne","city":"Brest","zipcode":"","latitude":xxxx,"longitude":xxxx,"metro_code":"","area_code":""}
Any help would be greatly appreciated!
$a = json_decode($location);
echo $a->country_name;
You might also want to have a look on this. http://www.php.net/manual/en/function.json-decode.php
Look for manual json_decode
$tmp = json_decode($location);
echo $tmp->country_code

Echo element from php array

Can someone tell me how I can loop through the below array?
http://pastebin.com/rhaF5Zdi
I've tried with out luck:
$_data = json_decode($_data);
foreach ( $_data as $tweet )
{
echo "{$tweet->text}\n";
}
thanks
ps: im follwoing this php script.
http://mikepultz.com/2013/06/mining-twitter-api-v1-1-streams-from-php-with-oauth/
hers another paste bin on the array. there seems to be multiple arrays happening
http://pastebin.com/dduzhpqY
It looks like you might be creating a PHP stnd object instead of an array
//this will create a php standard object
$objOfData=json_decode($json);
Instead Use the version below: (Notice the the 2nd parameter is TRUE)
$associativeArray=json_decode($json, TRUE);
This will turn the object into an associative array and you can access fields like so:
$id=$associativeArray['id'];
More info here: json_decode
The code in posted link is not JSON, but it is output of print_r() function. PHP has no invert function to print_r(), but on php.net in print_r() documentation's comments you can find some user-made functions which can read it.
For example this one:
http://www.php.net/manual/en/function.print-r.php#93529
I hope it will help. Good Luck :)
Ended up using:
if(array_key_exists('text', $_data)){
echo 'Tweet ID = '.$_data['id_str'];
echo 'Tweet Text = '.$_data['text'];
echo '<br /><br />';
}
cheers

php issue accessing array

I have the following code :
$results = $Q->get_posts($args);
foreach ($results as $r) {
print $r['trackArtist'];
}
This is the output :
["SOUL MINORITY"]
["INLAND KNIGHTS"]
["DUKY","LOQUACE"]
My question is, if trackArtist is an array, why can't I run the implode function like this :
$artistString = implode(" , ", $r['trackArtist']);
Thanks
UPDATE :
Yes, it is a string indeed, but from the other side it leaves as an array so I assumed it arrives as an array here also.
There must be some processing done in the back.
Any idea how I can extract the information, for example from :
["DUKY","LOQUACE"]
to get :
DUKY, LOQUACE
Thanks for your time
It's probably a JSON string. You can do this to get the desired result:
$a = json_decode($r['trackArtist']); // turns your string into an array
$artistString = implode(', ', $a); // now you can use implode
It looks like it's not actually an array; it's the string '["DUKY","LOQUACE"]' An array would be printed as Array. You can confirm this with:
var_dump($r['trackArtist']);
To me content of $r['trackArtist'] is NOT an array. Just regular string or object. Instead of print use print_r() or var_dump() to figure this out and then adjust your code to work correctly with the type of object it really is.

array stored as string back into array?

just trying to add some setting for the admin in a database entry.
i've saved;
array('aviva'=>'aviva','teacher'=>'teacher');
into the field 'fullPara' but can't seem to get it back into an array? Just spits it out as a string and i've tried eval but not sure where to go from here?
echo $userTypes['fullPara']; // spits out array('aviva'=>'aviva','teacher'=>'teacher');
any pointers welcome!
best, Dan
You want to look into the serialize() and unserialize() functions PHP offers.
Here is an example:
$array = array('1', '3', '4');
$s_array = serialize($array);
// insert that into the db.
// later on when fetching.
$array = unserialize($array_from_db);
print_r($array); // viola
EDIT
I do NOT recommend this but here is how you would convert it to an array using eval:
eval("\$array = " . $data_from_Db);
print_r($array);
Should get you what you were after.
If you already have a string of "array('aviva'=>'aviva','teacher'=>'teacher'); " and you wish to turn it into an array, this should work...
$str = "array('aviva'=>'aviva','teacher'=>'teacher');";
eval("\$foo = $str");
var_dump($foo);
It's really not the best way of doing it though.

Categories