473,908 Members | 4,845 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 27065
"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
1451
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
1904
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
5268
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
1845
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
24596
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
2574
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
5153
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
2008
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
6010
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
1233
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
10029
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9875
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
11334
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
10911
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...
0
10529
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
9719
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
8092
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
7240
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();...
1
4762
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

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.