Output of var_dump($arr)
array(4) {
[0]=>
array(4) {
[0]=>
string(1) "1"
[1]=>
string(1) "2"
[2]=>
string(1) "3"
[3]=>
string(2) "4
"
}
[1]=>
array(4) {
[0]=>
string(1) "5"
[1]=>
string(1) "6"
[2]=>
string(1) "7"
[3]=>
string(2) "8
"
}
[2]=>
array(4) {
[0]=>
string(1) "9"
[1]=>
string(2) "10"
[2]=>
string(2) "11"
[3]=>
string(3) "12
"
}
[3]=>
array(4) {
[0]=>
string(2) "13"
[1]=>
string(2) "14"
[2]=>
string(2) "15"
[3]=>
string(2) "16"
}
}
i Want to remove \n in $arr array.
I tried to use array_walk($arr,'intval'); but it did not work, because it is multiple dimensional array. What's the solution?
Is there any inbuilt PHP function? Or I need to use loops and remove it?
P.S: I am a newbie, try not to get too technical.
You can just do
array_walk_recursive($arr, function(&$v) { $v = trim($v); });
You cannot use trim directly as the callback, because it doesn't accept arguments by reference, so you have to wrap it in a callback that does.
Demo https://eval.in/904410
You can achieve this via loop and array_map()
$arr = array(
array("1", "2", "3", "4\n"),
array("5", "6", "7", "8\n"),
array("9", "10", "11", "12\n"),
array("13", "14", "15", "16\n"),
);
// result array
$result = [];
// Loop thru array
foreach ($arr as $value) {
// Map thru $value with trim to remove \n then push to result
$result[] = array_map('trim', $value);
}
// Output
echo('<pre>');
print_r($result);
There are alternatives, but recursively looping through your array gives you flexibility about what to remove, not only newlines:
function removeNewline($array) {
$result = array();
foreach ($array as $key => $value) {
// If the array value is an array itself, call the function recursively
if (is_array($value)) {
$result[$key] = removeNewline($value);
} else {
// Only remove newlines from strings
if (is_string($value)) {
$result[$key] = preg_replace('/\s+/', '', $value);
} else {
$result[$key] = $value;
}
}
return $result;
}
Related
I see some questions but no work to me.
In a for loop i receive an array like that:
array(1) { [0]=> array(1) { [0]=> string(1) "4" } }
array(1) { [0]=> array(2) { [0]=> string(1) "3" [1]=> string(1) "4" } }
array(1) { [0]=> array(7) { [0]=> string(1) "1" [1]=> string(1) "2" [2]=> string(2) "30" [3]=> string(2) "43" [4]=> string(2) "65" [5]=> string(2) "53" [6]=> string(3) "634" } }
I need implode that values with "-", my desired output isa string:
4
3-4
2-30-43-65-53-634
I try some ways, but no work, some ideia for do it simple?
If it is a two dimensional array and would like to output all elements, you could use a foreach loop and output the implode of each like so:
$mainArray = [
[4],
[3, 4],
[2, 30, 43, 65, 53, 634]];
foreach($mainArray as $key => $secArray){
echo implode('-', $secArray) . '<br/>';
}
PHP Implode
Notice the return type of implode is a string.
I'm not looking for a var dump, I just want the actual data inside each key of my array.
Original String: (meta_value) 1698, 4655, 4215, 1225, 5454, 698, 410, 122, 107, 7412, 3654, 1120
$explodeme = $serial['meta_value'];
$exploded = explode(",",$explodeme[0]);
var_dump($exploded);
Returns:
array(1) { [0]=> string(1) "1" } array(1) { [0]=> string(1) "9" } array(1) { [0]=> string(1) "1" } array(1) { [0]=> string(1) "9" } array(1) { [0]=> string(1) "1" } array(1) { [0]=> string(1) "1" } array(1) { [0]=> string(1) "1" } array(1) { [0]=> string(1) "1" } array(1) { [0]=> string(1) "1" } array(1) { [0]=> string(1) "1" } array(1) { [0]=> string(1) "1" } array(1) { [0]=> string(1) "1" } array(1) { [0]=> string(1) "1" } array(1) { [0]=> string(1) "1" } array(1) { [0]=> string(1) "1" } array(1) { [0]=> string(1) "1" } array(1) { [0]=> string(1) "1" } array(1) { [0]=> string(1) "1" }
I want to print the strings for every single key of my array. IE)
Serial Number: 42848 <- From Array
Serial Number: 48281 <- From Array
I have tried:
echo $exploded;
print_r($exploded);
All return the same value (See Returns: above)
I've also tried a for each loop, but it doesn't output the serial number string I have stored inside the array.
foreach ($exploded as $item) {
echo $item . "<br>";
}
I've also tried (to no avail):
foreach ($exploded as $item) {
echo $item['string(1)'] . "<br>";
}
Please help!
If $serial['meta_value'] holds the comma separated string, like this:
$serial['meta_value'] = '1698, 4655, 4215, 1225, 5454, 698, 410, 122, 107, 7412, 3654, 1120';
Then your error was using $explodeme[0] in this expression:
$exploded = explode(",",$explodeme[0]);
$explodeme[0] is the first character of the string. (See the PHP documentation for String access and modification by character.) Instead you should just use $explodeme to get the entire string.
$explodeme = $serial['meta_value'];
$exploded = explode(",", $explodeme);
foreach ($exploded as $item) {
echo $item . "<br>";
}
I've just tested this and the below seems to be working with a simple foreach loop.
<?php
$string = "1698, 4655, 4215, 1225, 5454, 698, 410, 122, 107, 7412, 3654, 1120";
$exploded = explode(',', $string);
foreach ($exploded as $item ) {
echo $item;
}
?>
I am new to PHP so be kind. :)
I have a 3 deep array. Something like this:
Array(5) {
[0]=> array(5) {
[0]=> string(0) ""
[1]=> string(21) "0,245.19000000,864432"
[2]=> string(21) "1,245.26000000,864432"
[3]=> string(21) "2,245.49000000,864432"
[4]=> string(21) "4,245.33000000,864432"
}
[1]=> array(5) {
[0]=> string(0) ""
[1]=> string(21) "0,245.19000000,864453"
[2]=> string(21) "1,245.26000000,864453"
[3]=> string(21) "2,245.49000000,864453"
[4]=> string(21) "4,245.33000000,864453"
}
}...
I want to explode the inner string by commas ("2,245.49000000,864453") so the arrays becomes 4 deep like so:
Array(5) {
[0]=> array(5) {
[0]=> string(0) ""
[1]=> array (3)
[0]=> "0"
[1]=> "245.19000000"
[2]=> "864432"
[2]=> array (3)
[0]=> "1"
[1]=> "245.26000000"
[2]=> "864432"
[3]=> array (3)
[0]=> "3"
[1]=> "245.49000000"
[2]=> "864432"
[4]=> array (3)
[0]=> "4"
[1]=> "245.3000000"
[2]=> "864432"
[4]=> array (3)
[0]=> "5"
[1]=> "245.3300000"
[2]=> "864432"
}
}
...
So far I have:
$done = array();
for ($i = 0; $i<=count($chunks); $i++) { //loops to get size of each 2d array
$r = count($chunks[$i]);
for ($c = 0; $c<=count($chunks[$r]); $c++) { //loops through 3d array
$arrayparts = $chunks[$i][$c];
$done[] = explode(",", $arrayparts); //$arrayparts is 3d array string that is exploded each time through loop
}
}
I think this code should work but when I var_dump nothing prints?
Can someone help me learn?
Thanks!
Suggested:
$chunks is 3d array
foreach($chunks as $innerArray) {
$result[] = array_map(function($v){
return explode(",", $v);
}, $innerArray);
}
Don't make it complicated, just use this:
(Here I go through each innerArray with a foreach loop and then I go through all values with array_map() and explode it and return it to the results array)
<?php
foreach($arr as $innerArray) {
$result[] = array_map(function($v){
return explode(",", $v);
}, $innerArray);
}
print_r($result);
?>
This uses array_map() two times. Maybe a better way but I'm on beer three:
$result = array_map(function($v){
return array_map(function($v){ return explode(',', $v); }, $v);
}, $array);
I have a session that looks like this:
array(3) {
["counter"]=>
int(0)
["currentItem"]=>
string(1) "2"
["addedToCart"]=>
array(12) {
[0]=>
array(11) {
["aantal"]=>
int(1)
["id"]=>
string(1) "1"
["filmtitel"]=>
string(11) "a_bugs_life"
["film_id"]=>
string(1) "2"
["zaal_id"]=>
string(1) "1"
["zaaltitel"]=>
string(6) "zaal 1"
["tijdstip"]=>
string(8) "15:00:00"
["stoeltjes"]=>
string(2) "21"
["dag"]=>
string(8) "woensdag"
["verwijder"]=>
int(2)
["vertoningId"]=>
string(1) "3"
}
[1]=>
array(11) {
["aantal"]=>
int(1)
["id"]=>
string(1) "1"
["filmtitel"]=>
string(11) "a_bugs_life"
["film_id"]=>
string(1) "2"
["zaal_id"]=>
string(1) "1"
["zaaltitel"]=>
string(6) "zaal 1"
["tijdstip"]=>
string(8) "15:00:00"
["stoeltjes"]=>
string(1) "7"
["dag"]=>
string(8) "woensdag"
["verwijder"]=>
int(2)
["vertoningId"]=>
string(1) "3"
}
[2]=>
array(11) {
["aantal"]=>
int(1)
["id"]=>
string(1) "1"
["filmtitel"]=>
string(11) "a_bugs_life"
["film_id"]=>
string(1) "2"
["zaal_id"]=>
string(1) "1"
["zaaltitel"]=>
string(6) "zaal 1"
["tijdstip"]=>
string(8) "15:00:00"
["stoeltjes"]=>
string(2) "22"
["dag"]=>
string(8) "woensdag"
["verwijder"]=>
int(2)
["vertoningId"]=>
string(1) "3"
}
}
}
Now, from $_SESSION['addedToCart] I would like to remove arrays if they meet to certain conditions. I have tried the following.
foreach ($_SESSION["addedToCart"] as $arr) {
if ($arr["stoeltjes"] == $stoeltje && $arr['film_id'] == $id) {
unset($arr);
}
}
This doesn't seem to work, it doesn't remove anything, I did a var_dump to check if the variables $stoeltje and $id were fine and they were fine so that cant be the problem.
Am I able to use unset in this kind of situation?
foreach ($_SESSION["addedToCart"] as &$arr)
& turns your variable into a reference instead of a copy. Normally this would be sufficient. unset() only works on data within the current scope (so your foreach loop) leaving the original unchanged (See unset() for details).
Instead you can do:
foreach ($_SESSION["addedToCart"] as $key => $val)
{
if ($val["stoeltjes"] == $stoeltje && $val['film_id'] == $id) {
unset($_SESSION["addedToCart"][$key]);
}
}
Even if the suggested way with the reference should work normally, here's an example without it:
foreach ($_SESSION["addedToCart"] as $key => $arr) {
if ($arr["stoeltjes"] == $stoeltje && $arr['film_id'] == $id) {
unset($_SESSION["addedToCart"][$key]);
}
}
It doesn't work, because foreach is working on a copy, therefore $arr is just a copy of each element in the main table.
from php.net:
As of PHP 5, you can easily modify array's elements by preceding $value with &. This will assign reference instead of copying the value.
$arr = array(1, 2, 3, 4);
foreach ($arr as &$value) {
$value = $value * 2;
}
// $arr is now array(2, 4, 6, 8)
Try this:
$arr = array(1, 2, 3, 4);
foreach ($arr as $key => &$value) {
if ($value == 2)
{
unset($arr[$key]);
}
}
print_r($arr);
remove_array_key("film_id", $array);
function remove_array_key($key, &$array)
{
$result = array_key_exists($key, $array);
if ($result) {
unset($array[$key]);
return $array;
}
foreach ($array as &$v) {
if (is_array($v)) {
$result = remove_array_key($key, $v);
}
if (is_array($result)) {
unset($v[$key]);
return $array;
}
}
return false;
}
Link to Github Explanation
i have the following array:
["addToCart"]=>
array(3) {
[1]=>
array(5) {
["aantal"]=>
int(1)
["film_id"]=>
string(1) "1"
["zaal_id"]=>
string(1) "1"
["dag"]=>
string(7) "maandag"
["seats"]=>
array(4) {
[0]=>
string(2) "67"
[1]=>
string(2) "68"
[2]=>
string(2) "69"
[3]=>
string(2) "70"
}
}
You can see that i have an array called "seats" inside the "addToCart" array.There are 4 items in the "seats" array.
what i would like to have is 4 separate arrays, they should all have the same content but each of them needs to have 1 value of "seats".
I'm not sure I got exactly what you're looking to do, but this would result in an array of arrays where each has only one seat:
$seatArrays = array();
foreach ($addToCart as $arr)
{
foreach ($arr["seats"] as $seat)
{
$seatArr = $arr; // Copy the original array
$seatArr["seats"] = $seat; // Replace the "seats" subarray with the current seat
$seatArrays[] = $seatArr;
}
}