Array to string conversion error to replace two elements - php

I have this code:
<?php
$url_constructor = "http://myecommerce.dev/edit/article_name/article_id";
$cart_line_link = str_replace($url_constructor, array(
'article_id',
'article_name'
) , array(
$line['article_name'],
$line['article_id']
));
I need replace the /article_name and /article_id by the cart lines variables.
I want get a result like this example with article:
"http://myecommerce.dev/edit/blue-mug/1728"

The correct syntax of str_replace() is:
str_replace ($search, $replace, $subject);
So, try this:
$cart_line_link = str_replace(array(
'article_id',
'article_name'
), array(
$line['article_name'],
$line['article_id']
), $url_constructor);

Related

Defining array in array in array - php

I am trying to create the following array dynamically:
$aSettings = array( "text"=>
array( "icon_type" =>
array(
"name"=>"icon_type",
"method"=>"dropdown",
"option"=>"",
"default"=>""
),
"column" =>
array(
"name"=>"column_count",
"method"=>"dropdown",
"default"=>"1"
)
)
)
I am not sure how to declare the array into the array.
I have the following example code:
$aSettings=array();
$aSetting_type['text']=array();
$aSetting_name['icon_type']=array();
$aSetting_name['column']=array();
$aSetting_values1=array('name'=>'icon_type','method'=>'dropdown','option'=>'','default'=>'');
$aSetting_values2=array('name'=>'column_count','method'=>'dropdown','default'=>1);
I guess I am overlooking something very simple, but how do I put all these arrays into each other?
I want to be able to call a value from the array as:
$aSettings['text']['column']['name'];
Any ideas?
You could do:
$aSettings['text']['icon_type'] = $aSetting_values1;
$aSettings['text']['column'] = $aSetting_values2;
If you need it more dynamic, you could use variables like so:
$type = 'text';
$name1 = 'icon_type';
$aSettings[$type][$name1] = $aSetting_values1;
Collect the array is reverse lower to higher order, it would be easy to collect without confusions, for example
$icon_type = array(
"name"=>"icon_type",
"method"=>"dropdown",
"option"=>"",
"default"=>""
);
$column = array(
"name"=>"column_count",
"method"=>"dropdown",
"default"=>"1"
);
$text = array(
"icon_type" => $icon_type,
"column" => $column
);
$aSettings = array(
"text"=> $text
);
Once you collect array like this you can easily access any element in the array i.e. echo $aSettings['text']['column']['name'];

String to array conversion if string has content like an array

I have something like this:
array('fields' => array(
0 => array('name' => 'family_name',
'label' => 'Family Names'),
array('name' => given_names',
'label' => 'Given Names'),
array('name' => 'additional_names',
'label' => 'Additional Names'),
array('name' => 'honorific_prefixes',
'label' => 'Honorific Prefixes'),
array('name' => honorific_suffixes',
'label' => 'Honorific Suffixes')
)
)
in a variable as string. The whole thing is in one database field. If I output the variable, it is a string.
I would have an array with the content as subarrays. How do I convert this value into an array?
I searched with google, but I found explode and split and so on, but I think I miss the key word to find any solution.
Thank you for any help in this case.
Try using eval(), https://secure.php.net/manual/en/function.eval.php
eval("\$array = $string;");
print_r($array);
Your string is not built correctly to put it into the eval function. Two strings inside don't have the preceding quote and that will lead to a parse error. But you can correct it with:
$string = str_replace("=> given_names'", "=> 'given_names'", $string);
$string = str_replace("=> honorific_suffixes'", "=> 'honorific_suffixes'", $string);
After that you can use the answer of shapeshifter (please mark his answer as the correct one):
eval("\$array = $string;");
var_dump($array);
If you just look for a method to save and restore your arrays you could also use serialize / unserialize.

String replacing does not work. Am I overlooking something?

