Take the 2-minute tour ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

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.

share|improve this question
    
Is using arrays as object properties somewhat reasoned? Because you don't have array of arrays but array of objects that have arrays. –  Voitcus Jul 4 '13 at 7:16
1  
You'll need recursive function or iterator. I am pretty sure this is already asked on stackoverflow. –  Leri Jul 4 '13 at 7:17
1  
it's not array, it's object array. –  Napster Jul 4 '13 at 7:18
    
This is just an example, it could go up to index +100, but I will always search on either Callnumber or Subject of Call. –  MHakvoort Jul 4 '13 at 7:19
    
array_filter is your friend –  Orangepill Jul 4 '13 at 7:33

1 Answer 1

up vote 1 down vote accepted

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.
}
share|improve this answer
    
That doesnt work :( –  MHakvoort Jul 4 '13 at 7:37
    
@Marijke Ohhh - I just noticed both your sub arrays are actually objects also. YOu will need to search each individually, or move them to arrays. Why are they objects in the first place? –  Toby Allen Jul 4 '13 at 7:39
    
My C# function makes them objects. Do you think it'll be usefull to post my function from the app too? –  MHakvoort Jul 4 '13 at 7:42
    
I posted my function too now. –  MHakvoort Jul 4 '13 at 7:48
    
I think I nailed it!! :D –  MHakvoort Jul 4 '13 at 8:02

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.