implode

Serialize vs Implode

独自空忆成欢 提交于 2019-12-01 10:46:04
What do you think is the better way to go about storing a few image id's inside a record in a MySQL database? It's just the image id's which will be used to fetch the images from a different library. Do i implode the id's in the record like 1#4#7#9#10#12 or do I just serialize the array and store that? Are there any performance benefits by using the one instead of the other? Stability preferences? I have just always used implode and explode, never really gave it much thought. Thanks. I would pefer serialize or JSON-encode. It is more flexible and for example will allow you to add image title

Alternative of php's explode/implode-functions in c#

左心房为你撑大大i 提交于 2019-11-30 10:45:31
are there a similar functions to explode/implode in the .net-framework? or do i have to code it by myself? String.Split() will explode, and String.Join() will implode. The current answers are not fully correct , and here is why: all works fine if you have a variable of type string[] , but in PHP, you can also have KeyValue arrays, let's assume this one: $params = array( 'merchantnumber' => "123456789", 'amount' => "10095", 'currency' => "DKK" ); and now call the implode method as echo implode("", $params); your output is 12345678910095DKK and, let's do the same in C#: var kv = new Dictionary

Serialize or Implode

試著忘記壹切 提交于 2019-11-30 09:34:49
I need to store lots of two-dimensional arrays inside database and was not sure what to use: serialize or implode . So I did a few tests, to find out which one is working faster and came to the conclusion it was serialize : Execution times: 1'000'000 Serialize: 1.4974119663239 seconds Implode: 2.5333571434021 seconds Explode: 4.0185871124268 seconds Unserialize: 1.6835169792175 seconds So the question: Why is implode+explode so much slower then serialize+unserialize ? PS: I found this question already, but it is not exactly what I am asking. My idea is that explode / implode operate on strings

Fastest way to implode an associative array with keys

◇◆丶佛笑我妖孽 提交于 2019-11-30 05:55:30
问题 I'm looking for a fast way to turn an associative array in to a string. Typical structure would be like a URL query string but with customizable separators so I can use ' & ' for xhtml links or ' & ' otherwise. My first inclination is to use foreach but since my method could be called many times in one request I fear it might be too slow. <?php $Amp = $IsXhtml ? '&' : '&'; $Parameters = array('Action' => 'ShowList', 'Page' => '2'); $QueryString = ''; foreach ($Parameters as $Key => $Value)

How can I implode an array while skipping empty array items?

半城伤御伤魂 提交于 2019-11-29 20:47:48
Perl's join() ignores (skips) empty array values; PHP's implode() does not appear to. Suppose I have an array: $array = array('one', '', '', 'four', '', 'six'); implode('-', $array); yields: one---four--six instead of (IMHO the preferable): one-four-six Any other built-ins with the behaviour I'm looking for? Or is it going to be a custom jobbie? You can use array_filter() : If no callback is supplied, all entries of input equal to FALSE (see converting to boolean ) will be removed. implode('-', array_filter($array)); Obviously this will not work if you have 0 (or any other value that evaluates

Alternative of php's explode/implode-functions in c#

夙愿已清 提交于 2019-11-29 15:55:01
问题 are there a similar functions to explode/implode in the .net-framework? or do i have to code it by myself? 回答1: String.Split() will explode, and String.Join() will implode. 回答2: The current answers are not fully correct , and here is why: all works fine if you have a variable of type string[] , but in PHP, you can also have KeyValue arrays, let's assume this one: $params = array( 'merchantnumber' => "123456789", 'amount' => "10095", 'currency' => "DKK" ); and now call the implode method as

How to convert an array into comma separated strings in smarty template?

跟風遠走 提交于 2019-11-29 06:17:28
问题 I've an array titled $preview_data assigned to smarty template as follows: Array ( [applicable_states] => Array ( [0] => 1 [1] => 3 [2] => 4 [3] => 10 [4] => 11 ) ) Now I want to show the above array elements as comma separated values in a div element of a smarty template. In short it should behave like implode() in php. Can someone please help me in achieving this in smarty template? Thanks in advance. 回答1: Try this: {', '|implode:$preview_data.applicable_states} It will give you what you

How to implode subarrays in a 2-dimensional array?

我只是一个虾纸丫 提交于 2019-11-28 14:23:59
I want to implode values in to a comma-separated string if they are an array: I have the following array: $my_array = [ "keywords" => "test", "locationId" => [ 0 => "1", 1 => "2"], "industries" => "1" ]; To achieve this I have the following code: foreach ($my_array as &$value) is_array($value) ? $value = implode(",", $value) : $value; unset($value); The above will also change the original array. Is there a more elegant way to create a new array that does the same as the above? I mean, implode values if they are an array in a single line of code? perhaps array_map() ? ...but then I would have

Fastest way to implode an associative array with keys

北城以北 提交于 2019-11-28 04:36:17
I'm looking for a fast way to turn an associative array in to a string. Typical structure would be like a URL query string but with customizable separators so I can use ' & ' for xhtml links or ' & ' otherwise. My first inclination is to use foreach but since my method could be called many times in one request I fear it might be too slow. <?php $Amp = $IsXhtml ? '&' : '&'; $Parameters = array('Action' => 'ShowList', 'Page' => '2'); $QueryString = ''; foreach ($Parameters as $Key => $Value) $QueryString .= $Amp . $Key . '=' . $Value; Is there a faster way? Greg You can use http_build_query() to

How to implode array with key and value without foreach in PHP

本小妞迷上赌 提交于 2019-11-27 17:09:04
Without foreach , how can I turn an array like this array("item1"=>"object1", "item2"=>"object2",......."item-n"=>"object-n"); to a string like this item1='object1', item2='object2',.... item-n='object-n' I thought about implode() already, but it doesn't implode the key with it. If foreach it necessary, is it possible to not nest the foreach? EDIT: I've changed the string EDIT2/UPDATE: This question was asked quite a while ago. At that time, I wanted to write everything in one line so I would use ternary operators and nest built in function calls in favor of foreach. That was not a good