WordPress: get data from array in database - php

I have custom fields that are created dynamicaly. I get the data from those fields and store it into a database as an array with update_post_meta. It's stored as a serialised array in the database:
a:4:{i:1;s:4:"1993";i:2;s:4:"1994";i:3;s:4:"1995";i:4;s:4:"1996";}
Now I need to get this array and echo it out on the website, so it looks something like: 4 children (1993,1994,1995,1996).
Here's the code I use now, but it doesn't work.
<?php
$children = get_post_custom_values('rbchildyear');
foreach ($children as $key => $value){
echo "$key => $value('rbchildyear')<br>";
}
?>
And thats what I get in the front office:
0 => a:4:{i:1;s:4:"1993";i:2;s:4:"1994";i:3;s:4:"1995";i:4;s:4:"1996";}('rbchildyear')
So how can I do that?
Thank you!

use unserialize().
$children = unserialize('a:4:{i:1;s:4:"1993";i:2;s:4:"1994";i:3;s:4:"1995";i:4;s:4:"1996";}');
print_r($children);
This will return array

If you use get_post_meta it will return an array of values (numerically indexed). Then you can loop through the array with a foreach.
$childYears = get_post_meta($post_id, "rbchildyear", true);
foreach($childYears AS $theYear)
{
$printThis .= $theYear.",";
}
print count($childYears)." children ( ".$printThis." )";

Related

how to call this variable in my array editcase

