Jump to content

imploding a multi dimensional array


shaddf
Go to solution Solved by benanamen,

Recommended Posts

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?

Link to comment
Share on other sites

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

Link to comment
Share on other sites

  • Solution
$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 by benanamen
Link to comment
Share on other sites

$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

Link to comment
Share on other sites

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 by Barand
typo correction
  • Like 1
Link to comment
Share on other sites

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

Link to comment
Share on other sites

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 by cyberRobot
Link to comment
Share on other sites

 

 

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 by benanamen
Link to comment
Share on other sites

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 by shaddf
Link to comment
Share on other sites

 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.

Link to comment
Share on other sites

 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 by shaddf
Link to comment
Share on other sites

$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 );


Link to comment
Share on other sites

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...).

Link to comment
Share on other sites

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.