473,406 Members | 2,451 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,406 software developers and data experts.

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 27040
"stuart" <st******@webobjectives.com.au> wrote in message
news:st****************************@news-vip.optusnet.com.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******@webobjectives.com.au> schreef in bericht
news:st****************************@news-vip.optusnet.com.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($object)) {
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
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...
7
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...
38
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...
2
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...
6
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...
4
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...
2
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...
1
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...
4
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...
1
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);...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
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...
0
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,...
0
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...

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.