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.

If have a array.I want to compare my array value with a string .

here i have a value $uid=something.

if my array item objectId is same with $uid then those element goes to new array .

    Array
    (
        [results] => Array
            (
                [0] => Array
                    (
                        [birthday] => 11-Apr-2014
                        [category] => Array
                            (
                                [0] => 204
                                [1] => 300
                                [2] => 304
                            )

                        [city] => fgfg
                        [country] => fgf
                        [email] => [email protected]
                        [fullName] => fg
                        [gender] => Male
                        [inspirational] => Run to dream
                        [phone] => gfg
                        [photo] => Array
                            (
                                [__type] => File
                                [name] => 8bef9bc3-ee64-45df-9698-0466e255c1bd-profilePhoto.jpg
                                [url] => http://files.com/c2dcf728-e2a3-4b2d-a8c8-6ec9b3c6502a/8bef9bc3-ee64-45df-9698-0466e255c1bd-profilePhoto.jpg
                            )

                        [username] => aapbd
                        [website] => http://aahgh.com
                        [createdAt] => 2014-04-10T19:01:16.396Z
                        [updatedAt] => 2014-04-28T07:36:18.459Z
                        [objectId] => IQSCdXE2hI
                    )

                [1] => Array
                    (
                        [birthday] => 09-Apr-1982
                        [category] => Array
                            (
                                [0] => 204
                                [1] => 307
                                [2] => 311
                                [3] => 313
                                [4] => 102
                                [5] => 103
                                [6] => 105
                                [7] => 107
                            )

                        [city] => Madrid
                        [country] => Spain
                        [coverPhoto] => Array
                            (
                                [__type] => File
                                [name] => aa53cf65-47af-464d-aa49-88202f91388f-coverPhoto.jpg
                                [url] => http://files.com/c2dcf728-e2a3-4b2d-a8c8-6ec9b3c6502a/aa53cf65-47af-464d-aa49-88202f91388f-coverPhoto.jpg
                            )

                        [description] => a lazy man
                        [email] => [email protected]
                        [fullName] => Shghghossain
                        [gender] => Male
                        [inspirational] => Honesty is the best policy
                        [phone] => 135469
                        [photo] => Array
                            (
                                [__type] => File
                                [name] => a1aec283-f3c7-484c-a8b2-a0b09c5f3023-profilePhoto.jpg
                                [url] => http://files.com/c2dcf728-e2a3-4b2d-a8c8-6ec9b3c6502a/a1aec283-f3c7-484c-a8b2-a0b09c5f3023-profilePhoto.jpg
                            )

                        [username] => asa
                        [website] => 
                        [createdAt] => 2014-04-09T07:58:19.043Z
                        [updatedAt] => 2014-05-07T11:13:40.671Z
                        [objectId] => iVb6olefaT
                    )

        )

)

I use this code but not work

$ids2 =array();
foreach($array_post['results'] AS $i)
{
$ids ='iVb6olefaT';

        if(in_array($i['objectId']==$ids))
        {
        $ids2[]=$i;


         }
         else
         {
         $ids2;
         }
}
share|improve this question
    
if(in_array($i['objectId']==$ids)) is that new syntactic sugar? –  bwoebi 19 hours ago
    
You don't need in_array. Just if($i['objectId'] == $ids). And you can put $ids = '...' before the foreach loop. It's cleaner this way. –  Alex Sorin Dachin 19 hours ago
    
else { $ids2 } does nothing. it's essentially a null statement. –  Marc B 19 hours ago
    
i am wrong but give me Suggestion @bwoebi –  user3533255 19 hours ago
add comment

1 Answer

Try this:

$ids2 = array();
$ids = 'iVb6olefaT';

foreach($array_post['results'] AS $i) 
    if($i['objectId']==$ids)
        $ids2[] = $i;
share|improve this answer
add comment

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.