I am trying to insert the contents of an array in to a string using PHP.
My array ($array1) looks like this:
Array1
(
[0] => http://www.example.com/1
[1] => http://www.example.com/2
)
I want to insert both links in to a coma separated string, so I can then insert it in to a database field.
I tried this:
foreach ($array1 as $name => $value) {
$string1 .= $value . ",";
}
echo $string1;
Which does work, but I am doing this twice in my code for another array that I also want in a separate string ($string2)
Array2
(
[0] => http://www.example.com/3
[1] => http://www.example.com/4
)
When I echo $string1 I get the correct output
http://www.example.com/1,http://www.example.com/2
But $string2 becomes this:
http://www.example.com/1,http://www.example.com/2,http://www.example.com/3,http://www.example.com/4
This happens even if I use different variable names in the foreach loop above.
Someone else also suggested I try this:
$string1 = implode(',' , $array1);
But I'm not getting any output.
Any help as to how to solve this, or any different approach is greatly appreciated!
There's a PHP function called implode for this exact purpose.
$csv = implode(',', $array);
echo $csv; //blah,blah,blah,blah
implode should work fine. It's won't give you any output unless you echo or otherwise output the result, of course.
$array1 = array("http://www.example.com/1", "http://www.example.com/2");
$array2 = array("http://www.example.com/3", "http://www.example.com/4");
echo implode(", ", array_merge($array1,$array2));
Output:
http://www.example.com/1, http://www.example.com/2, http://www.example.com/3, http://www.example.com/4
Related
Here's why i use print_r
preg_match($regexpattern, $string, $matches);
$array = print_r($matches);
and this will result the array like below
i have try to get the array result without printing it, but its doesnt work
Array
(
[0] => https://example.com/page-1
)
Question, how to get the $array in single value like:
echo $array['0']; and will show result like this https://example.com/page-1
Thank you
I Understand What You wants to say
use foreach loop insted of print_r()
<?php
$array = array("https://example.com/page-1");
foreach($array as $key => $value)
{
echo $value;
}
echo "<hr>";
echo $array [0];
?>
Also check Result Here.
I have an array with some keys and I want to get the array values according to the array keys where the keys are in a string.
Example:
$arr = array(
"COV" => "Comilla Victorians",
"RK" => "Rajshaji Kings"
);
$str = "COV-RK";
Now I want to show Comilla Victorians VS Rajshaji Kings.
I can do it using some custom looping, But I need some smart coding here and looks your attention. I think there are some ways to make it with array functions that I don't know.
You could do something like:
echo implode(' VS ', array_map(function($v) use ($arr) { return $arr[$v]; }, explode('-', $str)));
So explode the string, map the resulting array, returning the value of the matching key in $arr, then just implode it.
You could try this:-
<?php
$arr = array(
"COV" => "Comilla Victorians",
"RK" => "Rajshaji Kings"
);
$str = "COV-RK";
$values = explode("-", $str); // explode string to get keys actually
echo $arr[$values[0]] . " VS " . $arr[$values[1]]; // print desired output
I have a string like this:
$string = '[miniTrack, boxTrack]'
I want to turn it into an array of variables like this:
$array = [$miniTrack, $boxTrack];
So that I can perform loops and iterations through the variable. And access the variables they are referencing.
I tried eval but I can't figure out how to convert it to a PHP Variable.
I KNOW THAT THIS IS NOT IDEAL, THE STRUCTURE OF THE DATABASE THIS IS PULLING FROM CAN'T BE ADJUSTED UNFORTUNATELY
Your question starts unusually, because you show an array containing a single string that is comma-separated, rather than an array of individual strings.
You could try something like the following:
$arr = [];
$string = ['miniTrack, boxtrack'];
//split the one string into an array, trimming spaces
$vars= array_map('trim', explode(',', $string[0]));
foreach($vars as $var) {
$arr[] = $var; //add the var to the array
}
print_r($arr);
Array
(
[0] => miniTrack
[1] => boxtrack
)
And if you need to create a variable for each item, you can create "variable variables":
foreach($vars as $var) {
$my_var = $$var; //variable variable
}
It should be as easy as the following:
preg_match_all('~(\w+)~','[miniTrack, boxTrack]', $matches);
foreach($matches[1] as $var)
{
print $$var;
}
You can convert your strings to array like this. It may be not ideal but you can use try this.
$string = '[miniTrack, boxTrack]';
$string = str_replace(array('[',']'),array('', '' ), $string);
$array = explode(',',$string);
Here you can iterate your array whatever you want.
so I have this variable $_GET which receives the value such as
set=QnVzaW5lc3M=|RmluYW5jZQ==
The values are base64 enconded using base64_encode() and then separated by a delimiter '|'. I'm using implode function to generate the value of the set variable.
Now, the question is, how can I get the values from the set variable into an array and base64 decode them as well ?
Any suggestions are welcome.
I tried this :-
$test = array();
$test = explode('|', $_GET['set']);
var_dump($test);
This throws the value which is not usable.
But this made no difference.
$data = 'QnVzaW5lc3M=|RmluYW5jZQ==';
$result = array_map(
'base64_decode',
explode('|', $data)
);
var_dump($result);
This should work using foreach:
// Set the test data.
$test_data = 'QnVzaW5lc3M=|RmluYW5jZQ==';
// Explode the test data into an array.
$test_array = explode('|', $test_data);
// Roll through the test array & set final values.
$final_values = array();
foreach ($test_array as $test_value) {
$final_values[] = base64_decode($test_value);
}
// Dump the output for debugging.
echo '<pre>';
print_r($final_values);
echo '</pre>';
The output is this:
Array
(
[0] => Business
[1] => Finance
)
I am new PHP question and I am trying to create an array from the following string of data I have. I haven't been able to get anything to work yet. Does anyone have any suggestions?
my string:
Acct_Status=active,signup_date=2010-12-27,acct_type=GOLD,profile_range=31-35
I want to dynamically create an array called "My_Data" and have id display something like my following, keeping in mind that my array could return more or less data at different times.
My_Data
(
[Acct_Status] => active
[signup_date] => 2010-12-27
[acct_type] => GOLD
[profile_range] => 31-35
)
First time working with PHP, would anyone have any suggestions on what I need to do or have a simple solution? I have tried using an explode, doing a for each loop, but either I am way off on the way that I need to do it or I am missing something. I am getting something more along the lines of the below result.
Array ( [0] => Acct_Status=active [1] => signup_date=2010-12-27 [2] => acct_type=GOLD [3] => profile_range=31-35} )
You would need to explode() the string on , and then in a foreach loop, explode() again on the = and assign each to the output array.
$string = "Acct_Status=active,signup_date=2010-12-27,acct_type=GOLD,profile_range=31-35";
// Array to hold the final product
$output = array();
// Split the key/value pairs on the commas
$outer = explode(",", $string);
// Loop over them
foreach ($outer as $inner) {
// And split each of the key/value on the =
// I'm partial to doing multi-assignment with list() in situations like this
// but you could also assign this to an array and access as $arr[0], $arr[1]
// for the key/value respectively.
list($key, $value) = explode("=", $inner);
// Then assign it to the $output by $key
$output[$key] = $value;
}
var_dump($output);
array(4) {
["Acct_Status"]=>
string(6) "active"
["signup_date"]=>
string(10) "2010-12-27"
["acct_type"]=>
string(4) "GOLD"
["profile_range"]=>
string(5) "31-35"
}
The lazy option would be using parse_str after converting , into & using strtr:
$str = strtr($str, ",", "&");
parse_str($str, $array);
I would totally use a regex here however, to assert the structure a bit more:
preg_match_all("/(\w+)=([\w-]+)/", $str, $matches);
$array = array_combine($matches[1], $matches[2]);
Which would skip any attributes that aren't made up of letters, numbers or hypens. (The question being if that's a viable constraint for your input of course.)
$myString = 'Acct_Status=active,signup_date=2010-12-27,acct_type=GOLD,profile_range=31-35';
parse_str(str_replace(',', '&', $myString), $myArray);
var_dump($myArray);