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.
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥20 我想使用一些网络协议或者部分协议也行,主要想实现类似于traceroute的一定步长内的路由拓扑功能
  • ¥30 深度学习,前后端连接
  • ¥15 孟德尔随机化结果不一致
  • ¥15 apm2.8飞控罗盘bad health,加速度计校准失败
  • ¥15 求解O-S方程的特征值问题给出边界层布拉休斯平行流的中性曲线
  • ¥15 谁有desed数据集呀
  • ¥20 手写数字识别运行c仿真时,程序报错错误代码sim211-100
  • ¥15 关于#hadoop#的问题
  • ¥15 (标签-Python|关键词-socket)
  • ¥15 keil里为什么main.c定义的函数在it.c调用不了