1

here is an array and I want to extract "category_input" as sub array. This sub array contains "cat_id" and "post-titles". Then I will want to insert the posts into WordPress DB under each category. is. cat_20 will have posts named as post-in-cat-20 and so on.

Array
(
    [post_type] => post
    [post_status] => publish
    [content_texarea] => 
    [category_input] => Array
        (
            [0] => cat_20
            [1] => post-in-cat-20
            [2] => post-in-cat-20
            [3] => post-in-cat-20
            [4] => cat_19
            [5] => post-in-cat-19
            [6] => post-in-cat-19
            [7] => post-in-cat-19
            [8] => post-in-cat-19
            [9] => cat_2
            [10] => post-in-cat-2
            [11] => post-in-cat-2
            [12] => post-in-cat-2
        )

)

Expected Results

extracted array will be as

    [category_input] => Array
    (
        [0] => cat_20
        [1] => post-in-cat-20
        [2] => post-in-cat-20
        [3] => post-in-cat-20
        [4] => cat_19
        [5] => post-in-cat-19
        [6] => post-in-cat-19
        [7] => post-in-cat-19
        [8] => post-in-cat-19
        [9] => cat_2
        [10] => post-in-cat-2
        [11] => post-in-cat-2
        [12] => post-in-cat-2
    )

Then, I will need to make caegory and posts array to use in wp query

    [category_input] => Array
    (
        [cat_20] => Array
            [1] => post-in-cat-20
            [2] => post-in-cat-20
            [3] => post-in-cat-20
        [cat_19] => Array
            [5] => post-in-cat-19
            [6] => post-in-cat-19
            [7] => post-in-cat-19
            [8] => post-in-cat-19
        [cat_2] => Array
            [10] => post-in-cat-2
            [11] => post-in-cat-2
            [12] => post-in-cat-2
    )
4
  • Please add the expected result Commented Jul 2, 2017 at 6:14
  • Which one is your desire output?? second one?? Commented Jul 2, 2017 at 6:15
  • yes second one result Commented Jul 2, 2017 at 6:15
  • Checkout the answer Commented Jul 2, 2017 at 6:17

1 Answer 1

2

As you want to update the portion of your array- take a look at the given parts.

The array from the main array by $main_arr[category_input];

foreach($category_input as $val){
    $part = explode('_', $val);
    if(isset($part[0]) && $part[0] == 'cat'){
        $out_arr[$val] = array();
        $key = $val;
    }else{
        $out_arr[$key][] = $val;
    }
}

Look at the complete example: https://3v4l.org/JI9U7

Now you can put the $out_arr again to the $main_arr.

Sign up to request clarification or add additional context in comments.

4 Comments

Let me test it Brother!
Frayne! I think I made you confused. First I will want the to get a sub array of index "category_input".
By using the $main_arr[category_input] you can get the subarray
Thankyou very much @Frayne Konok. It is working perfect. Thanks. I have tested it and getting the desired results.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.