473,594 Members | 2,675 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

retrieving an object from an array

Hi there,
I have an array of objects, and I would like to retreive one of the
objects based on a given key and value (ie. name='john'). Is this
possible and if so what is the syntax?

thanks
Stuart
Jul 17 '05 #1
3 27054
"stuart" <st******@webob jectives.com.au > wrote in message
news:st******** *************** *****@news-vip.optusnet.co m.au...
Hi there,
I have an array of objects, and I would like to retreive one of the
objects based on a given key and value (ie. name='john'). Is this
possible and if so what is the syntax?

thanks
Stuart


yes and no, if your your array has keys like 'john' then yes,

other wise your question apears to be just like, can I retrieve an object
from an arrya based on mysocks=white. I am not truying to be mean, just
want you to understand how I see the question

I kinda need to know how name=john is tied into your array.

--
Mike Bradley
http://www.gzentools.com -- free online php tools
Jul 17 '05 #2
stuart wrote:
I have an array of objects, and I would like to retreive one of the
objects based on a given key and value (ie. name='john'). Is this
possible and if so what is the syntax?


Not sure I understand you correctly, but try this
<?php
// using @ to avoid "Notice: Creating default object from empty value in
// ..."
@$obj->name = 'Ann';
$obj->age = 7;
$arr[0] = $obj;
$obj->name = 'John';
$obj->age = 17;
$arr[1] = $obj;
$obj->name = 'Martha';
$obj->age = 15;
$arr[2] = $obj;

echo $arr[2]->name;
echo '<pre>'; print_r($arr); echo '</pre>';
?>
output is
Martha

Array
(
[0] => stdClass Object
(
[name] => Martha
[age] => 15
)

[1] => stdClass Object
(
[name] => Martha
[age] => 15
)

[2] => stdClass Object
(
[name] => Martha
[age] => 15
)

)
--
--= my mail box only accepts =--
--= Content-Type: text/plain =--
--= Size below 10001 bytes =--
Jul 17 '05 #3
"stuart" <st******@webob jectives.com.au > schreef in bericht
news:st******** *************** *****@news-vip.optusnet.co m.au...
Hi there,
I have an array of objects, and I would like to retreive one of the
objects based on a given key and value (ie. name='john'). Is this
possible and if so what is the syntax?

thanks
Stuart


Strange how different people seem to understand your question differently
(this is a hint!). This is what I think you absolutely propably need :)

for ($i = 0; $i < count($array); $i++) {
$object = $array[$i];
if ($object->name == 'john') break;
$object = NULL;
}

After this for loop $object contains the first object in the array with the
name 'john', or it is NULL if no object had that name. You can test that
very easily with the function is_null():

if (is_null($objec t)) {
echo 'I could not find an object with that name!';
}

Hope this helps.
Remon.
Jul 17 '05 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

0
1423
by: Roger Bonine | last post by:
This might be a stupid question, but I'd appreciate any help you could offer. I'm trying to pass an array of objects back to a Web service. The Web service expects this: Dim Recipients() as WS.Recipient but it won't let me create an object array of this type directly, so I'm creating an ArrayList object and using CType to convert it, like so:
7
1878
by: Brian P | last post by:
I am getting an invalid cast exception when I try to take an ArrayList with datetime values and use the ToArray method to create an object array. I need to use an object array becase I'm working with various datatypes and need the code to be as generic as possible. Most of to code involves dynamically determining the datatype, which all works.
38
5178
by: VK | last post by:
Hello, In my object I have getDirectory() method which returns 2-dimentional array (or an imitation of 2-dimentional array using two JavaScript objects with auto-handled length property - please let's us do not go into an "each dot over i" clarification discussion now - however you want to call - you call it ;-) array contains records of all files in the current dir. array contains records of all subs in the current dir
2
1829
by: Niklas | last post by:
Hi I'm creating a .NET dll, which I will use in VB 6. I have no problem declaring methods, which will take a single Object, String, Integer and so on as a parameter. I manage even to transfer arrays of Strings and integer, but I fail to pass an array of Objects in a parameter. How do I declare a method, where one of the parameters is an Object array, in .NET, which I will use in VB6? Regards /Niklas
6
24584
by: Christian Blackburn | last post by:
Hi Gang, I can't seem to figure out how to create an object array in VB.NET 2003. In VB6 you would just select an object copy and paste it and that was that. I don't know why in the heck they decided to change that. I thought it was great. I assume a 2002 answer would work in 2003 as well. Can somebody tell me how to create an object array or point me to an MSDN article regarding it? Thanks, Christian Blackburn
4
2558
by: kin | last post by:
After a I read Walkthrough: Connecting to Data in Objects (http://msdn2.microsoft.com/en-us/library/ms171892.aspx#Mtps_DropDownFilterText). I still have project on that. My School project provided me an array of objects - News, but VS 2005 seams only accept Collections like IList<News> for DataBinding. I can convert an array to IList<News> list = (IList<News>)news, but IList is not a class, so I tried to create a class that extends...
2
5141
by: jay1_z | last post by:
Here is my code in VB: Return Me.Document.GetItemValue("Subject")(0) The "GetItemValue" method returns an 'object' array, and the first item in the array is a string. This works for me in VB but when trying to convert this to C#: return doc.GetItemValue("Subject");
1
2000
by: comp.lang.php | last post by:
array_walk($result, create_function('$a, $b', 'return (strtolower($a->{$section . "_name"}) < strtolower($b->{$section . "_name"}));'), $section); I thought this would sort an object array by the property {$section . "_name"} (the variable $section has multiple values, hence, the object array might be "image_name", "video_name", "audio_name", "anything_name", but it will definitely exist, by whatever name comprises ${section}_name, in...
4
5993
by: buzzluck68 | last post by:
Hello, I am having trouble upgrading my VB apps to .net. When i started at my company there was a dll created here in vb6, and i need to use that dll for many functions that would take too much time to re-create for the limit that i am given. I have to create a new app in .net, per my instructions, but the dll uses variant arrays, and i keep getting an error whenever i pass an object array from my .net app to the vb6 dll which needs...
1
1224
Samishii23
by: Samishii23 | last post by:
I was wondering, since I'm about to embark on an heavy object array project. As I was studying arrays, I was wondering if Object arrays are treated the same? like: var arr1 = ; arr1.push(1,2,3); // arr1 = ; var obj1 = {}; obj1.push(id:1); // obj1 = {id:1};
0
7874
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
8246
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
8368
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
5738
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5404
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
3854
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
1
2383
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
1
1476
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
1205
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.