使用另一个数组更新Multidimentional数组,用于现有密钥PHP

时间:2016-05-24 11:07:29

标签: php multidimensional-array

我有两个数组: (' admin',' admin2',' admin3'可能太多了,名称也可能不同于' admin')

$old = array(
array('admin'=>array('a'=>'aaa','b'=>'bbb')),
array('admin2'=>array('c'=>'ccc','d'=>'ddd'))
);

$new = array(
array('admin2'=>array('e'=>'eee','f'=>'fff')),
array('admin3'=>array('g'=>'ggg','h'=>'hhh'))
);

我希望从以上两个数组中获取此数组: (新阵列中包含来自新阵列的两个PLUS类似键中的所有不同键)

$output = array(
array('admin'=>array('a'=>'aaa','b'=>'bbb')),
array('admin2'=>array('e'=>'eee','f'=>'fff')),
array('admin3'=>array('g'=>'ggg','h'=>'hhh'))
);

3 个答案:

答案 0 :(得分:1)

// Remove one level of array to make arrays as ['admin'=>array, 'admin2'=>array]
$old1 = call_user_func_array('array_merge', $old);
$new1 = call_user_func_array('array_merge', $new);
// Make replacement
$ready =  array_replace($old1, $new1);
// Return level making every item as array 
$result = array();
foreach($ready as $k=>&$v) 
    $result[] = array($k=>$v);
print_r($result);

demolink

答案 1 :(得分:1)

此代码将解决您的问题:

<?php 
$array1 = array(
    array('admin'=>array('a'=>'aaa','b'=>'bbb')),
    array('admin2'=>array('c'=>'ccc','d'=>'ddd'))
);

$array2 = array(
    array('admin2'=>array('e'=>'eee','f'=>'fff')),
    array('admin3'=>array('g'=>'ggg','h'=>'hhh'))
);

$output = $array1; ///merge array1 into output array

foreach($array2 as $key => $val)
{
    $is_present_key = false;
    $first_key = key($val);
    foreach($output as $k => $v)
    {
        if(array_key_exists($first_key,$output[$k])) ////check if key exit in $output array
        {
            $output[$k] = $val; ///push new value if key exists in $output
            $is_present_key = true;
        }
    }
    if($is_present_key == false)///skip for duplicate of new values if key exists in $output 
    {
        $output[] = $val;
    }
}

echo "<pre>"; print_r($output);

?>

这会给你:

Array
(
    [0] => Array
        (
            [admin] => Array
                (
                    [a] => aaa
                    [b] => bbb
                )

        )

    [1] => Array
        (
            [admin2] => Array
                (
                    [e] => eee
                    [f] => fff
                )

        )

    [2] => Array
        (
            [admin3] => Array
                (
                    [g] => ggg
                    [h] => hhh
                )

        )

)

<强> LIVE EXAMPLE

答案 2 :(得分:0)

    @Override
    public void onBindViewHolder(final ViewHolder holder, final int position) {

        holder.txtTitle.setText(questionha.get(position).getQuestionTitle());
        holder.txtDesc.setText(questionha.get(position).getQuestionDesc());
        holder.txtCntDown.setText(questionha.get(position).getQuestionDownCnt());
        holder.txtAuthorName.setText(questionha.get(position).getQuestionAuthorName());
        Glide.with(holder.itemView.getContext()).load(questionha.get(position).getQuestionAuthorPic()).into(holder.imgAuthorPic);
        holder.txtDate.setText(questionha.get(position).getQuestionDate());

        PreferenceHelper preferenceHelper = new PreferenceHelper(holder.itemView.getContext());

        boolean checked = preferenceHelper.getBoolean(preferenceHelper.MY_DATA);
        if(checked){
            holder.imgAddFav.setImageResource(R.drawable.ic_favorite_red_700_24dp);
        }else {
            holder.imgAddFav.setImageResource(R.drawable.ic_favorite_border_red_a700_24dp);
        }


        //=============BTN DOWNLOAD CLICK LISTENER =========================
        holder.btnDownload.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

                DownloadManager downloadManager = (DownloadManager) context.getSystemService(Context.DOWNLOAD_SERVICE);
                DownloadManager.Request request = new DownloadManager.Request(Uri.parse(questionha.get(position).getQuestionDownLink()));

                request.setTitle(questionha.get(position).getQuestionTitle());
                request.setDescription("در حال دانلود");
                request.setDestinationInExternalPublicDir(Environment.DIRECTORY_DOWNLOADS, questionha.get(position).getQuestionDownFileName());
                long enqueueId = downloadManager.enqueue(request);
            }
        });

        //============== IMG ADD TO FAVORITE CLICK LISTENER ======================
        holder.imgAddFav.setOnClickListener(new View.OnClickListener() {

            private PreferenceHelper preferenceHelper = new PreferenceHelper(context);

            @Override
            public void onClick(View v) {

                 boolean isClicked = preferenceHelper.getBoolean(preferenceHelper.MY_DATA);

                if (!isClicked) {
                    Toast.makeText(v.getContext(), "اضافه شد " + isClicked, Toast.LENGTH_SHORT).show();
                    holder.imgAddFav.setImageResource(R.drawable.ic_favorite_red_700_24dp);
                    preferenceHelper.putBoolean(preferenceHelper.MY_DATA, true);
                } else {
                    Toast.makeText(v.getContext(), "حذف شد", Toast.LENGTH_SHORT).show();
                    holder.imgAddFav.setImageResource(R.drawable.ic_favorite_border_red_a700_24dp);
                    preferenceHelper.putBoolean(preferenceHelper.MY_DATA, false);
                }
//=========== Save to Database =================================================         
                /*QuestionDatabaseAdapter databaseAdapter = new QuestionDatabaseAdapter(v.getContext());

                ModelQuestion question = new ModelQuestion();

           /*question.setQuestionTitle(questionha.get(position).getQuestionTitle());
                question.setQuestionDesc(questionha.get(position).getQuestionDesc());
                question.setQuestionDate(questionha.get(position).getQuestionDate());
                question.setQuestionAuthorName(questionha.get(position).getQuestionAuthorName());
                question.setQuestionAuthorPic(questionha.get(position).getQuestionAuthorPic());
                question.setQuestionDownLink(questionha.get(position).getQuestionDownLink());

                databaseAdapter.saveQuestion(question);

                Toast.makeText(v.getContext(), "اضافه شد", Toast.LENGTH_SHORT).show();
                holder.imgAddFav.setImageResource(R.drawable.ic_favorite_red_700_24dp);*/

//===================================================================


            }
        });
    }
相关问题