Jump to content

array problem


jagguy

Recommended Posts

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 .

 

Link to comment
https://forums.phpfreaks.com/topic/45261-array-problem/
Share on other sites

 

 

$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

 

 

Link to comment
https://forums.phpfreaks.com/topic/45261-array-problem/#findComment-220199
Share on other sites

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'];

Link to comment
https://forums.phpfreaks.com/topic/45261-array-problem/#findComment-220298
Share on other sites

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;

Link to comment
https://forums.phpfreaks.com/topic/45261-array-problem/#findComment-220330
Share on other sites

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

Link to comment
https://forums.phpfreaks.com/topic/45261-array-problem/#findComment-220331
Share on other sites

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.

 

Link to comment
https://forums.phpfreaks.com/topic/45261-array-problem/#findComment-220369
Share on other sites

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>';


?>

Link to comment
https://forums.phpfreaks.com/topic/45261-array-problem/#findComment-220825
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.