Jump to content

Getting new array of values from array


Go to solution Solved by Ch0cu3r,

Recommended Posts

Beginner here.  Not sure why but I arrays are a huge source of frustration for me.

 

I have this query:

 

        $query = $this->_db->getQuery ( true );
        $query->select( 'a.table,a.places');
        $query->from ( '#__tbb_reserves as a ');  
        $query->where ( 'id = ' . $tid );
        $this->_db->setQuery ( $query );
        $myarray =$this->_db->loadRowList();

 

resulting in this array

 

array {
  [0]=array {
    [0]= "9"
    [1]="10"
  }
  [1]= array {
    [0]= "5"
    [1]= "10"
  }
  [2]= array {
    [0]= "4"
    [1]= "6"
  }
}

 

From which I would like to create 2 new arrays:

one from key [0] $array1 = array(9,5,4) 

and one from key [1] $array2 = array(10,10,6)

 

Any help on how I would go about this is appreciated.  Please let me know if I have left anything important out in my question.

 

Also, if anyone knows of a good tutorial on the subject I would be very grateful.

 

Link to comment
https://forums.phpfreaks.com/topic/285945-getting-new-array-of-values-from-array/
Share on other sites

  • Solution

Use a foreach loop, to loop over the arrays in $myarray. Add each item in that array to separate arrays

$myarray =$this->_db->loadRowList();

$tables = array();
$places = array();

foreach($myarray as $array)
{
   $tables[] = $array[0];
   $places[] = $array[1];
}

printf('<pre>Tables %s</pre>', print_r($tables, true));
printf('<pre>Places %s</pre>', print_r($places, true));
Edited by Ch0cu3r
This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.