Php object array contains

How to check if an array object contains an associative array in php?

Try this Solution 3: Check if it is an array with: http://php.net/manual/de/function.is-array.php Question: The code below is not working Indexed arrays work fine, Look Out World Solution: there is an syntax error , you havent closed and double quotes More over age can be given as an integar Question: I’m trying very simply to use to check a key is in an array and then echo it’s value. Solution 2: Try with Solution 3: in_array checks for whether the value exists in the array.

How to check if an array object contains an associative array in php?

Here is an array object that contains an array [body]. How do I get to know that this object has array inside and give me it’s keys?

Array ( [6] => stdClass Object ( [vid] => 6 [uid] => 1 [title] => om [log] => [status] => 1 [comment] => 2 [promote] => 0 [sticky] => 0 [nid] => 6 [type] => article [language] => und [created] => 1436514497 [changed] => 1438003101 [tnid] => 0 [translate] => 0 [revision_timestamp] => 1438003101 [revision_uid] => 1 [body] => Array ( [und] => Array ( [0] => Array ( [value] 

You need to check whether the object has body property, which is array and which should not be empty .

Читайте также:  Html font style hover

And fetch the keys it all three conditions fulfill.

Use is_array(), array_keys() and isset()

Check if any of the object properties is an array and return its keys.

foreach (get_object_vars($obj) as $var) < if (gettype($var) == 'array') < $keys = array_keys($var); >> 

Considering $main_array as your given result. Try this

if( is_array($main_array->body) ) < // do your process >

Check if it is an array with:

Php — Checking if array is multidimensional or not?, What is the most efficient way to check if an array is a flat array of primitive values or if it is a multidimensional array? It’s worth pointing out that PHP does not have true multi-dimensional arrays — just simple associative array’s of values. as written, multi_3 will only work on zero-based non-associative arrays with no gaps …

Associative array is not working

The code below is not working Indexed arrays work fine, Look Out World

"19", "john"=>"18, "alex"=>"17"); asort($age); //looping an associative array foreach($age as $x=>$x_value) < echo "Name = " .$x. "age = " .$x_value. "
"; > ?>

there is an syntax error , you havent closed and double quotes

$age=array("peter"=>"19", "john"=>"18", "alex"=>"17"); asort($age); 

More over age can be given as an integar

$age=array("peter"=>19, "john"=>18, "alex"=>17); asort($age); 

PHP Search Multidimensional Array, and I want to search the array by the [x][0] index to check each string of words for the search term. If found, it should give back the index/key in the main array like «world» returns 1 How to check if PHP array is associative or sequential? 2903. Deleting an element from an array in PHP. 1333. How to Sort a …

In_array not working with associative array php

I’m trying very simply to use in_array() to check a key is in an array and then echo it’s value.

$array = array("abc" => "123", "def" => "456", "ghi" => "789"); if(in_array("abc", $array)) echo $allowed["abc"]; 

It should echo 123 but instead I get:

in_array() expects parameter 2 to be array, null given 

You can use Associative arrays with in_array() ?

I’ve also tried array_key_exists() but it gives the same error?

Stupid error is stupid

$array should be $this->array . long day, delete me maybe?

You should use array_key_exists instead.

in_array checks for whether the value exists in the array. To check whether a key exists, you should use isset.

$array = array("abc" => "123", "def" => "456", "ghi" => "789"); if (isset($array["abc"])) echo $array["abc"]; 

PHP — Check if two arrays are equal, Existing answers are unable to recursively sort an arbitrary array (array of arbitrary depth and order, containing a mixture of sequential and associative arrays) and hence cannot handle comparisons of arbitrary arrays. Sequential arrays are associative arrays with a sequential key (0,1,2,3) whereas …

In_array() doesn’t work as expected with associative array

I don’t know what’s causing this issue, but I’ll post the code below then go through what I’ve done so far and the results I’ve gotten.

$client_emails = array( 'email@ex-one.com' => null, // first array entry 'email@ex-two.com' => 'page_two', 'email@ex-three.com' => 'page_three', 'email@ex-four.com' => null, ); $form_email = 'email@ex-two.com'; if (!empty($form_email)) < if (isset($client_emails[$form_email])) < $client_page = $client_emails[$form_email]; >else < $client_page = null; >> if (in_array($form_email, $client_emails)) < if (!is_null($client_page)) < echo 'All seems to be good! - '; echo $client_page; >else < echo 'You can not be here.'; >> else

The above code is what I’ve been playing with, it does not work. It will work, however, if we change the value for the ‘first array entry’ (comment) from NULL to TRUE , like so:

$client_emails = array( 'email@ex-one.com' => true, // first array entry 'email@ex-two.com' => 'page_two', 'email@ex-three.com' => 'page_three', 'email@ex-four.com' => null, ); 

The whole thing technically works now, but TRUE is equal to 1 and now the rest of my script does not work properly because it will read that as a value of 1 and echo it. I need it to be NULL .

The ‘first array entry’ cannot be NULL or FALSE , the only value that works is TRUE . It can be empty IF the value for $form_email is equal to a key that does not have a value, if the key has a value and there is no value of TRUE for the first array key then the whole thing fails any ways.

Code to reproduce the issue

I don’t understand what’s happening. I have two questions:

  1. Any suggestions on how to get around this?
  2. If you could help me understand why this is happening — maybe I’m doing something wrong?

I’ve also tried the following:

$client_emails = array( 'email@ex-one.com' => 'null', // first array entry 'email@ex-two.com' => 'page_two', 'email@ex-three.com' => 'page_three', 'email@ex-four.com' => 'null', ); $form_email = 'email@ex-two.com'; if (!empty($form_email)) < if (isset($client_emails[$form_email])) < $client_page = $client_emails[$form_email]; >else < $client_page = null; >> if (in_array($form_email, $client_emails)) < if (!empty($client_page) && $client_page != 'null') < echo 'All seems to be good! - '; echo $client_page; >else < echo 'You can not be here.'; >> else

Your problem is your if statement:

if (in_array($form_email, $client_emails)) 

Here you search the email in the values ( [NULL, «page_two», «page_three», NULL] ), but you need to look into the keys ( [«email@ex-one.com», . «email@ex-four.com»] ), so just use array_keys() , e.g.

if (in_array($form_email, array_keys($client_emails))) //^^^^^^^^^^^ See here, so you serach the email in the keys 

Why don’t you use array_key_exists

if(array_key_exists($form_email, $client_emails))

You are comparing $form_email with the values of $client_emails .

if (in_array($form_email, $client_emails))  

$form_email should be compared with the keys of $client_emails , not values. - Try with -

if (in_array($form_email, array_keys($client_emails)))  

Or check for the existence of they -

if(array_key_exists($form_email, $client_emails))  

Php - in_array() doesn't work as expected with, How to check if PHP array is associative or sequential? 687. How do I remove objects from a JavaScript associative array? 802. Get first key in a (possibly) associative array? 1304. How to extend an existing JavaScript array with another array, without creating a new array. 4938.

Источник

in_array

Searches for needle in haystack using loose comparison unless strict is set.

Parameters

Note:

If needle is a string, the comparison is done in a case-sensitive manner.

If the third parameter strict is set to true then the in_array() function will also check the types of the needle in the haystack .

Note:

Prior to PHP 8.0.0, a string needle will match an array value of 0 in non-strict mode, and vice versa. That may lead to undesireable results. Similar edge cases exist for other types, as well. If not absolutely certain of the types of values involved, always use the strict flag to avoid unexpected behavior.

Return Values

Returns true if needle is found in the array, false otherwise.

Examples

Example #1 in_array() example

$os = array( "Mac" , "NT" , "Irix" , "Linux" );
if ( in_array ( "Irix" , $os )) echo "Got Irix" ;
>
if ( in_array ( "mac" , $os )) echo "Got mac" ;
>
?>

The second condition fails because in_array() is case-sensitive, so the program above will display:

Example #2 in_array() with strict example

if ( in_array ( '12.4' , $a , true )) echo "'12.4' found with strict check\n" ;
>

if ( in_array ( 1.13 , $a , true )) echo "1.13 found with strict check\n" ;
>
?>

The above example will output:

1.13 found with strict check

Example #3 in_array() with an array as needle

if ( in_array (array( 'p' , 'h' ), $a )) echo "'ph' was found\n" ;
>

if ( in_array (array( 'f' , 'i' ), $a )) echo "'fi' was found\n" ;
>

if ( in_array ( 'o' , $a )) echo "'o' was found\n" ;
>
?>

The above example will output:

See Also

  • array_search() - Searches the array for a given value and returns the first corresponding key if successful
  • isset() - Determine if a variable is declared and is different than null
  • array_key_exists() - Checks if the given key or index exists in the array

User Contributed Notes 8 notes

Loose checking returns some crazy, counter-intuitive results when used with certain arrays. It is completely correct behaviour, due to PHP's leniency on variable types, but in "real-life" is almost useless.

The solution is to use the strict checking option.

$array = array(
'egg' => true ,
'cheese' => false ,
'hair' => 765 ,
'goblins' => null ,
'ogres' => 'no ogres allowed in this array'
);

// Loose checking -- return values are in comments

// First three make sense, last four do not

in_array ( null , $array ); // true
in_array ( false , $array ); // true
in_array ( 765 , $array ); // true
in_array ( 763 , $array ); // true
in_array ( 'egg' , $array ); // true
in_array ( 'hhh' , $array ); // true
in_array (array(), $array ); // true

in_array ( null , $array , true ); // true
in_array ( false , $array , true ); // true
in_array ( 765 , $array , true ); // true
in_array ( 763 , $array , true ); // false
in_array ( 'egg' , $array , true ); // false
in_array ( 'hhh' , $array , true ); // false
in_array (array(), $array , true ); // false

I got an unexpected behavior working with in_array. I'm using following code:

// .
$someId = getSomeId (); // it gets generated/fetched by another service, so I don't know what value it will have. P.S.: it's an integer

// The actual data in my edge-case scenario:
// $someId = 0;
// $anyArray = ['dataOne', 'dataTwo'];
if ( in_array ( $someId , $anyArray )) // do some work
>
// .
?>

With PHP7.4, in_array returns boolean true.
With PHP8.1, in_array returns boolean false.

It took me quite some time to find out what's going on.

I found out that in_array will *not* find an associative array within a haystack of associative arrays in strict mode if the keys were not generated in the *same order*:

$needle = array(
'fruit' => 'banana' , 'vegetable' => 'carrot'
);

$haystack = array(
array( 'vegetable' => 'carrot' , 'fruit' => 'banana' ),
array( 'fruit' => 'apple' , 'vegetable' => 'celery' )
);

echo in_array ( $needle , $haystack , true ) ? 'true' : 'false' ;
// Output is 'false'

echo in_array ( $needle , $haystack ) ? 'true' : 'false' ;
// Output is 'true'

?>

I had wrongly assumed the order of the items in an associative array were irrelevant, regardless of whether 'strict' is TRUE or FALSE: The order is irrelevant *only* if not in strict mode.

I'd like to point out that, if you're using Enum data structures and want to compare whether an array of strings has a certain string Enum in it, you need to cast it to a string.

From what I've tested, the function works correctly:
if the array is filled with strings and you're searching for a string;
if the array is filled with Enums and you're searching for an Enum.

Here is a recursive in_array function:

$myNumbers = [
[ 1 , 2 , 3 , 4 , 5 ],
[ 6 , 7 , 8 , 9 , 10 ],
];

$array = [
'numbers' => $myNumbers
];

// Let's try to find number 7 within $array
$hasNumber = in_array ( 7 , $array , true ); // bool(false)
$hasNumber = in_array_recursive ( 7 , $array , true ); // bool(true)

function in_array_recursive ( mixed $needle , array $haystack , bool $strict ): bool
foreach ( $haystack as $element ) if ( $element === $needle ) return true ;
>

$isFound = false ;
if ( is_array ( $element )) $isFound = in_array_recursive ( $needle , $element , $strict );
>

if ( $isFound === true ) return true ;
>
>

If you're creating an array yourself and then using in_array to search it, consider setting the keys of the array and using isset instead since it's much faster.

$slow = array( 'apple' , 'banana' , 'orange' );

if ( in_array ( 'banana' , $slow ))
print( 'Found it!' );

$fast = array( 'apple' => 'apple' , 'banana' => 'banana' , 'orange' => 'orange' );

if (isset( $fast [ 'banana' ]))
print( 'Found it!' );

Источник

Оцените статью