Hello,
I have multidimentional array.
like:

$skill_array={
Database,test db skill 2,0,1,2014-Mar,ZCZC,1
Framwork,test fm skill,0,1,2014-Mar,axaxax,0
Networking,test db skill 2,0,1,2014-Mar,yt89887i,1
Database,test db skill 2,0,1,2014-Mar,hkjk,0
}

while showing this table in UI in javascript,
I want to show table in sorted format of 0th index.
i.e in above example,
it should come as
`

$skill_array={
Database,test db skill 2,0,1,2014-Mar,ZCZC,1
Database,test db skill 2,0,1,2014-Mar,hkjk,0
Framwork,test fm skill,0,1,2014-Mar,axaxax,0
Networking,test db skill 2,0,1,2014-Mar,yt89887i,1
}`

could you help me please.
Case is,
I have declared one table inside onw div in my php code.
and I am inserting my tr and td values to that table in javascript (wher i get the final array)
I have used below code for it.

skill_array // HERE I GET THE SKILL ARRAY VALUES AS THEY ARE APPENDED IN ARRAY i.e. UNSORTED
        var last_tr_id = $('div#information-table tr:last').attr('id');
        var tr_id=++last_tr_id;
        var global_arrayLength = skill_array.length;    
        var html="";
        var html_1="";

        $('table#myTable tr').not(':first').remove(); // this function removes the already appended values to myTable

        // define tr id for table which is to be append to myTable
        var tr_tmp_value=1;
        var other_skill_tr_value=1;
        global_other_skill_arrayLength=other_skill_array.length;

        for(var index=0;index < global_arrayLength;index++)
        {
            html+='<tr id='+(tr_tmp_value++)+' >';
            for(inner_index=0;inner_index <= 5;inner_index++)
            {
                if(inner_index==2)
                {
                    html+='<td>'+skill_array[index][inner_index]+' Years and '+skill_array[index][inner_index+1]+' months</td>';
                    inner_index++;
                }
                else
                html+='<td>'+skill_array[index][inner_index]+'</td>';
            }

            // is_other_skill
            html+='<td><a onClick=edit_skill('+(tr_tmp_value-1)+','+0+');>'+EDIT+'</td>';
            html+="</tr>";

The issue i am facing in sorting is ,
In above code I have given ID tr to edit_skill() function so that on clicking on it, it allows me to edit its values and update array.

html+='<td><a onClick=edit_skill('+(tr_tmp_value-1)+','+0+');>'+EDIT+'</td>';

when I show sorted array,
then I should not loose/change my this parameter for edit_skill().

Hope I am able to put down my doubt clearly.

Recommended Answers

All 2 Replies

Hello All,
So silly I am!
I forgot about .sort function in JS :D
I have sorted array.

I used skill_array=skill_array.sort(sortFunction);
where sortFunction is as :

function sortFunction(a, b) 
    {
    return a[0] > b[0];
    }

and then added to table and shown it.

but another problem is
I want to add some <td>s (i.e. as BREAKS)after
each different skill type.
how can I do it?

Member Avatar for diafol

//EDIT

IGNORE the post below - you posted to PHP forum, not realising that you were after javascript, even though you mentioned it in your post. :(

I have multidimentional array.

No you don't, this is a regular flat array...

$skill_array={
Database,test db skill 2,0,1,2014-Mar,ZCZC,1
Framwork,test fm skill,0,1,2014-Mar,axaxax,0
Networking,test db skill 2,0,1,2014-Mar,yt89887i,1
Database,test db skill 2,0,1,2014-Mar,hkjk,0
}

...with issues

  • string and date items need quotes around them
  • a comma is required at the end of each row
  • [] instead of {}

A multidimensional array would look like this:

$skill_array=array(
    array("Database","test db skill", 2,0,1,"2014-Mar","ZCZC",1),
    array("Framwork","test fm skill",0,1,"2014-Mar","axaxax",0),
    array("Networking","test db skill", 2,0,1,"2014-Mar","yt89887i",1),
    array("Database","test db skill", 2,0,1,"2014-Mar","hkjk",0)
);

You seemed to have used {} as a shorthand, whereas you can use [] as of php5.4:

$skill_array=[
    ["Database","test db skill", 2,0,1,"2014-Mar","ZCZC",1],
    ["Framwork","test fm skill",0,1,"2014-Mar","axaxax",0],
    ["Networking","test db skill", 2,0,1,"2014-Mar","yt89887i",1],
    ["Database","test db skill", 2,0,1,"2014-Mar","hkjk",0]
];
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.