Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/json/13.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php 如何在创建项后获取JSON id_Php_Json_Wordpress - Fatal编程技术网

Php 如何在创建项后获取JSON id

Php 如何在创建项后获取JSON id,php,json,wordpress,Php,Json,Wordpress,我正在使用一个函数与其他用户共享一组书签,我需要在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_collec

我正在使用一个函数与其他用户共享一组书签,我需要在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;在我提到的“这是我应该去的地方”使用它

get_collections函数如下所示

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

提前谢谢

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

$collections[] = array('label' => $name);
$idx = count($collections) - 1;
如果不是数字,或者可能存在漏洞,则必须首先确定要使用的索引:

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

谢谢你,伙计!成功了!!