2

i have attached the screenshot of my output. Now its an array format, i want to split those array values from javascript.

View File:

<form name="sortdata" method="POST" action="<?php echo base_url;?>home/sortby">
<select id="sortMyData" name="sortMyData" class="sortMydata">
    <option value="lowhigh">Low to high price</option>
    <option value="highlow">High to low price</option>
</select>
<input type="hidden" id="baseurl" name="baseurl" value="<?php echo base_url;?>">
</form>

<div id="sortbyprice" class="span6">


</div>

<script type="text/javascript">
$(document).ready(function()
{
    $(".sortMydata").change(function()
{
var baseurl=$("#baseurl").val();
var sortMyData=$("#sortMyData").val();
//alert(baseurl);
alert(sortMyData);
$.ajax
({
type:"GET",
url:baseurl + 'product/sortbydetails/' + sortMyData,
//data:this.value,
cache: false,
success:function(html)
    { 
        var length=html.length;
        alert(html);
        alert(html[0]); 

    }
});
});
});
</script>

Controller:

public function sortbydetails()
    {
        $lowhigh=$this->uri->segment(3);
        if($lowhigh=="lowhigh")
        {
            $lowtohighprice=$this->product_model->getlowtohighprice();  
            $count=count($lowtohighprice);
            //echo $count;

            for ($i=0; $i < $count ; $i++) 
                { 

                    foreach ($lowtohighprice as $key => $value) {
                    $sortby[] = array_values($value);

                }
                    print_r($sortby);
            }
            //print_r($lowtohighprice);
        }
        else
        {
            $hightolowprice=$this->product_model->gethightolowprice();
            //print_r($hightolowprice);
        }

    }

I am trying to split the array values from javascript. I wrote an jquery ajax code and display the output in a div. Now am getting the output as an array values, i want to split that array values. I spend a whole day to achieve this task, but i couldnt able to resolve.

2
  • we use split function to make an array from a string which has repetition of a character.what you exactly asking for? Commented Sep 14, 2013 at 14:28
  • You should use console.log() rather than alert(). It's much easier to work with, and the output will arrive in the JavaScript console you already have showing up on your screen. Commented Sep 16, 2013 at 4:35

2 Answers 2

2

I could not get what you want exactly. You can use any loop to get all values from array.

var myarray = [1,2,3,4];
for(var i= 0; i< myarray.length; i++)
{
alert(myarray[i]);
}

EDIT:

For 2 dimensional arrays

var myarray = [[1,2,3,4], [5,6,7,8]];
for(var i= 0; i< myarray.length; i++)
  for(var j=0; j < myarray[i].length; j++)
     {
      alert(myarray[i][j]);
     }
Sign up to request clarification or add additional context in comments.

1 Comment

i have attached the screenshot of my output. Now its an array format, i want to split those array values from javascript.
0

IN ES6

you can use map

var myarray = [1,2,3,4];

myarray.map(a=>alert(a));

it also return values

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.