shaddf Posted October 4, 2015 Share Posted October 4, 2015 how can i use implode() on a multi dimensional array? say I have: $cars = array ( array("Volvo",22,18), array("BMW",15,13), array("Saab",5,2), array("Land Rover",17,15) ); And i say: implode('||',$cars); what will it be like in the string ?what effect will it have on the inner child arrays of $cars?will they also have the '||' as separator? Quote Link to comment Share on other sites More sharing options...
cobusbo Posted October 4, 2015 Share Posted October 4, 2015 how can i use implode() on a multi dimensional array? say I have: $cars = array ( array("Volvo",22,18), array("BMW",15,13), array("Saab",5,2), array("Land Rover",17,15) ); And i say: implode('||',$cars); what will it be like in the string ?what effect will it have on the inner child arrays of $cars?will they also have the '||' as separator? Test it $str = implode('||',$cars); echo $string; and read here Quote Link to comment Share on other sites More sharing options...
Solution benanamen Posted October 4, 2015 Solution Share Posted October 4, 2015 (edited) $cars = array ( array("Volvo",22,18), array("BMW",15,13), array("Saab",5,2), array("Land Rover",17,15) ); function r_implode( $glue, $pieces ) { foreach( $pieces as $r_pieces ) { if( is_array( $r_pieces ) ) { $retVal[] = r_implode( $glue, $r_pieces ); } else { $retVal[] = $r_pieces; } } return implode( $glue, $retVal ); } echo r_implode( '||', $cars ) Result: Volvo||22||18||BMW||15||13||Saab||5||2||Land Rover||17||15 Source:http://www.craiglotter.co.za/2010/04/09/php-implode-a-multi-dimensional-array/ Edited October 4, 2015 by benanamen Quote Link to comment Share on other sites More sharing options...
shaddf Posted October 5, 2015 Author Share Posted October 5, 2015 $cars = array ( array("Volvo",22,18), array("BMW",15,13), array("Saab",5,2), array("Land Rover",17,15) ); function r_implode( $glue, $pieces ) { foreach( $pieces as $r_pieces ) { if( is_array( $r_pieces ) ) { $retVal[] = r_implode( $glue, $r_pieces ); } else { $retVal[] = $r_pieces; } } return implode( $glue, $retVal ); } echo r_implode( '||', $cars ) Result: Volvo||22||18||BMW||15||13||Saab||5||2||Land Rover||17||15 Source:http://www.craiglotter.co.za/2010/04/09/php-implode-a-multi-dimensional-array/ thanks this looks the right direction.but in my case each child array is a record,can't I specify a different glue in each of the children say ',' and '||'remains for the main parent Quote Link to comment Share on other sites More sharing options...
Barand Posted October 5, 2015 Share Posted October 5, 2015 (edited) You can do that, but if your aim is to reduce the array to a single string then use json_encode $cars = array ( array("Volvo",22,18), array("BMW",15,13), array("Saab",5,2), array("Land Rover",17,15) ); $str = json_encode($cars); echo $str; //--> [["Volvo",22,18],["BMW",15,13],["Saab",5,2],["Land Rover",17,15]] to recreate the array again, you then use $cars = json_decode($str, true); Edited October 5, 2015 by Barand typo correction 1 Quote Link to comment Share on other sites More sharing options...
benanamen Posted October 5, 2015 Share Posted October 5, 2015 Just what is the end result of what your trying to do with your data? I cant think of any reason you would want or need to do this. A lot of times what the code someone presents to us for an answer to is not the code that should be used to reach the ultimate goal. Quote Link to comment Share on other sites More sharing options...
shaddf Posted October 6, 2015 Author Share Posted October 6, 2015 Just what is the end result of what your trying to do with your data? I cant think of any reason you would want or need to do this. A lot of times what the code someone presents to us for an answer to is not the code that should be used to reach the ultimate goal. At the end of the day i need to loop through the main array(knowing its length from count($cars)),get each child array string,split it up one by one and save each as a single value in the same database table.I have tried this: set length=count($cars)#gotfrom php while v_counter <=length do set child = SPLIT_STRING($cars, '||', v_counter); select child ; set v_counter=v_counter+1; end while; but it gets only the first value in the array (volvo) i.e if the parent array has only one child: Volvo||22||18 it does not give me the whole string Quote Link to comment Share on other sites More sharing options...
cyberRobot Posted October 6, 2015 Share Posted October 6, 2015 (edited) Have you considered using a foreach loop to process the main array? Inside the loop, you could process each car individually. <?php $cars = array ( array("Volvo",22,18), array("BMW",15,13), array("Saab",5,2), array("Land Rover",17,15) ); foreach($cars as $currCar) { print '<pre>' . print_r($currCar, true) . '</pre>'; } ?> To be honest, I'm not sure what's going on in your code. So I'm not sure how much help this would be. But you could insert all the records in the database by doing something like this: <?php $cars = array ( array("Volvo",22,18), array("BMW",15,13), array("Saab",5,2), array("Land Rover",17,15) ); $carInformation = array(); foreach($cars as $currCar) { $carInformation[] = "('" . implode("', '", $currCar) . "')"; } $sql = "INSERT INTO yourDatabaseName (yourColumn1, yourColumn2, yourColumn3) VALUES " . implode(', ', $carInformation); print $sql; ?> Edited October 6, 2015 by cyberRobot Quote Link to comment Share on other sites More sharing options...
benanamen Posted October 6, 2015 Share Posted October 6, 2015 (edited) save each as a single value in the same database table. Ok, this is what I was looking for. Lets start from the beginning. What is the source of your array data? I am pretty sure whats going on here is all wrong. You say, save the values in the same table so it sounds like you are getting your data from a table and putting it into an array and then wanting to put it back in. Show me an sql dump of your table with sample data. Edited October 6, 2015 by benanamen Quote Link to comment Share on other sites More sharing options...
shaddf Posted October 6, 2015 Author Share Posted October 6, 2015 (edited) Ok, this is what I was looking for. Lets start from the beginning. What is the source of your array data? I am pretty sure whats going on here is all wrong. You say, save the values in the same table so it sounds like you are getting your data from a table and putting it into an array and then wanting to put it back in. Show me an sql dump of your table with sample data. this works well on the php side: $cars = array ( array("Volvo",22,18), array("BMW",15,13), array("Saab",5,2), array("Land Rover",17,15) ); function r_implode( $glue, $pieces ) { foreach( $pieces as $r_pieces ) { if( is_array( $r_pieces ) ) { $retVal[] = r_implode( $glue, $r_pieces ); } else { $retVal[] = $r_pieces; } } return implode( $glue, $retVal ); } echo r_implode( '||', $cars ) it does turn the array into a string: Result: Volvo||22||18||BMW||15||13||Saab||5||2||Land Rover||17||15 now iam using a stored procedure to insert into database and the code here works well for a single dimension array that has been imploded.now I would like to make it work for the multi deimensional array that has been imploded too.. set v_counter=1 while v_counter <=length do set child = SPLIT_STRING($cars, '||', v_counter); select child ; set v_counter=v_counter+1; end while; where length =no of items in array. so if i have the main array with 2 child arrays inside, length=2.this is where i was requesting for different separators as glue for tho main and child arrays inside it. such that i have something like: Result: Volvo||22||18|||BMW||15||13|||Saab||5||2|||Land Rover||17||15 Edited October 6, 2015 by shaddf Quote Link to comment Share on other sites More sharing options...
benanamen Posted October 6, 2015 Share Posted October 6, 2015 I am not interested in how you are trying to do what you think needs to be done. I want to now the ultimate goal, which I understand to be "save each as a single value in the same database table" The only other thing I need to see is the actual data you are working with and where it is coming from. Now your off into stored procedures. Quote Link to comment Share on other sites More sharing options...
shaddf Posted October 7, 2015 Author Share Posted October 7, 2015 (edited) I am not interested in how you are trying to do what you think needs to be done. I want to now the ultimate goal, which I understand to be "save each as a single value in the same database table" The only other thing I need to see is the actual data you are working with and where it is coming from. Now your off into stored procedures. the data is coming from a user input,user enters details about cars,these are put togetherinto an array that is imploded into string. create database cars; uuse cars; CREATE TABLE cars ( id Int Unsigned Not Null Auto_Increment, Cname VarChar(255) Not Null Default 'Untitled.txt', Esize int, Fsize int, PRIMARY KEY (id) )ENGINE=InnoDB DEFAULT CHARSET=utf8; the data iam working with is the cars details.and is coming from the array. Edited October 7, 2015 by shaddf Quote Link to comment Share on other sites More sharing options...
benanamen Posted October 7, 2015 Share Posted October 7, 2015 Post your form and your current code. You are definitely going about this all wrong. Quote Link to comment Share on other sites More sharing options...
shaddf Posted October 7, 2015 Author Share Posted October 7, 2015 $cars = array ( array("goat",22,18), array("pig",15,13), array("rabbit",5,2), array("doe",17,15) ); i have that array. and i would like it to be imploded like so: Result: goat||22||18~pig||15||13~rabbit||5||2~doe||17||15 how can I adjust this function to do the work; function r_implode( $glue, $pieces ) { foreach( $pieces as $r_pieces ) { if( is_array( $r_pieces ) ) { $retVal[] = r_implode( $glue, $r_pieces ); } else { $retVal[] = $r_pieces; } } return implode( $glue, $retVal ); Quote Link to comment Share on other sites More sharing options...
Barand Posted October 7, 2015 Share Posted October 7, 2015 foreach($cars as $a) { $tmp[] = join('||', $a); } $result = join('~', $tmp); echo $result; //--> goat||22||18~pig||15||13~rabbit||5||2~doe||17||15 Quote Link to comment Share on other sites More sharing options...
maxxd Posted October 7, 2015 Share Posted October 7, 2015 Is there a reason you're doing it this way and not just JSON encoding the entire multi-dimensional array? Seems like it'd be easier on both the encode and decode phases if there's not a specific need for this format (I'm assuming you're going to explode the arrays later for use somewhere...). Quote Link to comment Share on other sites More sharing options...
Barand Posted October 7, 2015 Share Posted October 7, 2015 @maxxd, He is intent on doing this way regardless of attempts to advise otherwise http://forums.phpfreaks.com/topic/298429-imploding-a-multi-dimensional-array/?do=findComment&comment=1522337 Quote Link to comment Share on other sites More sharing options...
benanamen Posted October 7, 2015 Share Posted October 7, 2015 Dude, why are you starting a whole new thread on the same exact thing? You're totally trying to do this wrong and if you provided what I asked for I would show you how to do it. I give up. Attention moderator, this is a duplicate post. Quote Link to comment Share on other sites More sharing options...
Barand Posted October 7, 2015 Share Posted October 7, 2015 Topics merged Quote Link to comment Share on other sites More sharing options...
maxxd Posted October 7, 2015 Share Posted October 7, 2015 @maxxd, He is intent on doing this way regardless of attempts to advise otherwise http://forums.phpfreaks.com/topic/298429-imploding-a-multi-dimensional-array/?do=findComment&comment=1522337 Ahhh... Well that explains why I was very confused just now when there were many more posts than I remembered! Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.