Welcome to WebmasterWorld Guest from 54.147.20.131

Forum Moderators: coopster & jatar k

Message Too Old, No Replies

Extracting one row of an array

-it's late and I'm stuck

     

johnhh

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

10+ Year Member



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

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

WebmasterWorld Senior Member swa66 is a WebmasterWorld Top Contributor of All Time 10+ Year Member



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

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

WebmasterWorld Senior Member Top Contributors Of The Month



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

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

10+ Year Member



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

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

WebmasterWorld Senior Member swa66 is a WebmasterWorld Top Contributor of All Time 10+ Year Member



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

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

10+ Year Member



@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

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

WebmasterWorld Senior Member swa66 is a WebmasterWorld Top Contributor of All Time 10+ Year Member



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

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

10+ Year Member



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

Featured Threads

Hot Threads This Week

Hot Threads This Month