Jump to content

Recommended Posts

<?php

$wgt_places = array('all', 'index', 'view', 'search');

$place = 'all, index';
$data = str_replace(', ', ',', $place);

$places = explode (',', $data);

if (!in_array ($places , $wgt_places))
{
    echo 'ERROR';
    exit;
}

echo 'PASS';
exit;

?>

 

Not found error in_array()  :'(

 

Anyone can solve this?  ;D

Link to comment
https://forums.phpfreaks.com/topic/181883-solved-in_array-problem/
Share on other sites

First argument of in_array cannot be an array itself AFAIK.

 

It can be as of 4.2 they added that ability, but the how the OP was using it is incorrect. The original array would need to be multi-dimensional and the array needle would need to match an array inside the multi-dimm array being searched exactly, as far as I know :)

 

But yea, see Mad's response for the solution.

 

EDIT:

An alternative to doing the in_array would possibly doing an array_search, depending on what the OP is looking to get out of this.

but i found an example in PHP manual:

 

<?php
$a = array(array('p', 'h'), array('p', 'r'), 'o');

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";
}
?> 

 

In this example, $needle is as array. Me also searching for array to array.

Your problem is that you're searching for the array $wgt_places, not each element separately. So it would work if your array looked like this: $wtg_places = array(array('all', 'index')); To get the functionality that you want you can do this:

 

foreach($places as $place)
{
if(!in_array($place, $wdt_places))
{
	echo "$place was not found<br />\n";
}
}

Or if you want a single function just to check if all the elements of $places are in $wgt_places something like this will work:

 

function ein_array($search, $array)
{
     $found = true;
     foreach($search as $element)
          if(!in_array($element, $array)
               $found = false;
     return $found;
}

I'm going to assume English isn't your main language. What exactly is your question?

 

Firstly why replace ', ', with ',' then explode on ','. Surely you may as well have just exploded on ', ' in the first place.

 

Secondly, when passing an array as the first argument (the needle), PHP will search the second argument (the haystack) for that array. It will not seach 'the haystack' to see if it contains all values within 'the needle'. As an example, this is a value that will pass your current criteria...

 

$wgt_places = array(array('all', 'index'),'all', 'index', 'view', 'search');

$place = 'all, index';
$data = str_replace(', ', ',', $place);

$places = explode (',', $data);

print_r($places);
if (!in_array ($places , $wgt_places))
{
    echo 'ERROR';
    exit;
}

echo 'PASS';
exit;

It seems more like you want to do something like this...

 

foreach($places as $v) {
   if(!in_array($v, $wgt_places)) {
      echo 'Error';
      exit;
   }
}

echo 'Pass';
exit;

 

Edit: D'oh, snap!

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.