homepage Welcome to WebmasterWorld Guest from
register, free tools, login, search, pro membership, help, library, announcements, recent posts, open posts,
Become a Pro Member
Home / Forums Index / Code, Content, and Presentation / PHP Server Side Scripting
Forum Library, Charter, Moderators: coopster & jatar k

PHP Server Side Scripting Forum

Extracting one row of an array
-it's late and I'm stuck

 1:28 am on Dec 5, 2013 (gmt 0)


We have some code ..keeping it simple

Start Loop ..

array_push ( $array('id'=>$id, 'data'=>$data, 'name'=> $name));

End loop...

Whatt I need to do (hopefully without looping around the array) is extract the whole row for a given id so I get the id,data,and name. Something like "get data from array where id =xx".
I cannot seem to find a way to do this.

Thanks folks



 8:33 am on Dec 5, 2013 (gmt 0)

I'd make the outer array of your multi dimensional array also associative, that way you can search it without having to do a script level linear search.

e.g. by replacing your array_push with

$array['id']=array('data'=>$data, 'name'=> $name);

After that you can extract values like:


 8:38 am on Dec 5, 2013 (gmt 0)

Not sure I completely understand [the OP -- was posting at the same time as swa]. Why not something like:


Start loop:


End loop:

$info_found=array_search("the id you're looking for",$info['id']);

if($info_found) {
- $the_info_you_need_for_id=$info['id'][$info_found];
- $the_info_you_need_for_data=$info['data'][$info_found];;
- $the_info_you_need_for_name=$info['name'][$info_found];
else {
- echo "didn't find the info";


 11:47 am on Dec 5, 2013 (gmt 0)

Thanks @swa66 and @JD_Toims

I didn't think about an array inside in an array per id !

I'll give both concepts a go and use whatever works for me as both look possible.

Thanks for the quick replies.


 11:04 pm on Dec 5, 2013 (gmt 0)

Ok, I clearly wasn't thinking when I wrote
$array['id']=array('data'=>$data, 'name'=> $name);

Should have written:
$array[$id]=array('data'=>$data, 'name'=> $name);


 12:15 am on Dec 6, 2013 (gmt 0)


That's OK I figured it out !

I used both concepts in the end , although I had to go multi-dimensional :(
$array[$id]=array('data'=>$data, 'name'=> $name);
$array[$id][$widgettype]=array('colour'=>$colour, 'voltage'=> $voltage);

I am consuming xml so it got a bit complex, as the data was fairly deep and complex. Obviously I didn't actually use $name type of variables but got the data from the xml elements !

Worked in the end so thanks both of you - now off to um, design a page !


 12:31 am on Dec 6, 2013 (gmt 0)

When parsing XML, I have found it benificial to use PHP's "simpleXML" instead of trying to parse it myself (even if I don't need the whole tree, the library is quite efficient ...)

Ref: [php.net...]

using e.g. simplexml_load_file is nearly trivial:

$xml = simplexml_load_file('test.xml');
and the $xml variable contains your xml content, ready to use:

[You can also load from a string etc.]
ref: [php.net...]


 12:50 am on Dec 6, 2013 (gmt 0)

I actually did use simpleXML - but the really complex data has so many if's and but's so to display it the way I want straight from xml source would be a problem.

I needed to produce something that was actually understandable so I pushed it into arrays so I can manipulate the data easier and ignore the bits I don't want, as some of the feed ( which is external to us )is actually wrong.

Seems fine and fairly fast.

Global Options:
 top home search open messages active posts  

Home / Forums Index / Code, Content, and Presentation / PHP Server Side Scripting
rss feed

All trademarks and copyrights held by respective owners. Member comments are owned by the poster.
Home ¦ Free Tools ¦ Terms of Service ¦ Privacy Policy ¦ Report Problem ¦ About ¦ Library ¦ Newsletter
WebmasterWorld is a Developer Shed Community owned by Jim Boykin.
© Webmaster World 1996-2014 all rights reserved