I need to sort out different components of a metadata string in the table postmeta in my WordPress database.
The string looks like this in the database:
a:1:{i:0;a:4:{s:8:"employer";s:15:"ExampleEmployer";s:9:"job_title";s:9:"Job Title";s:4:"date";s:9:"2014-2016";s:5:"notes";s:20:"Experience(job)notes";}}
I got quite worried when I saw this the first time, and tried to search for a more humane row, but couldn't find one, so this is the only one with the information I need.
The information is from a plugin call WP Job Manager, and I haven't found any documentation on how to sort their array out, so I'm asking you kind people.
Thank you!
EDIT: I'm very tired. The string automatically unserializes when fetched from the database, I just saw that now. Thank you all for your help.
It is a serialized String. You have to unserialize the string and fetch the output.
<?php
$serialized_data = serialize(array('Math', 'Language', 'Science'));
echo $serialized_data . '<br>';
// Unserialize the data
$var1 = unserialize($serialized_data);
// Show the unserialized data;
var_dump ($var1);
?>
Output:
Serialized:
a:3:{i:0;s:4:"Math";i:1;s:8:"Language";i:2;s:7:"Science";}
Unserialized:
array(3) { [0]=> string(4) "Math" [1]=> string(8) "Language" [2]=> string(7) "Science" }
Related
I got my data from Laravel database query command:
$group = DB::table('groups')->where("id", $group_id)->first();
When I var dump my data, I get:
object(stdClass)#200 (7) {
["id"]=>
int(1)
["levels_id"]=>
int(1)
["title"]=>
string(8) "Novice 1"
["description"]=>
string(11) "Lorem Ipsum"
["max_question_display"]=>
int(5)
["created_at"]=>
NULL
["updated_at"]=>
NULL
}
I want to access the max_question_display. But when I do:
var_dump($group["max_question_display"]);
PHP returns error Cannot use object of type stdClass as array.
When I do:
var_dump($group->max_question_display);
I get:
int(5)
But I don't want the int. I only want the 5. In integer form.
If I foreach loop the $group:
foreach ($group as $t) {
echo "<pre>";
var_dump($t);
echo "</pre>";
}
I get each of the data as a single data each loop.
int(1)
int(1)
string(8) "Novice 1"
string(11) "Lorem Ipsum"
int(5)
NULL
NULL
This is obviously also not the way the result accessed that I'm looking for.
I also tried to get the first element of array, thinking that this might be an array with 1 element, but that also raise the same error.
I get it that the general answer in this site about this error is that "stdClass is not array". I have browsed several question with similar title like mine, but nothing address object that came from Laravel DB. When I read the manual on Laravel DB, I was assured that I can access the data returned like a simple dictionary / hashmap.
EDIT: Sorry, I understand my very, very newbie mistakes. No need to answer this. Thanks.
Notice the first line of your first var_dump:
object(stdClass)#200
Because you're dealing with an object, you access its properties with ->. When you do:
var_dump($group->max_question_display);
The reason you see (int) in the output is that the var_dump function shows the value type, next to the value. To access the value, do
$group->max_question_display;
If you want to see it on screen without the type, use echo
echo $group->max_question_display; // 5
stdClass is an object. You cannot use an object with array syntax to access its properties, if the class does not implement ArrayAccess interface.
As pointed out by #IbrahimLawal , var_dump outputs both the type and value. Just echoing $group->max_question_display will provide just the value
echo $group->max_question_display; // 5
In Summary: You must use arrow syntax when interacting with stdClass.
I'm using this to define the valid keys that can be used to perform a search on my front end:
$validKeys = array('gender','marital_status', 'age');
The rest of my code works great in that it only accepts keys sent to it in an AJAX call that are in this array.
However, I want to be able to make this list ('gender','marital_status', 'age') dynamic. That is, I'd like to include all of the columns from my table in here so that every column is essentially a valid key. I have about 10-15 depending on the table and I'd rather not hard-code it into each PHP file.
How would I go about getting all the column names in my table and arranging them into this variable? So, I need to get all the names, put them in single quotes and comma separate. Unless there's a way to get them in a proper array and skip the array part of defining the $validkeys variable.
If I var_dump $validKeys, I get this:
array(5) {
[0]=>
string(6) "gender"
[1]=>
string(14) "marital_status"
[2]=>
string(3) "age"
[3]=>
string(7) "anglers"
[4]=>
string(8) "baseball"
}
Any direction would be appreciated.
EDIT: Should have been more explicit that I am using PDO.
you can try with mysqli's fetch_fields-function.
$finfo = $result->fetch_fields();
Documentation: http://php.net/manual/de/mysqli-result.fetch-fields.php
Run a query using the DESCRIBE syntax. That will return all columns to you which you can then place in the array.
DESCRIBE `table_name`;
Another option is SHOW COLUMNS. Either will work for your requirements.
I should have been a bit more explicit that I am using PDO, so Jay's answer above pointed me in the right direction and I found this answer that gave me the details: https://stackoverflow.com/a/7091984/989722
The full code snippet based on my question looks like this:
$q = $dbh->prepare("DESCRIBE full_db2");
$q->execute();
$table_fields = $q->fetchAll(PDO::FETCH_COLUMN);
$validKeys = $table_fields;
I have an object and like to retrieve the value of one or more elements from the object. Hire is one of the objects if put in a var_dump().
object(SimpleXMLElement)#13 (2) {
["#attributes"]=>
array(1) {
["name"]=>
string(5) "chain"
}
["value"]=>
string(11) "Abba Hotels"
}
I get the value but i can not get to the name.
To get the value i use for example:
echo $row->property->value
My first thought was to use:
echo $row->property->#attributes->name
, but it return as a ERROR. I try to use #attributes in a variable but that gives a NULL.
At second thought i tried to use get_object_vars() and in_array() but no luck again.
Do you guys have a idea about how i can get to the value of the "name" object?
See the docs for SimpleXMLElement:
$object->attributes()
Will give you what you need. I.e.
echo $object->attributes()->name;
It looks like you are using a property value from somewhere. If $row is the object, then you could use this I think.
$row->#attritubes['name']
Im not fully sure but thought id give it a go helping anyawy. Let me know if it works.
I have an array with a field of type string in php.
I'm using it to keep track of history. everytime a user performs a specified action the user id is added to the string. I'm using the RedBean ORM, so to declare the field varchar using mysql I have initialized it as:
$history_field="0 ,";
but when I perform:
$history_array= explode(',', $history_field);
I get:
array(2) { [0]=> string(2) "0 " [1]=> string(0) "" }
There should be one element in the array - namely '0' , I would have thought. Is this a bug?
how can I fix this to get my expected result. Thank you.
As per the documentation (and the comments) this is expected behavior, you can use this to filter out the empty values:
array_filter(explode(',', $history_field), 'strlen');
On stream.get, I try to
echo $feeds["posts"][$i]["attachment"]["href"];
It return the URL, but, in the same array scope where "type" is located (which returns string: video, etc), trying $feeds["posts"][$i]["attachment"]["type"] returns nothing at all!
Here's an array through PHP's var_dump: http://pastie.org/930475
So, from testing I suppose this is protected by Facebook? Does that makes sense at all?
Here it's full: http://pastie.org/930490, but not all attachment/media/types has values.
It's also strange, because I can't access through [attachment][media][href] or [attachment][media][type], and if I try [attachment][media][0][type] or href, it gives me a string offset error.
["attachment"]=> array(8) {
["media"]=> array(1) {
[0]=> array(5) {
["href"]=> string(55) "http://www.facebook.com/video/video.php?v=1392999461587"
["alt"]=> string(13) "IN THE STUDIO"
["type"]=> string(5) "video"
My question is, is this protected by Facebook? Or we can actually access this array position?
Well, once the data is returned to you, it can no longer be protected by Facebook. You have full access to everything in that result as a regular data structure.
From the looks of it, there are multiple href properties throughout, so you'll want to be careful which one you're going for. $feeds["posts"][$i]["attachment"]["href"] is a valid element for some items, but $feeds["posts"][$i]["attachment"]["media"][0]["href"] is also a valid element.
There doesn't appear to be a $feeds["posts"][$i]["attachment"]["type"] element though, so that's why you're getting nothing for that particular item. There is a type inside ["attachment"]["media"][0] however, which is probably what you want.
If you are getting a string offset error when using array syntax, you've probably mixed up an element somewhere. Strings can be accessed via array syntax. For example:
$str = "string";
echo $str[1]; //echos 't'
You would get an offset warning if you tried to access an index that was larger than the string. In any case, from the looks of that output, $feeds["posts"][$i]["attachment"]["media"][0]["type"] should work.