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;
Related
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
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 have an array ($arr):
[0] => Array
(
[sv_317] => 1,650
[sv_318] => 1,254
)
[1] => Array
(
[sv_317] => 1,580
[sv_318] => 1,580
)
I am trying to use these element values as number values and so need to remove any non numeric characters (commas in the above example).
To do this I am using:
foreach($arr as $k=>$v)
{
$v[sv_317] = str_replace(",", "", $v[sv_317]);
$v[sv_317] = preg_replace('/\s+/', '', $v[sv_317]);
$v[sv_318] = str_replace(",", "", $v[sv_318]);
$v[sv_318] = preg_replace('/\s+/', '', $v[sv_318]);
echo "318 :".$v[sv_318];
echo "317 :".$v[sv_317];
}
The echos are there just to test that it is doing what I intended, and sure enough they print the values of the elements without commas or white-space.
However this didn't result in being able to use the elements numerically, so I tested the array with print_r($arr); immediately following the above loop, and the array elements appeared unaffected (i.e. still containing commas.
Is there an issue of scope here? If so how can I remove the commas permanently?
Many thanks.
Since you're trying to change $v, it needs to be a reference variable, like this:
foreach($arr as $k => &$v) {
$v[sv_318] = str_replace(",", "", $v[sv_318]);
}
...But there is more to fix in your code:
You should unset($v) at the end, so subsequent code doesn't act strangely
The array indexes should be quoted, since they are string literals.
I prefer strtr() to str_replace(), since the correspondence is clearer:
.
foreach($arr as $k => &$v) {
$v['sv_318'] = strtr( $v['sv_318'], array(','=>'') );
}; unset($v);
Also, to handle any number of values in $v, I'd use another foreach:
foreach ($arr as $key => &$subarr) {
foreach ($subarr as $subkey => &$val) {
$val = strtr( $val, array(','=>'') );
}; unset($val);
}; unset($subarr);
The foreach loop doesn't work with the array itself. It works with a copy of the array. So, when you do print_r($arr) it's displaying the original array. The actual array $arr is not modified.
From PHP foreach documentation:
In order to be able to directly modify array elements within the loop precede $value with &. In that case the value will be assigned by reference.
<?php
$arr = array(1, 2, 3, 4);
foreach ($arr as &$value) {
$value = $value * 2;
}
// $arr is now array(2, 4, 6, 8)
unset($value); // break the reference with the last element
?>
Check this SO post to understand how foreach actually works:
How does PHP 'foreach' actually work?
Hope this answers your question :)
As a php beginner, I meet a problem with calculating the elements of array in php
$effect=array("a"=>array(1,2),"b"=>array(1,2),"c"=>array(1,2));
I just want to make the result as this
$effect['a'][0]=$effect['a'][0]/$effect['a'][1];
$effect['b'][0]=$effect['b'][0]/$effect['b'][1];
$effect['c'][0]=$effect['c'][0]/$effect['c'][1];
Except do this one by one , How to do this calculation with foreach or other loop way
Your array syntax is a bit off. It should be $effect['a'][0].
The loop is trivial, and foreach was the right idea.
You can use it to iterate over all the letters using:
foreach ($effect as $letter => $numbers) {
...
}
Then put your assignment/division line in the loop, replacing the fixed 'a' and 'b' etc. with the $letter variable.
You need something like this?
foreach ($effect as $key => $val)
{
$results[$key] = $val[0] / $val[1];
}
print_r($results);
Also one counter-intuitive thing in PHP, is that arrays are passed by value by default. You can use & to get a reference to the array
$effects =array("a"=>array(1,2),"b"=>array(1,2),"c"=>array(1,2));
foreach ( $effects as $key => &$effect ) {
$effect[0] = $effect[0]/$effect[1];
unset($effect);
}
print_r( $effects );
In the screenshot, you can see i have an array of arrays. I need to find an array that contains say, 'Russia', and unset it completely. That is, for Russia, remove the element [303].
I've played around with array search but I'm sure theres a funkier way of doing this.
Chris.
$array = your array;
foreach ($array as $key => $value) {
if ($value['countryName'] == 'Russia') {
unset($array[$key]);
}
}
and if you want reorder the key , you could use:
$new_array = array_values($array);