String to associative array - php

I'm having trouble using preg_match_all to split a string into key value pairs. An example of my string:
"%Title:Movie%Sortable%Writer:%Indexed:false%"
Where I expect results like:
$result['Title'] = 'Movie';
$result['Sortable'] = '';
$result['Writer'] = '';
$result['Indexed'] = 'false';
I can split the string using preg_match('/%/',$str,-1,PREG_SPLIT_NO_EMPTY); but it returns an indexed array. I need an associative array so that order is not important and I can use the key in a switch statement. What would be the correct regex to use in preg_match_all?

Try with:
$input = "%Title:Movie%Sortable%Writer:%Indexed:false%";
$output = array();
$data = explode('%', $input);
foreach ($data as $item) {
list($key, $value) = explode(':', $item);
$output[$key] = $value;
}

<?php
$arr = array();
$string = "%Title:Movie%Sortable%Writer:%Indexed:false%";
$d = explode('%', $string);
foreach($d as $item){
list($key,$value) = explode(':', $item);
$arr[$key] = $value;
}
print_r($arr);
?>

Related

How to splitt a string to an array with keys and values using php

I have string and I want to splitt it to array and remove a specific part and then convert it back to a string .
$string = "width,100;height,8600;block,700;narrow,1;"
I want to search block in this string and remove it with its value "block,700;" and get the string as "width,100;height,8600;narrow,1;"
below is my code php which i tried.Please advice me
$outerARR = explode(";", $string);
$arr = array();
foreach ($outerARR as $arrvalue) {
$innerarr = explode(",", $arrvalue);
$arr[] = $innerarr;
}
for ($i = 0; $i < count($arr); $i++) {
if (in_array($arr, 'block')) {
unset($arr[$i]);
}
}
Please note that "block" in aboive string will not always contain and the value may differ. So I can't use string replace . please advice
You essentially want to replace part of your string:
$string = "width,100;height,8600;block,700;narrow,1;";
$regex = '#(block,(.*?);)#';
$result = preg_replace($regex, '', $string);
echo $result;
Try this:
$string = "width,100;height,8600;block,700;narrow,1;"
$outerARR = explode(";", $string);
$arr = array();
foreach ($outerARR as $arrvalue) {
$innerarr = explode(",", $arrvalue);
$arr[$innerarr[0]] = $innerarr[1];
}
if (array_key_exists('block', $arr)) {
unset($arr['block']);
}

How to re-order array from index value

I'm looking for a way to re-sort a string/array using index value.
Example: string (1,2,3,4,5) change to (1,5,2,3,4)
$string = "1,a,v,v|2,b,v,v|3,c,v,v";
// Convert string to array first...
$sections = explode ('|', $string);
$current_index = "1";
$new_index = "2";
$arrReorder = array();
foreach($sections as $key => $val){
if ($key = $current_index) {
$arrReorder[$new_index] = $val;
}
else {
$arrReorder[$key] = $val;
}
}
$new_string = implode("|", $arrReorder);
echo $new_string;
$new_string should output "1,a,v,v|3,c,v,v|2,b,v,v"
I found a solution for anyone trying to do the same thing:
$string = "1,2,3";
// Convert string to array first...
$sections = explode ('|', $string);
function array_move(&$sections, $oldpos, $newpos) {
if ($oldpos==$newpos) {return;}
array_splice($sections,max($newpos,0),0,array_splice($sections,max($oldpos,0),1));
}
array_move($sections, 0, 2);
print_r($sections);

How to add characters into string

I have a string
$str = "a,b,c,d,e";
And I want convert string as:
$str_convert = "'a','b','c','d','e'";
What should I do?
Try my solution:
<?php
$str = "a,b,c,d,e";
$arr = explode(',',$str);
foreach ($arr as &$value) {
$value = "'$value'";
}
$str_convert= implode(',', $arr);
echo $str_convert;
Like this:
$str = "a,b,c,d,e";
$items = split(",", $str);
$convert_str = "";
foreach ($items as $item) {
$convert_str .= "'$item',";
}
$convert_str = rtrim($convert_str, ",");
print($convert_str);
If you would like a different solution using functional programming coding style, here it is:
<?php
$str = 'a,b,c,d,e';
$add_quotes = function($str, $func) {
return implode(',', array_map($func, explode(',', $str)));
};
print $add_quotes(
$str,
function ($a) {
return "'$a'";
}
);

Split of data in PHP object

In the following code, is it possible to spilt the Object $author where white space occurs ?
<?php
$url="http://search.twitter.com/search.rss?q=laugh";
$twitter_xml = simplexml_load_file($url);
foreach ($twitter_xml->channel->item as $key) {
$a = $key->{"author"};
echo $a;
}
?>
$split = explode(' ', (string) $key->{"author"}));
OR
$split = preg_split('/\s+/', (string) $key->{"author"}));
To split by # just take $split and run in loop
foreach($split as $key => $value) {
$eta = explode('#', $value);
var_dump($eta);
}
To check if string exist use strpos
foreach($split as $key => $value) {
if (strpos($value, '#') !== 0) echo 'found';
}
Use explode:
$array = explode(' ', $key->{"author"});
There is an explode function that can easily accomplish that. So, for example:
$a = $key->{"author"};
$author = explode(" ", $a);
$first_name = $author[0];
$last_name = $author[1];
Hope that helps.
Assuming you merely care to get 2 parts: email, and "friendly name" (cause people have 1 to n number of names).
<?php
$url="http://search.twitter.com/search.rss?q=laugh";
$twitter_xml = simplexml_load_file($url);
foreach ($twitter_xml->channel->item as $key) {
$a = $key->{"author"};
preg_match("/([^ ]*) (.*)/", $a, $matches);
print_r($matches);
echo "\n";
}
?>

how to explode a string and make an echo of a chosen attribute

I have a string containing ; subdivided by , and would like to make an echo of a chosen value.
My string is $string='Width,10;Height,5;Size,1,2,3'
I want to make an echo of the Height value (echo result must be 5)
$parts = explode(';', $string);
$component = explode(',', $parts[1]); // [1] is the Height,5 portion
echo $component[1]; // 5
Or this:
$p = explode(';', $string);
$data = array();
foreach($p as $part) {
$split = explode(',',$part,2); //the 'Size' bit different that the rest. I assume 1,2,3 is the value for Size?
$data[$split[0]] = $split[1];
}
$what_you_want_to_find = 'Height';
echo $data[$what_you_want_to_find];
try this:
$attrs = explode(";", $string);
$attrHeight = "";
foreach ($attrs as $value) {
if (strpos($value, "Height") !== false)
$attrHeight = explode(",", $value);
}
echo $attrHeight;

Categories