#bestPractice #php #js #py
Join an array into String in (PHP, JS, PY)
Bad practice:
//python
print("a" + " " + "b)
//php
echo "a"." ".b";
Best Practice:
// js
console.log(["a", "b"].join(" "));
//python
print(" ".join(["a", "b"])
//php
echo implode(" ", ["a", "b"]);
Join an array into String in (PHP, JS, PY)
Bad practice:
//python
print("a" + " " + "b)
//php
echo "a"." ".b";
Best Practice:
// js
console.log(["a", "b"].join(" "));
//python
print(" ".join(["a", "b"])
//php
echo implode(" ", ["a", "b"]);
#py
Convert a string into dict in Py
Convert a string into dict in Py
def strDict(string):
result = {}
pairs = string.split(";")
for pair in pairs:
if "=" in pair:
split_pair = pair.split("=", 1)
if len(split_pair) == 2:
key, value = split_pair
result[key.strip()] = value.strip()
return result#py
Divide a string into parts in Py
Divide a string into parts in Py
def divide_string(string, parts=4):
if len(string) % parts == 0:
part_length = len(string) // parts
return [string[i:i+part_length] for i in range(0, len(string), part_length)]
else:
return NoneHow to merge 2 or many json dict into One in (Python, PHP, JS)
#js
Usage: Use "var" keyword to re assign the variable and use spread method to merge "...json object "
#php
Usage: array_merge used to merge arrays into one array and we need use json_decode and json_encode to make a json obj
#py
Best way to merge in python is using unpacking operator
#js
var b = { success:true }
var c = { message: "something", ...b }
console.log(c)Usage: Use "var" keyword to re assign the variable and use spread method to merge "...json object "
#php
$b = [ "success"=> true ];
$c = ["biswa":"string"];
array_merge($b, $c)
Usage: array_merge used to merge arrays into one array and we need use json_decode and json_encode to make a json obj
#py
Best way to merge in python is using unpacking operator
**dict_name here is example:b = { "success":True }
c = { "hell":None}
print({**b, **c})😱7😢5🤮4💩4😁3🤬3👎2🤔2👍1🔥1🤯1