homepage Welcome to WebmasterWorld Guest from 54.226.191.80
register, free tools, login, search, pro membership, help, library, announcements, recent posts, open posts,
Become a Pro Member
Visit PubCon.com
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
johnhh




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

Hi

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

 

swa66




msg:4627993
 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:
$id=xx;
$foo=$array[$id]['data'];
$bar=$array[$id]['name'];

JD_Toims




msg:4627994
 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:

$info=array();

Start loop:

$info['id'][]=$id;
$info['data'][]=$data;
$info['name'][]=$name;

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";
}

johnhh




msg:4628038
 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.

swa66




msg:4628204
 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);
instead.

johnhh




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

@swa66

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 !

swa66




msg:4628219
 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:
print_r($xml);

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

johnhh




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

@swa66
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