I am trying my best to build a generator for the fantastic WordPress Plugin Boilerplate by Tom Mc Farlin. All works out quite well. I load the file from github, extract it to a directory and replace all needed strings like 'plugin_name', 'Your name ' etc.
Unfortunately there is a protected class variable named $plugin_name and some other tiny bits. So I decided to 'repair' some flaws after replacing, like this:
// Repair some flaws
$repair_file = $newAbsDir.'/plugin-name/trunk/includes/class-'.$new_plugin_name.'.php';
$repair_file_content = file_get_contents($repair_file);
$repair_strings = array(
'$'.$new_plugin_name => '$plugin_name',
'$this->'.$new_plugin_name => '$this->plugin_name',
'get_'.$new_plugin_name => 'get_plugin_name'
);
foreach($repair_strings as $string => $replace){
$repair_file_content = str_replace($string, $replace, $repair_file_content);
}
file_put_contents($repair_file, $repair_file_content);
BUt what seemed to work quite well with my glob array of files, does simply not work with the above. I assume it has something to do with the dollar sign. Does anybody have an idea how to fix this?
From PHP manual:
If search and replace are arrays, then str_replace() takes a value from each array and uses them to search and replace on subject.
So, it's like Elias Van Ootegem said, do a simple str_replace() without foreach(). The array values are passed on the literal array and the keys on array_keys(). And as the array keys are the ones to be searched, you have to invert the array:
$repair_strings = array(
'$plugin_name' => '$'.$new_plugin_name,
'$this->plugin_name' => '$this->'.$new_plugin_name,
'get_plugin_name' => 'get_'.$new_plugin_name
);
$repair_file_content = str_replace( array_keys( $repair_strings ), $repair_strings, $repair_file_content );
Inverting the array is just to keep the logic search => replace, but to use your original array it's a matter of:
$repair_file_content = str_replace( $repair_strings, array_keys( $repair_strings ), $repair_file_content );
D'oh ...
$My_fantastic_plugin differs slightly from $my_fantastic_plugin. Sorry for the hassle and thank you for the good advice with $repair_file_content = str_replace( array_keys( $repair_strings ), $repair_strings, $repair_file_content );

how to serilaze and unserialize an array in php

I have an array like this:
$arr = array(
'type' => 'airport ',
'airport' => 'Delhi Indra Gandhi',
'address' => '',
'city' => '',
'postcode' => ''
);
I serialized it and saved it wp database.
$wpdb->insert( 'table_name', serialize($arr), '%s' );
When I am selecting this data in front end it giving me string like this
a:5:{s:4:\"type\";s:7:\"airport\";s:7:\"airport\";s:18:\"Delhi Indra
Gandhi\";s:7:\"address\";s:0:\"\";s:4:\"city\";s:0:\"\";s:8:\"postcode\";s:0:\"\";}
When I am unserializing it , it gives nothing means an empty string.
Kindly help me.
When you unserialize use urldecode and unserialize example:
$str = serialize($arr);
$strenc = urlencode($str);
Unserialize:
$array = unserialize(urldecode($strenc));

str_replace is replacing on one line, but not on another

I am running into a strange issue. I have the following code:
$foo = array(
"some" => array(
"foo" => "boohoo",
"bar" => "foobar"
),
"really" => array(
"foo" => "boohoo",
"bar" => "barfoo"
),
"strange" => array(
"foo" => "boohoo",
"bar" => "foobarfoo"
),
"occurences" => array(
"foo" => "boohoo",
"bar" => "barbaz"
)
);
$page = "";
foreach($foo as $bar)
{
$subj = $template->loadTemplate('foobar', true);
$str = "";
$str = str_replace("{foo}", $bar['foo'], $subj);
$str = str_replace("{bar}", $bar['bar'], $subj);
$page .= $str;
}
The issue here is that when the PHP Code is run, {bar} is replaced in my template but not {foo}. I switched the two str_replace lines around and I got a different result -- {foo} is replaced but {bar} isn't! I've also tried swapping it for preg_replace and nothing changed. For the record, the $template->loadTemplate() function performs no operations on the string loaded, it simply gets the template from a file.
My questions are: why does PHP behave in this fashion, and second, how can I overcome this limitation/bug?
You changing only one, because use same input string for both replaces:
$str = str_replace("{foo}", $bar['foo'], $subj);
$str = str_replace("{bar}", $bar['bar'], $subj);
Try this:
$str = str_replace("{foo}", $bar['foo'], $subj);
$str = str_replace("{bar}", $bar['bar'], $str);
As said CORRUPT you are replacing the strings voiding the previous command.
I'd add that str_replace supports Array() as parameter.
$str = str_replace(Array("{foo}","{bar}"), Array($bar['foo'], $bar['bar']) , $subj);

Categories