I have a session called $_SESSION['data']
And I have a text input called 'lengtezijde'
I already used foreach on the session:
foreach ($_SESSION['data'] as $key => $data);
And if I want to use my input from lengtezijde, I tried it like this:
echo $_SESSION['data'][$_GET['key'];
but then it is an array and I want the input value.
How do I go a layer deeper in the array to use the value?
Please try this inside the foreach loop:
echo $data['lengtezijde'];
or
echo $_SESSION['data'][$key]['lengtezijde'];
We have seen $key will have indexing value 0.
Note: as looping on session data you will get $data value as when print $data:
Array ( [hoogte] => 1 [kleur] => 1 [lengtezijde] => 800 [toevoegen] => toevoegen )
So you can get directly the value of lengtezijde by using as :
$data['lengtezijde']; inside the foreach loop.
Try this ....
echo $_SESSION['data'][$key]['lengtezijde'];
It is not clear how you set the value of the array. If you set the array value in the for-loop like this:
foreach ($_SESSION['data'] as $key => $data);
$data['lengtezijde'] = "some value";
Then to get the value you must do something like this:
$key = $_GET['key']; //or the key to the index you want
echo $_SESSION['data'][$key]['lengtezijde'];

how to Access to nested items in JSON with foreach PHP?

I have the following JSON structure
{"Id":"1","Persons":[{"Name":"Carl","Time":"00:00:03","info":"","Timeext":"","Timeout":"","Timein":""}, {"Name":"Carl","Time":"00:00:03","info":"","Timeext":"","Timeout":"","Timein":""}{"Name":"Luis","Time":"00:00:08","info":"","Timeext":"","Timeout":"","Timein":""}]}
How I can have acces or read the item inside the nest? For example if I just want the value of time for Carl or all information about Carl. Till now I just can get without a problem the single item in the collection 'Id'. The rst nested items not.
I tryed with json_decode like this:
if( $_POST ) {
$arr['Id'] = $_POST['Id'];
$arr['NP'] = $_POST['NP'];
$jsdecode = json_decode($arr);
foreach ($jsdecode as $values){
echo $values->Time;
}
Can PLEASE somebody help me?
if( $_POST ) {
$arr['Id'] = $_POST['Id'];
$arr['NP'] = $_POST['NP'];
$jsdecode = json_decode($arr,true);
foreach ($jsdecode as $values){
echo $values->Time;
}
Adding 'true' to json_decode converts it to array
You are processing it correct , Just add true as the second parameter to json_decode which will be converted to an Array like
$jsdecode = json_decode($arr,true);

wordpress post_meta update array

I am currently working with wordpress. I needed to be able to save post meta, and update it at a later date without overwriting what was previously stored.
I came up with this quick solution:
$ref = get_post_meta($post->ID, 'page_ref', true );
update_post_meta($post->ID,'page_ref',array($ref,$newdata));
So basically i am getting the current data, storing it in an array and then adding the $newdata to the array. This works great and is stored in the database like this:
a:2:{i:0;a:2:{i:0;s:0:"";i:1;s:34:"data1";}i:1;s:22:"data2";}
When i then loop through the array like this:
foreach ($ref as $i){
echo $i;
}
I get the following result:
Arraydata2
I'm not sure if the array is getting stored correctly, and not entirely sure why the returned data is only showing the latest entry to the array?
Any help would be greatly appreciated
this is serialized data you can get this data using unserialized
method(function)
$serialized = 'a:3:{i:0;s:5:"apple";i:1;s:6:"banana";i:2;s:6:"orange";}';
var_dump(unserialize($serialized));
Output:
Array
(
[0] => apple
[1] => banana
[2] => orange
)
<?php echo var_dump(
unserialize('a:2:{i:0;a:2:{i:0;s:0:"";i:1;s:34:"data1";}i:1;s:22:"data2";}')
); ?>
output
bool(false)
<?php $datas = unserialize(
'a:3:{i:0;s:5:"apple";i:1;s:6:"banana";i:2;s:6:"orange";}'
);
foreach($datas as $key => $val)
{
echo $val;
}
?>
Output
applebananaorange

json_encode get values foreach object

Hello I can't figure out how to loop through this json encoded array and for each object, get all its values. I need each value as a variable for itself.
echo json_encode($formulars);
This is what i get when i echo it out
[{"project_name":"polle","type":"support","title":"vi","reason":"prover","solution":"igen","comments":"okay ","date_stamp":"2013-08-20 14:06:37","used_time":132},{"project_name":"dolla","type":"support","title":"lolol","reason":"skl","solution":"dskal","comments":"kflafda ","date_stamp":"2013-08-20 14:11:36","used_time":210},{"project_name":"polle","type":"fejl","title":"lol","reason":"aksdl","solution":"fdjks","comments":"djsks ","date_stamp":"2013-08-20 14:13:27","used_time":1230}]
I have tried this piece of code and I managed to get out the project_name from the first object and that's it:
foreach ($formulars as $current => $project_name) {
$project_name['project_name'];
}
So is there any way i can get all the variables for each object in my array instead of just the project_name?
Like this:
foreach ($formulars as $current){
$projectName = $current['project_name'];
$type = $current['type'];
$reason = $current['reason'];
}
Thanks in advance
Seems like you have an objects inside an array. So you will need to loop through the array and get each object. Just JSON_DECODE your encoded string like below.
Perhaps:
$data = json_decode($formulars,true);
/* Since it's only one object inside the array, you could just select element zero, but I wil loop*/
//You should now be able to do this
foreach ($data as $current){
$projectName = $current['project_name'];
$type = $current['type'];
$reason = $current['reason'];
}
The reason I loop is because there is a object inside an array(Javascript way I think).
Use json_decode to convert the json object to an array; then use foreach to loop through the array. That should work.
<?php
$arr_json = json_decode($formulars);
foreach($arr_json as $key => $value)
//Code to perform required actions
?>
This should give you some ideas.
Use json_decode (with TRUE for getting an associative array)to convert your JSON object to an associative array. After that, you can use a foreach loop to traverse through your multidimensional array and print the required values.
Code:
$json = json_decode($string, true);
foreach ($json as $key => $value) {
foreach($value as $key2 => $value2) {
echo $value2."\n";
}
}
Working Demo!

Printing contents of a nested array

I am trying to print the contents of a nested array, but it simply returns "Array" as a string yet will not iterate with a foreach loop. The arrays are coming from a mongodb find(). The dataset looks like this:
User
Post_Title
Post_Content[content1,content2,content3]
I am trying to get at the content1,2,3.
My current code looks like this:
$results = $collection->find($query);
foreach ($results as $doc)
{
echo $doc['title']; //this works
$content[] = $doc['content'];
print_r($content); //this prints "Array ( [0] => Array )"
foreach ($content as $item)
{
echo $item;
}
}
All this code does is print the Title, followed by Array ( [0] => Array ), followed by Array.
I feel quite stupid to not figure out something that seems so basic. Most posts on stack overflow refer to multidimensional associative arrays - in this case, the top level array is associative, but the content array is indexed.
I have also tried
foreach ($doc['content'] as $item)
But that gives
Warning: Invalid argument supplied for foreach()
I even tried iterating over the returned array again using a nested foreach, like the following:
foreach ($results as $doc)
{
echo $doc['title']; //this works
$content[] = $doc['content'];
print_r($content); //this prints "Array ( [0] => Array )"
foreach ($content as $item)
{
foreach ($item as $next_item)
{
echo $next_item;
}
echo $item;
}
}
The second foreach failed with an invalid argument..
Any thoughts would be greatly appreciated.
edit: perhaps it has something to do with how I am inserting the data to the DB. That code looks like this
$title = $_POST['list_title'];
$content[] = $_POST['list_content1'];
$content[] = $_POST['list_content2'];
$content[] = $_POST['list_content3'];
$object = new Creation();
$object->owner = "$username";
$object->title = "$title";
$object->content = "$content";
$object->insert();
Is this not the proper way to add an array as a property to a class?
The issue is some of array content is an array while other parts are strings. You should test for the sub content $doc being an array using is_array(). If it is, loop through the $doc like you would any other array, and if it isn't, you can echo the content (may need to test the content type before doing this if you're uncertain as to what content it can be)
The problem is in the code that you are saving data. You should replace the following line
$object->content = "$content";
with
$object->content = $content;

Categories