I have an associated array saved in my database that Im saving to a variable called response_cost using the Wordpress function get_post_meta.
$response_cost = get_post_meta( $postID, $metaKey, true );
The array looks something like this:
a:2: {
i:0;a:3:
{s:4:"type";s:6:"months";s:4:"cost";s:0:"";s:8:"modifier";s:1:"=";}
i:1;a:3:
{s:4:"type";s:5:"weeks";s:4:"cost";s:0:"";s:8:"modifier";s:1:"+";}
}
I have a form that I've created which outputs a new array and it's saved in a variable called $add_to_response_cost.
$add_to_response_cost = array (
'type' => $type,
'cost' => $cost,
'modifyer' => $modifyer
);
I'm can't figure out how to add $add_to_response_cost as another instance of $response_cost so that the out put ends up like so:
a:3: {
i:0;a:3:
{s:4:"type";s:6:"months";s:4:"cost";s:0:"";s:8:"modifier";s:1:"=";}
i:1;a:3:
{s:4:"type";s:5:"weeks";s:4:"cost";s:0:"";s:8:"modifier";s:1:"+";}
i:2;a:3:
{ my new array I've constructed via $add_to_response_cost }
}
Any help or direction with this is greatly appreciated.
To understand the problem, first we need to explain a little about how saving arrays in custom fields works
Here we have an array
$array = array(
"0" => array(
"type" => "months",
"cost" => null,
"modifier" => "=",
),
"1" => array(
"type" => "weeks",
"cost" => null,
"modifier" => "+",
)
);
If we decide to save it as a custom field in the database, we use the update_post_meta() function
update_post_meta( 1, 'response_cost', $array );
Before saving it to the database, Wordpress will serialize our array and then put it into the database.
As a result, the array will be saved in the database in the following format
a:2:{i:0;a:3:{s:4:"type";s:6:"months";s:4:"cost";N;s:8:"modifier";s:1:"=";}i:1;a:3:{s:4:"type";s:5:"weeks";s:4:"cost";N;s:8:"modifier";s:1:"+";}}
Then, if we want to get an array, we use get_post_meta()
$myarray = get_post_meta(1, "response_cost", true);
Wordpress will take a serialized array from the database and convert it to a typical array.
Then we can add any data we want to the array and save it back to the database.
$add_to_response_cost = array (
'type' => $type,
'cost' => $cost,
'modifyer' => $modifyer
);
$myarray[] = $add_to_response_cost;
update_post_meta( 1, 'response_cost', $myarray );
Usually this problem occurs when using the get_post_meta function without specifying a key, with only one parameter (ID), then we get arrays without processing and they must first be unserialized
This can also happen when you get an array from a database not through a Wordpress function, but by direct SQL query to the database.
Then you need to unserialize the array first, add data to it and pack it back.
You can try unserialize() to convert it to array.
$result = unserialize($response_cost);
Print_r($result);
Related
I am writing a page that pulls images and image data out of a multidimensional array. I need to be able to click a button that calls a function to sort out the images by tags(IE tag_GlassDoor & tag_GlassWall) - basically to show only images that do or do not have that particular element (in this case im using 0 and 1 for yes and no), such as a glass door. I can currently make that array display the data, but I cant figure out how to sort the data by one of the array keys, or even really the syntax to pull a single value out at will.
$arrImages[] =
[
'img_sm'=>'image1.jpg',
'tag_GlassDoor'=>0,
'tag_GlassWall'=>1,
];
$arrImages[] =
[
'img_sm'=>'image2.jpg',
'tag_GlassDoor'=>1,
'tag_GlassWall'=>1,
];
Filtering is the answer, it can be used to filter one dimensional Arrays and multidimensional arrays.
the general implementation would be something like this:
$arr = array(
array(
'image' => "data",
'hasObject' => 1
),
array(
'image' => "data",
'hasObject' => 0
),
);
$finteredArray = array_filter($arr, function ($r) {
return (bool) $r['hasObject'];
});
print_r($finteredArray);
// it outputs:
// Array ( [0] => Array ( [image] => data [hasObject] => 1 ) )
I have this multidimensional array in php:
$products = array(array(
"name" => "Hannah",
"id" => "eg01",
"price" => 120
),
array(
"name" => "Natasha",
"id" => "eg02",
"price" => 125
));
How do I push new data in the array and save it?
I tried this code:
array_push($products, $name, $id, $price);
but it only replaces the new one every time I click the push button.
I wanted it to be saved every time i push.
Either you can use array_push() method for inserting records in an array or we can append data like this
$products[] = ['key'=>'value'];
Your array is made of sub arrays with named keys, you need to construct your inner array first.
array_push($products,array('name'=>$name,'id'=>$id,'price'=>$price));
or for verbosity
$item = array(
'name' => $name,
'id' => $id,
'price' => $price
);
array_push( $products, $item );
you may also see this syntax
$products[] = array('name'=>$name,'id'=>$id,'price'=>$price);
in which you can optionally specify a key if needed between the [] notation
I am trying to bring through content based on a unique GUID. The database is stored in a serialized array and this cannot be changed.
An example record would be:
a:1:{i:0;s:8:"16YL3332";}
And I currently use this in WP to serialize the string and run it in get_posts function using the following args
$guid serialize(strval($_GET['guid'])); // var dump = string(15) "s:8:"16YL3332";"
$args = array(
"post_type" => "custom-post-type",
"post_status" => "publish",
"posts_per_page" => 1,
"meta_query" => array(
"key" => "_fixtures",
"value" => array($guid),
"compare" => "LIKE"
)
);
I am having no success in bring the appropriate data back. WP defaults to bring back the most recent content published in the post type.
Why are you casting the $guid variable to an array? I'm not sure that's needed. If you just pass it as 'value' => $guid, does that work?
Also, see the following QA for some more information that may help you:
https://wordpress.stackexchange.com/questions/115833/how-do-i-search-an-array-stored-in-a-custom-field-using-wp-query
I am working on wordpress with woocommerce and using WCK plugin for custom fields. I am creating products programatically.
I need to save custom fields data as array in database progrmatically. But it is not saving correctly and not showing the custom field values in backend for products. I am using this code.
$data= array(
'alternative-product-names' => $alternative_pname,
'manufacturers-part-number' => $manufature_park_number,
'currently-packaged'=> $currently_packaged,
'other-package-options' => $other_pkg_opt,
'inner-pack-qty' => $inner_pack_qty,
'inner-pack-dimensions' => $inner_pck_dimension,
'packaging-picture'=>''
);
update_post_meta( $post_id, 'productextrainfo1234', $data );
I need to save data in this format:
a:1:{i:0;a:15:{s:25:"alternative-product-names";s:4:"fgfg";s:25:"manufacturers-part-number";s:4:"gffg";s:18:"currently-packaged";s:4:"fgfg";s:21:"other-package-options";s:4:"fgfg";s:14:"inner-pack-qty";s:4:"fggf";s:21:"inner-pack-dimensions";s:17:"packaging-picture";s:3:"561";}}
After testing your code:
First, as you have 7 lines of key/values in your array, so your serialized string can't begin with a:1:{i:0;a:15:{ … but instead with a:1:{i:0;a:7:{ ….
Second, You need to embed your array in an empty array to get the correct format like you want:
a:1:{i:0;a:7:{ … }};.
So your code will have to be like this:
$data= array(
array(
'alternative-product-names' => $alternative_pname,
'manufacturers-part-number' => $manufature_park_number,
'currently-packaged' => $currently_packaged,
'other-package-options' => $other_pkg_opt,
'inner-pack-qty' => $inner_pack_qty,
'inner-pack-dimensions' => $inner_pck_dimension,
'packaging-picture' => ''
)
);
update_post_meta( $product_id, 'productextrainfo1234', $data );
This way you will get this serialized data value in your database:
a:1:{i:0;a:7:{s:25:"alternative-product-names";N;s:25:"manufacturers-part-number";N;s:18:"currently-packaged";N;s:21:"other-package-options";N;s:14:"inner-pack-qty";N;s:21:"inner-pack-dimensions";N;s:17:"packaging-picture";s:0:"";}}
Instead of:
a:7:{s:25:"alternative-product-names";N;s:25:"manufacturers-part-number";N;s:18:"currently-packaged";N;s:21:"other-package-options";N;s:14:"inner-pack-qty";N;s:21:"inner-pack-dimensions";N;s:17:"packaging-picture";s:0:"";}
$header_content = array();
$header_content['pageone'][] = array(
'image' => 'photo-one.png',
'desc' => "One. Some text here.",
);
$header_content['pagetwo'][] = array(
'image' => 'photo-two.png',
'desc' => "Two. Some text here.",
);
I do not want to echo the entire array, just certain parts when called... like $header_content['pageone']['image'], except this doesn't work... How do you go about echoing parts of an array?
I have searched but it's all a little confusing right now for me. Thanks.
Define it like -
$header_content['pagetwo'] = array(
'image' => 'photo-two.png',
'desc' => "Two. Some text here.",
);
The keys are different pageone, pagetwo. No need of that extra index i think. And then access it - echo $header_dontent['pagetwo']['image'];
For printing array values, you can use :
print_r function
Example : print_r($array)
Specific key data can be accessed through : print_r($array[$key])
OR
var_dump function
Example : var_dump($array)
Specific key data can be accessed through : var_dump($array[$key])
use it like $header_content['pageone'][0]['image']
Since
$header_content['pageone'][] = array();
[] appends an element at the end of an array.