0

I hope this is the best way to ask this. Not sure how to word it. I was thinking there is a native PHP function to determine this, which leads me to think that maybe my wording in searches isn't the best.

I want to search my array for a specific [key] => [value].

If that [key] => [value] is found within my array I want to get another [key] => [value] from its array parent.

Examples from my code below to explain.

Example 1:

If [post_type] = [page] I want to get [activate_layout] = [value] from array [0].

Example 2:

If [post_type] = [post] I want to get [activate_layout] = [value] from array [1].

General Concept Example 3:

If [post_type] = [x] I want to get [activate_layout] = [x] from its parent array [x].

My question is how can I differentiate a [key] and [value] from another [key] and [value] by its parent array [key]?

Below is how my array data is stored.

[post_type_layouts] => Array
        (
            [0] => Array
                (
                    [context] => Array
                        (
                            [option] => Array
                                (
                                    [activate_layout] => 1
                                    [post_type] => page
                                )

                        )

                )

            [1] => Array
                (
                    [context] => Array
                        (
                            [option] => Array
                                (
                                    [activate_layout] => 1
                                    [post_type] => post
                                )

                        )

                )

        )
Fearless Mode
  • 241
  • 3
  • 13

1 Answers1

1

If I really understand your problem I think that the solution is more simple that you're doing.

I follow this array like example:

$arrayTest = [
0 => [
    'context' => [
        'option' => [
            'post_type' => 'page',
        ],
    ],
],

1 => [
    'context' => [
        'option' => [
            'post_type' => 'post',
        ],
    ],
],
];

And, surfing in that and to get the parent value of post_type I only create a foreach steatment where I use switch steatment to check the post_type value. It's something like this:

foreach ($arrayTest as $subLevel1) {
switch ($subLevel1['context']['option']['post_type']) {
    case 'page':
        $subLevel1['context']['option']['active_layout'] = 0;
        break;

    default:
        $subLevel1['context']['option']['active_layout'] = 1;
        break;
}
print_r($subLevel1);
}

My return is like your sample above, and I think that this solve your problem:

    php -f testArray.php 
    Array
      (
        [context] => Array
          (
            [option] => Array
              (
                [post_type] => page
                [active_layout] => 0
              )
          )
     )
     Array
     (
       [context] => Array
         (
           [option] => Array
             (
               [post_type] => post
               [active_layout] => 1
             )
           )
         )

Good code!

  • This looks brilliant!! I think this easily answers my question in a much lighter way too. I will play around with it to confirm and answer/update. – Fearless Mode Feb 22 '17 at 17:36
  • Thanks! This got me started in the appropriate way! I needed to remove the switch case because I need the case value to be a Wordpress function to match the var against, like so... $post_type = $post_type_layout['context']['option']['post_type']; if (is_singular($post_type)) { // This custom code... } elseif (!is_singular($this_post_type)) { // This default code... } – Fearless Mode Feb 22 '17 at 21:04
  • You're welcome! Any times, everything that we need is a little kick to move us to the correct. Good code! – vicentimartins Mar 02 '17 at 14:51