How to get check if a certain value is in multidimensional array

Hi,

I am using the ACF Repeater field to store different statuses from a product. The product info are imported with the amazing WP ALL IMPORT plugin from a XML url.

The ACF Repeater field has two keys

ID
STATUS

Whenever, I use the this function get_field('status'); I get a multidimensional array for each of the ID and STATUSes. Like this:

Array
	(
		[0] => Array
			(
			[id] => 1
			[active_status] => ValueInfo1
		)

		[1] => Array
			(
				[id] => 2
				[active_status] => ValueInfo2
			)

		[2] => Array
			(
				[id] => -30
				[active_status] => ValueInfo3
			)

		[3] => Array
			(
				[id] => -13
				[active_status] => ValueInfo4
			)

	)

I talked with @Michael Bissett at the forum about how to get a certain value (he referred me to the forum). He suggested this option http://stackoverflow.com/questions/8102221/php-multidimensional-array-searching-find-key-by-specific-value/8102246#8102246, which is great.

However, it will clutter my code quite a bit, if I need to run that loop every time I need to get a certain value from the multidimensional array.

Therefore I want to know if there is an overall way to get access to all the values in the multidimensional array so I simply have to make a simple statement like this (or something alike):

if($array[id] == 3){
echo "Something";
}else{
echo "Something else";
}

Looking forward hearing back from you!

Sincerely,
Mika

  • Vaughan

    Hi Mika,

    Hope you're well?

    You could just iterate through the array, you'd need to get the index though for which array you want.

    so something like this for example

    echo $array[0]['id'];
    echo $array[0]['active_status'];
    echo $array[1]['id'];
    echo $array[1]['active_status'];

    would display:

    1
    ValueInfo1
    2
    ValueInfo2

    So in your if statement you can use the following (as long as you know the index key (in your case 0 - 3, the first key is always 0 not 1.

    if($array[0][id] == 3){
    echo "Something";
    }else{
    echo "Something else";
    }

    but that probably will not work for what you need as it's not dynamic enough.

    The only way I can think of is as in the article to loop through each array key with a foreach() loop.

    I can't think of any other ways of doing it which would work in your case.

    Hope this helps

  • Mika

    Hey Vaughan.

    I just tried what you suggested. Here is the code I used:

    <?php
    	$status = get_field('status_id');
    
    	foreach($status as $key) {
    	        if($key['id'] == -4) {
    	            echo 'do something <br/>';
    	        } else {
    	            echo 'Something else <br/>';
    	        }
    	}
    ?>

    It worked. However, I had to remove the else statement so it doesn't show "Something else" for all the other records where the ID is not -4.

    I think, I'm going to create a function for each of the IDs I need to find, then I can simply call a the specific function at the right location in my template file. Then my code will only be something like this:

    <?php mik_get_id_1($status); ?>

    Instead of the whole loop every time. Or in other words, the heavy work will be done in the functions.php file and then I can keep my template code clean using simple functions :slight_smile:

    Amazing! Thanks a lot Vaughan!!!

    Sincerely,
    Mika Kaltoft

Thank NAME, for their help.

Let NAME know exactly why they deserved these points.

Gift a custom amount of points.