【问题标题】:How to get JSON id after item is created创建项目后如何获取 JSON id
【发布时间】:2015-03-11 11:46:56
【问题描述】:

我正在使用一个函数与其他用户共享一组书签,我需要在 wordpress userpro 书签插件中创建新项目后获取 id

function share_collection($id,$name,$cbcolor,$user) {
    $user_id = get_user_by( 'email', $user );
    $user_id_current = get_current_user_id();
    $collectionscurrent = $this->get_collections($user_id_current);
    $collections = $this->get_collections($user_id->ID);
    $collections[] = array('label' => $name);

    // transfer bookmarks to shared collection
    foreach($collectionscurrent[$coll_id] as $k => $arr) {
        if ($k != 'label') {
            $collections["This is where id should go"][$k] = 1;
        }
    }

    update_user_meta($user_id->ID, '_wpb_collections', $collections);
}

我如何检索从 $collections[] = array('label' => $name); 创建的 id并在我提到“这是 id 应该去的地方”的地方使用它

get_collections函数如下

function get_collections($user_id) {
    return (array)get_user_meta($user_id, '_wpb_collections', true);
}

提前致谢!

【问题讨论】:

    标签: php json wordpress


    【解决方案1】:

    称为数组索引。假设$collections 是基于零的数字数组,其中没有任何“孔”(孔将是例如[0 => 'a', 2 => 'b'] 中的索引1),您可以使用:

    $collections[] = array('label' => $name);
    $idx = count($collections) - 1;
    

    如果不是数字或者可能有空洞,你必须先弄清楚要使用的索引:

    $idx = count($collections);
    $collections[$idx] = array('label' => $name);
    

    【讨论】:

    • 谢谢伙计!做到了!
    猜你喜欢
    • 1970-01-01
    • 2014-09-09
    • 2011-06-08
    • 2020-08-12
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多