I have a variable, let's call it $var that echoes out something like:
32-Widgets: 18,28-Widgets: 24,57-Widgets: 45,44-Widgets: 24,55-Widgets: 45
The variable is created from a combination of a form submission and jQuery Sortables (which is why everything ends up in one variable, not two). The order is very important.
What I'd like to end up with is two variables (can be arrays) that would be:
$newVar1 = 32,18,24,45,24
$newVar2 = Widgets: 18,Widgets: 24,Widgets: 45,Widgets: 24,Widgets: 45
I started by:
$newVars = explode(",",$var);
But I'm at a loss of where to go from there. I've tried a variety of statements such as:
foreach ($newVars as $newVar) :
//Various explode() functions tried here.
endforeach;
If anybody has any idea what I'm missing I would certainly appreciate the help.
Thank you,
Eric
It's not very pretty, but it'll do the trick.
<?php
$str = "32-Widgets: 18,28-Widgets: 24,57-Widgets: 45,44-Widgets: 24,55-Widgets: 45";
$entries = explode(",", $str);
$parts1 = array();
$parts2 = array();
foreach ($entries as $e)
{
$temp = explode("-", $e);
$parts1[] = $temp[0];
$parts2[] = $temp[1];
}
print_r($parts1);
print_r($parts2);
?>
Running example: http://ideone.com/KkL06f
Have you tried something like this:
public function just_a_test() {
$var = "32-Widgets: 18,28-Widgets: 24,57-Widgets: 45,44-Widgets: 24,55-Widgets: 45";
$vars = explode(',', $var);
$partsA = $partsB = array();
foreach ($vars as $aVar) {
preg_match('/^(\d+)-(Widgets: \d+)$/i', $aVar, $parts);
$partsA []= $parts[0];
$partsB []= $parts[1];
}
echo '<pre>';
print_r($partsA);
print_r($partsB);
echo '</pre>';
}
You could easily end up with output like yours by calling the implode() with each array.
Related
Trying to use the implode() function to add a string at the end of each element.
$array = array('9898549130', '9898549131', '9898549132');
$attUsers = implode("#txt.att.net,", $array);
print($attUsers);
Prints this:
9898549130#txt.att.net,9898549131#txt.att.net,9898549132
How do I get implode() to also append the glue for the last element?
Expected output:
9898549130#txt.att.net,9898549131#txt.att.net,9898549132#txt.att.net
//^^^^^^^^^^^^ See here
There is a simpler, better, more efficient way to achieve this using array_map and a lambda function:
$numbers = ['9898549130', '9898549131', '9898549132'];
$attUsers = implode(
',',
array_map(
function($number) {
return($number . '#txt.att.net');
},
$numbers
)
);
print_r($attUsers);
This seems to work, not sure its the best way to do it:
$array = array('9898549130', '9898549131', '9898549132');
$attUsers = implode("#txt.att.net,", $array) . "#txt.att.net";
print($attUsers);
Append an empty string to your array before imploding.
But then we have another problem, a trailing comma at the end.
So, remove it.
Input:
$array = array('9898549130', '9898549131', '9898549132', '');
$attUsers = implode("#txt.att.net,", $array);
$attUsers = rtrim($attUsers, ",")
Output:
9898549130#txt.att.net,9898549131#txt.att.net,9898549132#txt.att.net
This was an answer from my friend that seemed to provide the simplest solution using a foreach.
$array = array ('1112223333', '4445556666', '7778889999');
// Loop over array and add "#att.com" to the end of the phone numbers
foreach ($array as $index => &$phone_number) {
$array[$index] = $phone_number . '#att.com';
}
// join array with a comma
$attusers = implode(',',$array);
print($attusers);
$result = '';
foreach($array as $a) {
$result = $result . $a . '#txt.att.net,';
}
$result = trim($result,',');
There is a simple solution to achieve this :
$i = 1;
$c = count($array);
foreach ($array as $key => $val) {
if ($i++ == $c) {
$array[$key] .= '#txt.att.net';
}
}
I have a variable that contains text with values according to an example below:
$data = "5:7|4:1|504:2|1:3|"
And I would like to achieve results like this:
$data[5] = 7;
$data[4] = 1;
$data[504] = 2;
$data[1] = 3;
I tried with explode:
$data = explode("|", $data);
//but it makes $data[0]="5:7"; $data[1]="4:1"; and so on.
Should I use explode again? Is it has any sense, or is there another way? I would like to ask for a hint or help.
There may be a more clever way, but I'd do it like this:
$data = array();
foreach (explode("|", $your_data) as $part)
{
$pieces = explode(':', $part);
// Assumes we have 2 pieces, might want to make sure here...
$data[$pieces[0]] = $pieces[1];
}
Also, I'm not sure what this data represents but keep in mind that array keys will overwrite each other, so 1:1|1:2 will result in an array with only one item (the last piece). There may be good reason to take another approach.
Sure, explode again:
$data = "5:7|4:1|504:2|1:3";
$array = array();
foreach (explode('|', $data) as $pair) {
list($id, $val) = explode(':', $pair);
$array[$id] = $val;
}
Yes you should use explode twice, like this
$newData = array();
$pairs = explode('|',$data);
foreach($pairs as $pair){
$tmp = explode(':',$pair);
$newData[$tmp[0]] = $tmp[1];
}
Try using a regexp:
$data = preg_split ("\||:", $data);
One line version:
$data = array_map(function($d) { return (int)explode(":", $d)[1]; }, explode("|", $data));
function test()
{
$content = "lang=en]text en|lang=sp]text sp";
$atts = explode('|', $content);
}
What I'm trying to do is to allow myself to echo $param[en] to get "text en", $param[sp] to get "text sp". Is that possible?
the $content is actually from a database record.
$param = array();
$langs = explode('|', $content);
foreach ($langs as $lang) {
$arr = explode(']', $lang);
$key = substr($arr[0], 5);
$param[$key] = $arr[1];
}
This is if you are sure $content is well-formatted. Otherwise you will need to put in additional checks to make sure $langs and $arr are what they should be. Use the following to quickly check what's inside an array:
echo '<pre>'.print_r($array_to_be_inspected, true).'</pre>';
Hope this helps
if this is not hard coded string in $content
function test()
{
$content = "lang=en]text en|lang=sp]text sp";
$atts = explode('|', $content);
foreach($atts as $att){
$tempLang = explode("]", $att);
$params[array_pop(explode("=", $tempLang[0]))] = $tempLang[1];
}
var_dump($params);
}
I think in this case you could use regular expressions.
$atts = explode('|', $content);
foreach ($atts as $subtext) {
if (preg_match('/lang=(\w+)\](\w+) /', $subtext, $regs)) {
$param[$regs[0]] = $regs[1];
}
}
Although it seems that you have a bad database structure if that value comes from a database - if you can edit it, try to make the database adhere to make the database normal.
I have a string that contains elements from array.
$str = '[some][string]';
$array = array();
How can I get the value of $array['some']['string'] using $str?
This will work for any number of keys:
$keys = explode('][', substr($str, 1, -1));
$value = $array;
foreach($keys as $key)
$value = $value[$key];
echo $value
You can do so by using eval, don't know if your comfortable with it:
$array['some']['string'] = 'test';
$str = '[some][string]';
$code = sprintf('return $array%s;', str_replace(array('[',']'), array('[\'', '\']'), $str));
$value = eval($code);
echo $value; # test
However eval is not always the right tool because well, it shows most often that you have a design flaw when you need to use it.
Another example if you need to write access to the array item, you can do the following:
$array['some']['string'] = 'test';
$str = '[some][string]';
$path = explode('][', substr($str, 1, -1));
$value = &$array;
foreach($path as $segment)
{
$value = &$value[$segment];
}
echo $value;
$value = 'changed';
print_r($array);
This is actually the same principle as in Eric's answer but referencing the variable.
// trim the start and end brackets
$str = trim($str, '[]');
// explode the keys into an array
$keys = explode('][', $str);
// reference the array using the stored keys
$value = $array[$keys[0][$keys[1]];
I think regexp should do the trick better:
$array['some']['string'] = 'test';
$str = '[some][string]';
if (preg_match('/\[(?<key1>\w+)\]\[(?<key2>\w+)\]/', $str, $keys))
{
if (isset($array[$keys['key1']][$keys['key2']]))
echo $array[$keys['key1']][$keys['key2']]; // do what you need
}
But I would think twice before dealing with arrays your way :D
This is probably a simple question, but how do you iterate through an array, doing something to each one, until the last one and do something different?
I have an array of names. I want to output the list of names separated by commas.
Joe, Bob, Foobar
I don't want a comma at the end of the last name in the array, nor if there is only one value in the array (or none!).
Update: I can't use implode() because I have an array of User model objects where I get the name from each object.
$users = array();
$users[] = new User();
foreach ($users as $user) {
echo $user->name;
echo ', ';
}
How can I achieve this and still use these objects?
Update: I was worrying too much about how many lines of code I was putting in my view script, so I decided to create a view helper instead. Here's what I ended up with:
$array = array();
foreach($users as $user) {
$array[] = $user->name;
}
$names = implode(', ', $array);
Use implode:
$names = array('Joe', 'Bob', 'Foobar');
echo implode(', ', $names); # prints: Joe, Bob, Foobar
To clarify, if there is only one object in the array, the ', ' separator will not be used at all, and a string containing the single item would be returned.
EDIT: If you have an array of objects, and you wanted to do it in a way other than a for loop with tests, you could do this:
function get_name($u){ return $u->name; };
echo implode(', ', array_map('get_name', $users) ); # prints: Joe, Bob, Foobar
$array = array('joe', 'bob', 'Foobar');
$comma_separated = join(",", $array);
output: joe,bob,Foobar
Sometimes you might not want to use implode.
The trick then is to use an auxiliary variable to monitor not the last, but the first time through the loop.
vis:
$names = array('Joe', 'Bob', 'Foobar');
$first = true;
$result = '';
foreach ($names as $name)
{
if (!$first)
$result .= ', ';
else
$first = false;
$result .= $name;
}
implode(', ', $array_of_names)
psuedocode....
integer sigh=container.getsize();
sigh--;
integer gosh=0;
foreach element in container
{
if(gosh!=sigh)
dosomething();
else
doLastElementStuff();
gosh++;
}
looking at all the other answers, it seems PHP has gotten a lot more syntactic S since I last wrote anything in it :D
I come accross this a lot building SQL statements etc.
$joiner = " ";
foreach ($things as $thing) {
echo " $joiner $thing \n";
$joiner = ',';
}
FOr some reason its easier to work out the logic if you think of the ",", "AND" or "OR" as an option/attribute that goes before an item. The problem then becomes how to suppress the the "," on the first line.
I personally found the fastest way (if you're into micro optimization) is:
if(isset($names[1])) {
foreach ($names as $name) {
$result .= $name . ', ';
}
$result = substr($result, 0, -2);
} else {
$result = $names[0];
}
isset($names[1]) is the fastest (albeit not so clear) way of checking the length of an array (or string). In this case, checking for at least two elements is performed.
I actually find it easier to create my comma delimited text a little differently. It's a bit more wordy, but it's less function calls.
<?php
$nameText = '';
for ($i = 0; $i < count($nameArray); $i++) {
if ($i === 0) {
$nameText = $nameArray[$i];
} else {
$nameText .= ',' . $nameArray[$i];
}
}
It adds the comma as a prefix to every name except where it's the first element if the array. I have grown fond of using for as opposed to foreach since I have easy access to the current index and therefore adjacent elements of an array. You could use foreach like so:
<?php
$nameText = '';
$nameCounter = 0;
foreach ($nameArray as $thisName) {
if ($nameCounter === 0) {
$nameText = $thisName;
$nameCounter++;
} else {
$nameText .= ',' . $thisName;
}
}