PHP array key exists from string

I have an array:

<?php $array = [ 'fruits' => [ 'apple' => 'value', 'orange' => 'value' ], 'vegetables' => [ 'onion' => 'value', 'carrot' => 'value' ]; 

I also have a line:

 $string = 'fruits[orange]'; 

Is there a way to check if the array key specified in the string exists in the array?

For example:

 <?php if(array_key_exists($string, $array)) { echo 'Orange exists'; } 
+8
string arrays php
source share
5 answers

Try it. Here we use the foreach and isset functions.

Note: This solution will also work for deeper levels of Ex: fruits[orange][x][y]

Try this piece of code here

 <?php ini_set('display_errors', 1); $array = [ 'fruits' => [ 'apple' => 'value', 'orange' => 'value' ], 'vegetables' => [ 'onion' => 'value', 'carrot' => 'value' ] ]; $string = 'fruits[orange]'; $keys=preg_split("/\[|\]/", $string, -1, PREG_SPLIT_NO_EMPTY); echo nestedIsset($array,$keys); function nestedIsset($array,$keys) { foreach($keys as $key) { if(array_key_exists($key,$array))://checking for a key $array=$array[$key]; else: return false;//returning false if any of the key is not set endif; } return true;//returning true as all are set. } 
+4
source share

It would be much easier to check the other way around. As if the key is in a string. Since the keys are unique, you have no duplicates.

 $array = [ 'fruits' => [ 'apple' => 'value', 'orange' => 'value' ], 'vegetables' => [ 'onion' => 'value', 'carrot' => 'value' ] ]; $string = 'fruits[orange]'; $keys = array_keys($array['fruits']); foreach($keys as $fruit) { if(false !== stripos($string, $fruit)) { return true; } } 

Although this solution is not necessarily perfect, the problem is not quite ordinary to begin with.

+1
source share

You can detonate and check array indices.

 $array = array( 'fruits' => [ 'apple' => 'value', 'orange' => 'value' ], 'vegetables' => [ 'onion' => 'value', 'carrot' => 'value' ]); $string = 'fruits[orange]'; $indexes = (preg_split( "/(\[|\])/", $string)); $first_index= $indexes[0]; $seconnd_index= $indexes[1]; if(isset($array[$first_index][$seconnd_index])) { echo "exist"; } else { echo "not exist"; } 

Demo

+1
source share

You can walk recursively:

 $array = [ 'fruits' => [ 'apple' => 'value', 'orange' => 'value' ], 'vegetables' => [ 'onion' => 'value', 'carrot' => 'value' ] ]; $exists = false; $search = "orange"; array_walk_recursive($array, function ($val, $key) use (&$exists,$search) { if ($search === $key) { $exists = true; } }); echo ($exists?"Exists":"Doesn't exist"); 

Print

Exists

Example: http://sandbox.onlinephpfunctions.com/code/a3ffe7df25037476979f4b988c2f36f35742c217

+1
source share

Instead of using regular expressions or strpos, like other answers, you can also simply split $string by [ and resolve the keys one by one until only one key remains. Then use this last key in combination with array_key_exists() to check your item.

This should work for any number of dimensions (for example, fruit[apple][value][1] ).

Example:

 <?php $arr = [ 'fruits' => [ 'orange' => 'value' ] ]; // Resolve keys by splitting on '[' and removing ']' from the results $keys = 'fruits[orange]'; $keys = explode("[", $keys); $keys = array_map(function($s) { return str_replace("]", "", $s); }, $keys); // Resolve item. // Stop before the last key. $item = $arr; for($i = 0; $i < count($keys) - 1; $i++) { $item = $item[$keys[$i]]; } // Check if the last remaining key exists. if(array_key_exists($keys[count($keys)-1], $item)) { // do things } 
+1
source share

All Articles