douqiang1851 2013-07-04 07:07
浏览 98
已采纳

函数in_array() - 在多维对象数组中查找值

I'm facing a bit of a problem I can't fix myself. What I'm trying to achieve is sort of a search filter. I have an array which can variate between 1 row to +100 rows. The array is built like this:

Array
(
    [0] => Array
        (
            [0] => PK customer
            [1] => Call number
            [2] => Subject of the call
            [3] => Date created
            [4] => Date changed
        )

      )

Here is a real version of one of my array's:

stdClass Object ( [ReadOpenCallsResult] => stdClass Object ( 
   [ArrayOfstring] => Array
   ( 
      [0] => stdClass Object ( 
        [string] => Array 
        (
           [0] => 180355
           [1] => C0000448207
           [2] => TESTDOC 
           [3] => 3-7-2013 14:20:14 
           [4] => 3-7-2013 14:20:14 
             )
       [1] => stdClass Object ( 
         [string] => Array 
         (
            [0] => 180355
            [1] => C0000448209
            [2] => TESTDOC 
            [3] => 2-7-2013 14:20:14 
            [4] => 2-7-2013 14:20:14  
    ) 
 )

I have a WCF webservice which generates an array of the result of a function in C# and then sends it to my PHP page. Now I was testing the in_array function, it works perfectly with an easy array but I can't seem to make it work with a multidimensional array. I store my array into $_SESSION['searchCalls']

I tested with all kinds of array's but I can't get the 'real' array to work. I tried it this way:

$key = array_search('180335',$_SESSION['searchCalls']);

And this way

if (in_array('180335',$_SESSION['searchCalls']))

EDIT: I saw some really good examples, but.. is it possible to get all the values in the sub array when someone looks for 'C0000448207' and then get the subject of the call and the datecreated with it?

This is the function which generates the object arrays.

 public List<List<string>> ReadOpenCalls(int relation)
        {

            RidderIQSDK IQSDK = new RidderIQSDK();

                SDKRecordset inboundSet = IQSDK.CreateRecordset("R_ACTIONSCOPE", "PK_R_ACTIONSCOPE, DESCRIPTION, DATECREATED, DATECHANGED, CODE", "FK_RELATION = " + relation, "DATECREATED DESC ");
                var messages = new List<List<string>>();
                List<string> mess = new List<string>();

                if (inboundSet != null && inboundSet.RecordCount > 0)
                {
                    inboundSet.MoveFirst();

                    do
                    {
                        List<string> list = new List<string>();
                        string pkas = inboundSet.Fields["PK_R_ACTIONSCOPE"].Value.ToString();
                        string code = inboundSet.Fields["CODE"].Value.ToString();
                        string descr = inboundSet.Fields["DESCRIPTION"].Value.ToString();
                        string datecreated = inboundSet.Fields["DATECREATED"].Value.ToString();
                        string datechanged = inboundSet.Fields["DATECREATED"].Value.ToString();

                        list.Add(pkas);
                        list.Add(code);
                        list.Add(descr);
                        list.Add(datecreated);
                        list.Add(datechanged);

                        messages.Add(list);

                        inboundSet.MoveNext();

                    }
                    while (!inboundSet.EOF);
                    return messages;
                }
                    mess.Add(null);
                    messages.Add(mess);

                    IQSDK.Logout();
                    return messages;


                }

I solved it myself already, this is my solution which is kinda nasty but it works.

$roc = array('relation' => $_SESSION['username']);
$rocresponse = $wcfclient->ReadOpenCalls($roc);
$_SESSION['searchCalls'] = $rocresponse;

    foreach ($rocresponse->ReadOpenCallsResult as $key => $value){
    if (count($value) === 0) {
    }
    if (count($value) === 1) { 
        foreach ($value as $key1 => $value1){
            if (in_array($searchWord,$value1)){
                    echo "Value is in it";
                }
        }
    }
    else{
        foreach($value as $key1 => $value1){
            foreach($value1 as $key2 => $value2){
                if (array_search($searchWord,$value2)){
                    print_r($value2);
                }

            }
        }
    }
}

I'm always interested in better solutions, and maybe this solution can help someone else out too.

  • 写回答

1条回答 默认 最新

  • doufendi9063 2013-07-04 07:31
    关注

    As pointed out by Nisarg this isn't an Array its an Object. Or you need to update your question to show you are accessinng the object.

    What if you try something like this

    $SearchCalls =  $_SESSION['searchCalls'];
    if (in_array('180335',$SearchCalls->ReadOpenCallsResult)){
    
     //do some work.
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 乌班图ip地址配置及远程SSH
  • ¥15 怎么让点阵屏显示静态爱心,用keiluVision5写出让点阵屏显示静态爱心的代码,越快越好
  • ¥15 PSPICE制作一个加法器
  • ¥15 javaweb项目无法正常跳转
  • ¥15 VMBox虚拟机无法访问
  • ¥15 skd显示找不到头文件
  • ¥15 机器视觉中图片中长度与真实长度的关系
  • ¥15 fastreport table 怎么只让每页的最下面和最顶部有横线
  • ¥15 java 的protected权限 ,问题在注释里
  • ¥15 这个是哪里有问题啊?