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 如何在scanpy上做差异基因和通路富集?
  • ¥20 关于#硬件工程#的问题,请各位专家解答!
  • ¥15 关于#matlab#的问题:期望的系统闭环传递函数为G(s)=wn^2/s^2+2¢wn+wn^2阻尼系数¢=0.707,使系统具有较小的超调量
  • ¥15 FLUENT如何实现在堆积颗粒的上表面加载高斯热源
  • ¥30 截图中的mathematics程序转换成matlab
  • ¥15 动力学代码报错,维度不匹配
  • ¥15 Power query添加列问题
  • ¥50 Kubernetes&Fission&Eleasticsearch
  • ¥15 報錯:Person is not mapped,如何解決?
  • ¥15 c++头文件不能识别CDialog