Php 在序列化数组中访问和更新数据时出现问题
我希望有人能帮助我,或者给我指出正确的方向。我花了一周的时间试图找出如何访问和更新WP电子商务中的权重字段,但我在访问它和更新新值方面没有成功 我使用的脚本访问一个网站来获取漫画书信息。然后,它解析xml文件中的数据,并为第二个变量分配特定于WP电子商务的值 整个脚本工作得很漂亮,为每本漫画书创建产品页面,所有数据都添加到每个产品中。除了一个!我已经被困了一个星期,试图找出如何访问元和更新它。这是_wpsc_product_元数据数组,用于存储在其中的权重和权重单位以及其他元数据 一本漫画书的重量大约为3盎司,我一直试图将这个值分配给每张卡片,因为它创建了“3”和“盎司”——我没有成功 我可能已经尝试过很多不同的方法,从挖掘电子商务代码到浏览网络,阅读多维数组、序列化数组,而不是点击我的noggin。请记住,我只是在过去一个月的PHP编程,在一个月前,我对代码的了解,除了15年前我涉足C和C++的时候。当时,它主要是复制现有代码,并在MUDS(多用户地下城)中更改输出——回到在线游戏是基于文本的时候,哈哈 我假设我可以通过以下方式访问WP电子商务元:Php 在序列化数组中访问和更新数据时出现问题,php,e-commerce,wordpress,Php,E Commerce,Wordpress,我希望有人能帮助我,或者给我指出正确的方向。我花了一周的时间试图找出如何访问和更新WP电子商务中的权重字段,但我在访问它和更新新值方面没有成功 我使用的脚本访问一个网站来获取漫画书信息。然后,它解析xml文件中的数据,并为第二个变量分配特定于WP电子商务的值 整个脚本工作得很漂亮,为每本漫画书创建产品页面,所有数据都添加到每个产品中。除了一个!我已经被困了一个星期,试图找出如何访问元和更新它。这是_wpsc_product_元数据数组,用于存储在其中的权重和权重单位以及其他元数据 一本漫画书的重
get_post_meta($post_id, '_wpsc_product_metadata', true); That did not work.
我尝试了其他实现这一点的方法:
get_post_meta(get_the_id($post_id), '_wpsc_product_metadata', true); // no success
我尝试使用特定的产品ID,如下所示:
get_the_id(300), or just '300', i've used single quotes double quotes no qoutes, "{}", etc etc etc etc
现在当我使用:
get_post_meta($post_id, ''); //while in the loop
输出SKU和价格的值,但不输出_wpsc_product_元数据中的值
然后当我使用这个:
$product_data['meta'] = array();
$product_data['meta'] = maybe_unserialize(get_post_meta('$post_id', '' ));
这是我打印时得到的
Array ( [_wpsc_price] => Array ( [0] => Array ( [0] => 19.99 ) )
[_wpsc_sku] => Array ( [0] => Array ( [0] => 978-0-7851-5209-5 ) ) )
因此,我尝试了另一件事,我进入了一个特定的产品,我手动将权重设置为5,并更改了代码,如下所示:
$product_data['meta'] = array();
$product_data['meta'] = maybe_unserialize(get_post_meta( '44317', '' ));
这是输出:
Array ( [_wpsc_stock] => Array ( [0] => Array ( [0] => ) )
[_wpsc_product_metadata] => Array ( [0] => Array ( [0] =>
a:19:{s:25:"wpec_taxes_taxable_amount";s:0:"";s:13:"external_link";s:0:
"";s:18:"external_link_text";s:0:"";s:20:"external_link_target";s:0:"";s:6:
"weight";s:3:"0.3";s:11:"weight_unit";s:5:"pound";s:10:"dimensions";a:6:
{s:6:"height";s:1:"0";s:11:"height_unit";s:2:"in";s:5:"width";s:2:"0
";s:10:"width_unit";s:2:"in";s:6:"length";s:1:"0";s:11:"length_unit";
s:2:"in";}s:8:"shipping";a:{s:5:"local";s:1:"0";s:13:"international";s:1:"0";}
s:14:"merchant_notes";s:0:"";s:8:"engraved";s:1:"0";s:23:
"can_have_uploaded_image";s:1:"0";s:15:"enable_comments";s:0:"";
s:24:"unpublish_when_none_left";s:1:"0";s:11:"no_shipping";s:1:"0";s:16:
"quantity_limited";s:1:"0";s:7:"special";s:1:"0";s:17:"display_weight_as";s:5:
"pound";s:16:"table_rate_price";a:2:{s:8:"quantity";a:0:{}s:11:"table_price";a:0:
{}}s:17:"google_prohibited";s:1:"0";} ) ) [_wpsc_special_price] => Array ( [0] =>
Array ( [0] => 0 ) ) [_edit_last] => Array ( [0] => Array ( [0] => 1 ) )
[_edit_lock] => Array ( [0] => Array ( [0] => 1333358836:1 ) ) [_wpsc_sku] =>
Array ( [0] => Array ( [0] => 978-0-7851-6421-0 ) ) [_wpsc_price] => Array
( [0] => Array ( [0] => 24.99 ) ) [_wpsc_is_donation] => Array ( [0] => Array (
[0] => 0 ) ) [_wpsc_currency] => Array ( [0] => Array ( [0] => a:0:{} ) ) )
我希望,不,我请求有人能启发我,告诉我如何访问和更新存储在_wpsc_product_元数据中的数据,特别是重量和重量单位,如果你知道如何访问和更新,那会更好,您可以向我展示一个将新创建的帖子权重更新为值并将默认权重单位从磅更改为盎司的示例
以下是代码供参考:
<?php
function scraping_comic()
{
$html = file_get_html('http://site-to-strip.com/');
$matches = str_replace (' ', ' ', $article);
foreach($html->find('li.browse_result') as $article)
{
// get comic title
$item['title'] = trim($article->find('h4', 0)->find('span',0)->outertext);
// get comic title url
$item['title_url'] = trim($article->find('h4', 0)->find('a.grid-hidden',0)->href);
// get comic image
$item['image_url'] = trim($article->find('img.main_thumb',0)->src);
// get comic excerpt
$item['excerpt'] = trim($article->find('p.browse_result_description_release', 0)->plaintext);
// get comic sales info
$item['on_sale'] = trim($article->find('.browse_comics_release_dates', 0)->plaintext);
// strip numbers and punctuations
$item['title2'] = trim(preg_replace("/[^A-Za-z-\t\n\s]/","",$article->find('h4',0)->find('span',0)->plaintext));
$item['title3'] = trim(preg_replace("/[^A-Za-z]/","",$article->find('h4',0)->find('span',0)->plaintext));
$ret[] = $item;
}
$html->clear();
unset($html);
return $ret;
}
$ret = scraping_comic();
if ( ! empty($ret))
{
foreach($ret as $v)
{
//download the image
$url = $v['image_url'];
$title = $v['title3'];
$now = time();
$num = date("w");
if ($num == 0)
{ $sub = 6; }
else { $sub = ($num-1); }
$WeekMon = mktime(0, 0, 0, date("m", $now) , date("d", $now)-$sub, date("Y", $now));
$todayh = getdate($WeekMon);
$d = $todayh[mday];
$m = $todayh[mon];
$y = $todayh[year];
$date_stamp = $d.$m.$y;
//scrape inside pages
$scrape = 'http://domain.com';
$comic_details = $v['title_url'];
$comic_details_url = $scrape.$comic_details;
$url2 = file_get_html($comic_details_url);
foreach($url2->find('.comics_detail_lead_left_panel_content') as $the_details);
$matches = str_replace (' ', ' ', $the_details);
{
$item2['image2_url'] = trim($the_details->find('img.frame-img',0)->src);
$item2['full_desc'] = trim($the_details->find('p',0)->plaintext);
$item2['data'] = trim($the_details->find('dl',0)->plaintext);
}
$url2->clear();
unset($url2);
//download medium-sized image
$root2 = ('/home/****/public_html/wp-content/blogs.dir/14/files/comics/' .$title.$date_stamp. '-medium.jpg');
$image2 = $item2['image2_url'];
copy($image2, $root2);
unset($root2);
unset($image2);
//match specific data and assign variables
$string = $item2['data'];
$number = preg_match("/(Comic|Hardcover|Paperback)[^A-Za-z]+/", $string, $fields);
switch ($fields[1])
{
case ('Comic'):
$cat = array(51, 52);
break;
case ('Hardcover'):
$cat = array(85, 52);
break;
case ('Paperback'):
$cat = array(95, 52);
break;
default: "";
}
$number = preg_match("/((January)|(February)|(March)|(April)|(May)|(June)|(July)|(August)|(September)|(October)|(November)|(December))[^A-Za-z0-9,]+[A-Za-z0-9,\s]+/", $string, $fields);
$date = $fields[0];
$number = preg_match("/((UPC)|(ISBN))[^0-9-]+([0-9-]+)/", $string, $fields);
$upc = $fields[4];
$number = preg_match("/((Price))[^0-9.]+([0-9.\s]+)/", $string, $fields);
$price = $fields[3];
$full_desc = $item2['full_desc'];
$maintitle = $v['title'];
$excerpt = $v['excerpt'];
$comic_post = array();
$comic_post['post_title'] = wp_strip_all_tags($maintitle);
$comic_post['post_content'] = wp_strip_all_tags($full_desc);
$comic_post['post_status'] = 'publish';
$comic_post['post_author'] = 1;
$comic_post['post_type'] = 'wpsc-product';
$comic_post['post_category'] = $cat;
$comic_post['comment_status'] = 'closed';
$comic_post['ping_status'] = 'closed';
$comic_post['post_excerpt'] = wp_strip_all_tags($excerpt);
// create comic book
$post_id = wp_insert_post( $comic_post );
// category insertion does not work fixed this by calling wp_set_post_terms
wp_set_post_terms($post_id, $cat, 'wpsc_product_category' );
$wpsc_custom = update_post_meta;
$wpsc_custom($post_id, '_wpsc_price', $price);
$wpsc_custom($post_id, '_wpsc_sku', $upc);
//Gain access to WP Ecommerce meta data and assign new values to weight ***IN PROGRESS***
$product_data['meta'] = array();
$product_data['meta'] = maybe_unserialize(get_post_meta( $post_id, '' ));
//***TESTING***//
echo '<br /> the data <br />';
print_r($product_data['meta']);
//Set featured image for product
$filename = ('comics/' .$title .$date_stamp. '-medium.jpg');
update_post_meta( $post_ID, 'image_thumbnail', $imgloc );
set_featured_image($post_id, $filename);
}
}
else { echo 'Could not scrape site!'; }
?>
对于一个已经很久没有编程,最近才开始研究PHP的人来说,你似乎做得很好 关于阵列_wpsc_产品_元数据似乎是JSON。您应该查看PHP手册,特别是下面链接的与JSON相关的部分
如果您使用json_decode,您将能够检索到具有所需属性作为对象属性的对象。在WP电子商务贡献者的帮助下,以下是用于更新权重的缺失拼图
if(!isset($product_data['meta']) || !is_array($product_data['meta'])) {
$product_data['meta'] = array();
}
if(!isset($product_data['meta']['_wpsc_product_metadata'])) {
$product_data['meta']['_wpsc_product_metadata'] =
maybe_unserialize(get_post_meta($post_id, '_wpsc_product_metadata', true));
}
if(!is_array($product_data['meta']['_wpsc_product_metadata'])) {
$product_data['meta']['_wpsc_product_metadata'] = array();
}
$product_data['meta']['_wpsc_product_metadata']['weight_unit'] = 'ounce';
$product_data['meta']['_wpsc_product_metadata']['weight'] = 0.19;
update_post_meta($post_id, '_wpsc_product_metadata', $product_data['meta']['_wpsc_product_metadata']);
我忘了提一下,我以前在BBS上玩MajorMUD的时候都会编写脚本。“一点点”我仍然记得,有些事情看起来很熟悉,我问了很多问题。再加上我每天只睡4个小时,除了编程、阅读、学习、吃饭什么都不做,其余的时间都在睡觉,哈哈。。。正在阅读Json_解码。。。。创建后,我需要在对象处于循环中时访问该对象,如果我查询一篇特定文章“where”时手动输入了权重,我只会显示_wpsc_product_元数据的内容。似乎在您手动输入数据之前,WP E-commerce不会分配_wpsc_product_元数据-然后创建该文章的表。但我不确定。我知道json只有在用json设置的情况下才有效,所以我用if(json_decode($product_data['meta'])==NULL)运行了一个检查程序。产生的错误是警告:json_decode()要求参数1为字符串,数组为给定值。我需要访问的元数据在一个数组中,所以我看不出这有什么帮助,输出结果是它不是有效的json。我错过什么了吗?