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.
Related
I have a this:
$output = print_r($feedDrive, true);
But I just want the actual values, not the commas, or [0] => etc.
How can I do this?
There is foreach loop in php. You have to traverse the array.
foreach($array as $key => $value)
{
echo $value;
}
If you simply want to add commas between values, consider using implode
$string=implode(",",$array);
echo $string;
Please help me on how to group the value from foreach result:
foreach ($result as $value) {
$group = $value['Barcode'];
echo $group.'<br>';
}
the result of this:
9822550005004
9822550005004
9844660005002
9844660005002
9844660005002
9844660005002
My expected result would be:
9822550005004
9844660005002
You can use foreach and external array for getting the output like you want.
Using the foreach loop you need to store the each value in an array, here i store the value to the $arr array and makes the key as same as the value, cause yuo need the unique values, after storing the values just implode them with suitable delimiter space and get the desire output.
$arr = array();
foreach($result as $value){
$arr[$value['Barcode']] = $value['Barcode'];
}
echo implode(" ", $arr); //9822550005004 9844660005002
Using Array functions...
array_column Get all the columns from the array as name Barcode and makes anther array of them, After that array_unique choose the unique values from the returned array and also make another array of it. So now you need to implode them as you want. The implode method makes the array as string with a delimiter. Here i use space.
$arr = array_unique(array_column($result, "Barcode"));
echo implode(" ", $arr); //9822550005004 9844660005002
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
)
How does one correctly/properly convert an array like this?
For example, if I do, print_r($array);
It would print out a result like,
Array([0] => Array([0] => 5))
How did that array come to be?
I know how to convert a single array to string by using implode(). It however, doesn't work on an array inside an array.
I don't think using implode() twice will do the trick. Does anyone have any idea?
if you want to get the array as string, use print_r with the second parameter true
$string = print_r($array, true);
or serialize
$string = serialize($array);
or json_encode
$string = json_encode($array);
And if you want to use implode, use this with array_walk_recursive
function test_print($item, $key)
{
if (is_array($item))
{
echo implode(',', $item);
}
}
array_walk_recursive($array, 'test_print');
Why not just loop it using a foreach construct ?
Something like this..
<?php
$arr = array(0=>array(0=>5),1=>array(0=>6));
foreach($arr as $arr1)
{
$str.=implode(' ',$arr1).",";
}
echo rtrim($str,','); //"prints" 5,6
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