jagguy Posted April 2, 2007 Share Posted April 2, 2007 Hi, I have an array from a program of the type $list_box_contents[$row][$col]['text']; There are 3 columns and an indefinite no. of rows. Each array element contains a few bits of information. I want to to extract this and store in seprate variables . $list_box_contents[$row][2]['text']; has 2 bits of information, a price and a small text box with some text. How can extract the price data from this . Quote Link to comment https://forums.phpfreaks.com/topic/45261-array-problem/ Share on other sites More sharing options...
desithugg Posted April 2, 2007 Share Posted April 2, 2007 can you show some example of what the code outputs im lost Quote Link to comment https://forums.phpfreaks.com/topic/45261-array-problem/#findComment-219746 Share on other sites More sharing options...
jitesh Posted April 2, 2007 Share Posted April 2, 2007 Use nested foreach Quote Link to comment https://forums.phpfreaks.com/topic/45261-array-problem/#findComment-219773 Share on other sites More sharing options...
jagguy Posted April 2, 2007 Author Share Posted April 2, 2007 $list_box_contents[$row][2]['text'] will output eg '$29.99 add text' then a inputbox '. I can't display the input box on this forum. The 3rd column of the table under the price heading. The price and the html input textbox ( and 'Add:' text')are all in the 1 array element. http://jagguy.ej.am/zen/index.php?main_page=index&cPath=3_10 Quote Link to comment https://forums.phpfreaks.com/topic/45261-array-problem/#findComment-220199 Share on other sites More sharing options...
jagguy Posted April 3, 2007 Author Share Posted April 3, 2007 I can use a split. Now i need to know how to sort an array of type echo $list_box_contents[row][col]['text']; where there are 4 cols and many rows. I need to be able to sort by name which is col 2 $list_box_contents[row][2l]['text']; and by price which is col 4 $list_box_contents[row][4l]['text']; Quote Link to comment https://forums.phpfreaks.com/topic/45261-array-problem/#findComment-220298 Share on other sites More sharing options...
jitesh Posted April 3, 2007 Share Posted April 3, 2007 I think foreach($list_box_contents as $key => $value){ foreach($value as $key1 => $value1){ echo $value1['text'] ; } } Quote Link to comment https://forums.phpfreaks.com/topic/45261-array-problem/#findComment-220312 Share on other sites More sharing options...
jagguy Posted April 3, 2007 Author Share Posted April 3, 2007 That displays it but not sorted? I am confused myself to be honest. data in this form needs to be sorted with the cols in same order 0-3 but rows should be sorted by letter and also by price. $data=array(); //$data[row][col][text] $data[0][0]['text']=21; $data[0][1]['text']=$14; $data[0][2]['text']='cca'; $data[0][3]['text']=18; $data[1][0]['text']=21; $data[1][1]['text']=$278; $data[1][2]['text']='bb'; $data[1][3]['text']=118; sort by name we should have $data[1][0]['text']=21; $data[1][1]['text']=$278; $data[1][2]['text']='bb'; $data[1][3]['text']=118; $data[0][0]['text']=21; $data[0][1]['text']=$14; $data[0][2]['text']='cca'; $data[0][3]['text']=18; sort by price we should have $data[0][0]['text']=21; $data[0][1]['text']=$14; $data[0][2]['text']='cca'; $data[0][3]['text']=18; $data[1][0]['text']=21; $data[1][1]['text']=$278; $data[1][2]['text']='bb'; $data[1][3]['text']=118; Quote Link to comment https://forums.phpfreaks.com/topic/45261-array-problem/#findComment-220330 Share on other sites More sharing options...
MadTechie Posted April 3, 2007 Share Posted April 3, 2007 use sort($data); Elements will be arranged from lowest to highest when this function has completed. arsort($data); to sort an array in reverse order and maintain index association asort($data); to sort an array and maintain index association rsort($data); to sort an array in reverse order ksort($data); to sort an array by key Quote Link to comment https://forums.phpfreaks.com/topic/45261-array-problem/#findComment-220331 Share on other sites More sharing options...
jagguy Posted April 3, 2007 Author Share Posted April 3, 2007 I think sort is for simple arrays where i have a 2-d array. Each col number must stay in order of 0 to 3. I just want to sort the on specific col numbers eg sort the list with every row that has '2' as col number eg price is in that col. So I am sorting rows which is theonly thing that should change. Just putting sort($data) doesn't do that or any of the other basic sorts I imagine. Quote Link to comment https://forums.phpfreaks.com/topic/45261-array-problem/#findComment-220369 Share on other sites More sharing options...
jagguy Posted April 3, 2007 Author Share Posted April 3, 2007 Hi, How do i sort a 2-d array where each row (record) is a set of values like members of a class a name and a number ? If I want to sort by letters or numbers? //$mydata[row][col] $mydata[0][0]=1; $mydata[0][1]='zaaa'; $mydata[1][0]=1; $mydata[1][0]='vvv'; Quote Link to comment https://forums.phpfreaks.com/topic/45261-array-problem/#findComment-220807 Share on other sites More sharing options...
Barand Posted April 3, 2007 Share Posted April 3, 2007 try a custom sort <?php function mysort ($aa, $b) { global $sortby; if ($a[$sortby]['text']==$b[$sortby]['text']) return 0; return ($a[$sortby]['text'] < $b[$sortby]['text']) ? -1 : 1; } $data=array(); $data[0][0]['text']=21; $data[0][1]['text']=14; $data[0][2]['text']='cca'; $data[0][3]['text']=18; $data[1][0]['text']=21; $data[1][1]['text']=278; $data[1][2]['text']='bb'; $data[1][3]['text']=118; $sortby = 2; // index of name value uasort ($data, 'mysort'); echo '<pre>', print_r($data, true), '</pre>'; $sortby = 1; // index of price value uasort ($data, 'mysort'); echo '<pre>', print_r($data, true), '</pre>'; ?> Quote Link to comment https://forums.phpfreaks.com/topic/45261-array-problem/#findComment-220825 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.