[SOLVED] Extract array from another array

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
Rovas
Forum Contributor
Posts: 272
Joined: Mon Aug 21, 2006 7:09 am
Location: Romania

[SOLVED] Extract array from another array

Post by Rovas »

I made two php classes that work with the database: the first would connect to the database and perform the queries, the second one would display the results or process and send data to be introduced in the database. Since these databases don' t follow any pattern or MVC and contain a lot of redundant code, the second class, I made a new function that wraps the resulting array in other array.

Code: Select all

 
class DisplayResults extends MySQLiResult{
   
  function Generalization($ident,$query){
     $wrap=array();
     $result=array(); 
     $temp="";
     //initialise the first class
    $dataB=new PMExtender($ident['server'], $ident['DataBase'], $ident['User'], $ident['pass']);
    //QueryExec is a function that returns a MySQLi object
    $result=$dataB->QueryExec($query); 
    while($temp=$dataB->fetch_assoc()){$wrap[]=$temp;}
  }
}
 

The result is a array in an array.

Code: Select all

 
  //an example of print_r($wrap); 
  wrap=[0=>Array[
                            "Title"=>"Test first query", 
                            "Text"=>"Sample text for the tests involving this database"
                        ] 
            1=>Array[
                              "Title"=>"Welcome to the site", 
                            "Text"=>"This is my first post on these site..."
                      ]
           ]
   //I hope you got the idea 
 
The problem is that I don' t know I way to extract in a fast manner the original array from the wrapper. I tried using extract, array_walk, foreach but I got only errors or nothing was being shown.
Any ideas?
My mistake: I didn' t put the exact name and order for the array. Thanks Kendal!
Last edited by Rovas on Fri Aug 01, 2008 8:42 am, edited 1 time in total.
User avatar
kendall
Forum Regular
Posts: 852
Joined: Tue Jul 30, 2002 10:21 am
Location: Trinidad, West Indies
Contact:

Re: Extract array from another array

Post by kendall »

Code: Select all

wrap=[0=>Array[
                            "Title"=>"Test first query", 
                            "Text"=>"Sample text for the tests involving this database"
                        ] 
            1=>Array[
                              "Title"=>"Welcome to the site", 
                            "Text"=>"This is my first post on these site..."
                      ]
           ]
 

Code: Select all

 
echo $wrap[0]['Text'];
$new_arr = array();
foreach ($wrap as $arr){
print $arr['Title'];
print $arr['Text'];
$new_arr[$arr['Title']] = $arr['Text'];
}
 
seems simple ....
Post Reply