I have this array :
Array
(
[0] => Array
(
[0] => Windows#XP
[1] => 3620
)
[1] => Array
(
[0] => Windows#Vista
[1] => 1901
)
[2] => Array
(
[0] => Windows#7
[1] => 88175
)
and so on...I want to replace # with an space here's my code and it doesnt seem to work:
$tab_os = str_replace('#',' ',$tab_os);
Any solution for that?
Thanks!
You can try this
foreach ($tab_os as $key => $value){
$tab_os[$key] = str_replace('#',' ',$value);
}
but really, str_replace accepts and returns arrays so this shouldn't be the problem. see docs
I have tried and tested this code, try it here: http://codepad.org/Ok1fZ16O
$tab_os = array( array( "Windows#XP", 1 ), array( "Windows#7", 1 ) );
foreach ($tab_os as $key => $value) {
$tab_os[$key] = str_replace('#', ' ', $value);
}
var_dump($tab_os);
Because loops are too mainstream (and also because it's a bit faster), you can use array_map() :
function replaceValue($value) {
return str_replace('#', ' ', $value[0]);
}
array_map('replaceValue', $tab_os);
$tab_os = array(
array('Windows#XP', 3620),
array('Windows#Vista', 1901),
array('Windows#7', 88175),
);
foreach ($tab_os as $os_k => $os_v){
$tab_os[$os_k] = str_replace('#', ' ', $os_v);
}
print_r($tab_os);
You need to loop through it
Related
I have a product variation combination ID. Hyphen (-) The characters between the strings represent the variation options id.
I want to make copies of other IDs for free variation options based on the main combination ID.
My codes:
function find_replace($array, $find, $replace){
$array = array_replace($array,
array_fill_keys(
array_keys($array, $find),
$replace
)
);
return $array;
}
function get_var_key($array, $value){
$key_name=false;
foreach ($array as $n=>$c)
if (in_array($value, $c)) {
$key_name=$n;
break;
}
return $key_name;
}
$get_free_keys = array(
"var1" => array(
"free1",
"free2"
),
"var2" => array(
"free3",
"free4"
)
);
$main_combine = "a1-b1-free1-c1-d1-free3";
$main_combine_explode = explode("-", $main_combine);
for($i=0; $i < count($main_combine_explode); $i++){
$get_key_by_value = get_var_key($get_free_keys,
$main_combine_explode[$i]); // return "var1" or "var2"
foreach($get_free_keys[$get_key_by_value] as $values){
$find_combine = find_replace($main_combine_explode,
$main_combine_explode[$i], $values);
$combines[] = implode("-", $find_combine);
}
}
print_r($combines);
Wrong result:
Array
(
[0] => a1-b1-free1-c1-d1-free3 // main combine (ok)
[1] => a1-b1-free2-c1-d1-free3 // ok
[2] => a1-b1-free1-c1-d1-free3 // wrong
[3] => a1-b1-free1-c1-d1-free4 // wrong
)
Result is incorrect
I want to get the following result:
Array
(
[0] => a1-b1-free1-c1-d1-free3-e1 // $main_combine
[1] => a1-b1-free1-c1-d1-free4-e1
[2] => a1-b1-free2-c1-d1-free3-e1
[3] => a1-b1-free2-c1-d1-free4-e1
)
or
Array
(
[var1] => Array
(
[0] => a1-b1-free1-c1-d1-free3 // $main_combine
[1] => a1-b1-free2-c1-d1-free3
)
[var2] => Array
(
[0] => a1-b1-free1-c1-d1-free4
[1] => a1-b1-free2-c1-d1-free4
)
)
Thank you.
You can use get_combinations and str-replace and do:
$template = "a1-b1-#FIRST#-c1-d1-#SECOND#-e1";
foreach (get_combinations($get_free_keys) as $e) {
$res[] = str_replace(['#FIRST#', '#SECOND#'], $e, $template);
}
Live example: 3v4l
I am having a few issues with an array being brought from a database.
The original array once converted looks like this:
Array
(
[0] => {"lastTimeSave":"1494000000"
[1] => "rankexpire":"0"
[2] => "evocity_rank":"g-vip"
[3] => "evocity_rankexpire":"0"}
)
I successfully removed some unuseful characters so my final array looks like this:
Array
(
[0] => lastTimeSave:1494000000
[1] => rankexpire:0
[2] => evocity_rank:g-vip
[3] => evocity_rankexpire:0
)
What I am wanting to do is get everything before the ':' and place it into the key of the array, then remove the ':' so it looks something like this:
Array
(
['lastTimeSave'] => 1494000000
['rankexpire'] => 0
['evocity_rank'] => g-vip
['evocity_rankexpire'] => 0
)
I am separating using:
$staffarray = str_replace('"', "", $staffarray);
$staffarray = str_replace('{', "", $staffarray);
$staffarray = str_replace('}', "", $staffarray);
I have already tried multiple things, including:
foreach ($stafftestarray as $key => $value) {
$substring = substr($value, 0, strpos($value, ';'));
$subsubstring = str_replace($substring, "", $value);
$value = $subsubstring;
}
However nothing seems to change it and the output is not being changed, I would really appreciate any help I can get with this problem as I have searched for countless hours how to fix it to no avail.
Looks like an exploded json object stored as on array.
Though I'm not sure why your data looks like that (did you explode the string by , ?)
This will do what you want:
$data = json_decode(implode(',', $yourArray), true);
Don't remove the " 's and use explode to create a new array based on your old array.
$array = array
(
0 => "lastTimeSave:1494000000",
1 => "rankexpire:0",
2 => "evocity_rank:g-vip",
3 => "evocity_rankexpire:0"
);
$new_array = array();
foreach($array as $value)
{
$item = explode(":", $value);
$new_array[$item[0]] = $item[1];
}
print_r($new_array);
Where print_r($new_array) will give:
Array
(
[lastTimeSave] => 1494000000
[rankexpire] => 0
[evocity_rank] => g-vip
[evocity_rankexpire] => 0
)
I have an array like this
array(
[0] => 'sku_name'
[1] => 'price'
[2] => 'typesku_'
)
i want to detect which index that contain 'sku_' at the exact first 4 characters in string and remove the 'sku_'
This should do the trick:
$yourArray = array(
'sku_name',
'price',
'typesku_'
);
$replaced = array_map(
function($val)
{
if (stripos($val, "sku_") === 0)
{
return substr($val, 4);
}
return $val;
},
$yourArray
);
print_r($replaced);
Output:
Array (
[0] => name
[1] => price
[2] => typesku_
)
You will get the answer with stripos
$demo=array('sku_name','price','typesku_');
foreach($demo as $k=>$d){
if(stripos($d, 'sku_')===0){
$demo[$k]= str_replace('sku_',"",$d);
}
}
Output
Array
(
[0] => name
[1] => price
[2] => typesku_
)
<?php
$arr=array(0 => 'sku_name', 1 => 'price', 2 => 'typesku_');
$i=0;
$s=0;
foreach($arr as $index){
if(stripos($index,"sku_")===0){
$str=strlen($index);
for ($i=4;$i<$str;$i++){
$index[$s]=$index[$i];
$index[$i]="\0";
$s++;
}
echo $index;
echo "<br>";
}
}
?>
This is one of the ways you can do it, I just thought of it myself, it may not be the best way, but it works, cheers! :) Hope it helps.
I have a dynamic multidimensional array and I want to convert it to string.
here is an example:
Array
(
[data] => check
[test1] => Array
(
[data] => Hello
)
[test2] => Array
(
[data] => world
)
[test3] => Array
(
[data] => bar
[tst] => Array
(
[data] => Lorem
[bar] => Array
(
[data] => doller
[foo] => Array
(
[data] => sit
)
)
)
)
[test4] => Array
(
[data] => HELLO
[tst] => Array
(
[data] => ipsum
[bar] => Array
(
[data] => Lorem
)
)
)
)
The example for string is:
check&hello&world&bar...lorem&doller...sit ....
I have tried alot of things. I even checked the solutions given on other SO questions. like:
Convert Multidimensional array to single array & Multidimensional Array to String
But No luck.
You can simply use array_walk_recursive like as
$result = [];
array_walk_recursive($arr, function($v) use (&$result) {
$result[] = $v;
});
echo implode('&', $result);
Demo
First convert it to flat array, by
$it = new RecursiveIteratorIterator(new RecursiveArrayIterator($input_array));
$flat = iterator_to_array($it, false);
false prevents array key collision.
Then use implode,
$str = implode('&', $flat);
You can use following recursive function to convert any multidimensional array to string
public function _convertToString($data,&$converted){
foreach($data as $key => $value){
if(is_array($value)){
$this->_convertToString($value,$converted);
}else{
$converted .= '&'. $value;
}
}
}
You can call above function in following way:
$str = array(
"data" => "check",
"test1" => array(
"data" => "Hello",
"test3" => array(
"data" => "satish"
)
),
"test2" => array(
"data" => "world"
)
);
$converted = "";
//call your function and pass your array and reference string
$this->_convertToString($str,$converted);
echo $converted;
Output will be following:
check&Hello&satish&world
you can modify code to meet your requirement.
Let me know if any further help required.
php has some build in functions that can do this. Like var_dump, json_encode and var_export. But if you want to control the output more it can be doen with a recursive function
function arrToStr(array $data)
{
$str = "";
foreach ($data as $val) {
if (is_array($val)) {
$str .= arrToStr($val);
} else {
$str .= $val;
}
}
return $str;
}
You can format extra line breaks and spaces at will with this.
I would use recursion for this type of array :
echo visit($your_array);
function visit($val){
if( !is_array($val) ){
return $val;
}
$out="";
foreach($val as $v){
$out.= "&".visit($v);
}
return $out;
}
I have already read this question and doesn't answer my issue.
I have an Array like this:
Array
(
[0] => Array
(
[COM] => 10659.68
)
[1] => Array
(
[MCD] => 1219.09
)
[2] => Array
(
[MCR] => 77047.65
)
)
And when I make a json_encode() it return this;
[{"COM":10659.68},{"MCD":1219.09},{"MCR":77047.65}]
What I need is get the data in this way:
[["COM":10659.68],["MCD":1219.09],["MCR":77047.65]]
Any idea how can I achieve this
Even though that's not a valid JSON, you can replace the { with [
echo str_replace(array('{','}'),array('[',']'),json_encode($your_array));
Depending on the content you might need a more complex replacement with regular expressions.
More complex solution:
function toJson($arr){
$return = array();
foreach($arr as $k => $v){
if(is_array($v)) $return[] = toJson($v);
else $return[] = sprintf('"%s":%s', $k, $v);
}
return sprintf('[%s]', implode(',', $return));
}
Test:
$input = array(
array('COM' => '10659.68'),
array('MCD' => '1219.09'),
array('MCR' => '77047.65'),
);
var_dump(toJson($input));
string(51) "[["COM":10659.68],["MCD":1219.09],["MCR":77047.65]]"