How to remove empty element in an array [duplicate] - php

This question already has answers here:
Remove empty array elements
(27 answers)
Closed 5 years ago.
I have been trying array_filter but doesn't work on my part.

If you want is to remove empty arrays inside an array, you can use this
$array= array_filter(array_map('array_filter', $array));

Related

PHP: How to make an array by a comma separated string? [duplicate]

This question already has answers here:
Split a comma-delimited string into an array?
(8 answers)
Closed 5 years ago.
I have this code: 37,40,42,46,49,54,56,57 now I wanna separate each number and convert all numbers to an Array.
The array that I want is:
array(37,40,42,46,49,54,56,57)
You can use explode() like this:
$string = "37,40,42,46,49,54,56,57";
$array = explode("," , $string);

How do I append a value to an array within an array in PHP? [duplicate]

This question already has answers here:
How to add elements to an empty array in PHP?
(8 answers)
Closed 5 years ago.
Given this PHP array:
$options['systems'] = array(1, 2, 3)
How would I append the value 4 to the $systems array within the $options array?
You could use array_push to push additional items like so:
array_push($options['systems'], 4);
Or the shorthand version:
$options['systems'][] = 4;
You can use php array_push function. Like this. array_push($options['systems'],4);
you can read the detail of array_push from below link.array_push manual

What does foo[] mean in $this->foo[] in php [duplicate]

This question already has answers here:
Reference Guide: What does this symbol mean in PHP? (PHP Syntax)
(24 answers)
Closed 6 years ago.
Is foo[] an array?
In the following code is an array element being assigned to foo[]? And is there an array key that is automatically associated with it?
$this->foo[] = 'hello world';
This is adding an element to the end of the foo array.
It's the same as using array_push().

Get specifics value from chain in PHP [duplicate]

This question already has answers here:
Parse query string into an array
(12 answers)
Closed 8 years ago.
My chain in PHP is like the following:
$chain = "m=toto&i=12&a=new";
How to get m, i and a values ?
Thanks.
Try This:
<?php
$chain = "m=toto&i=12&a=new";
parse_str($chain,$array);
This will create an array named $array containing all values you can access them as $array['m']
You can Print all this by:
print_r($array);

How to split array values to new array? [duplicate]

This question already has answers here:
How to "flatten" a multi-dimensional array to simple one in PHP? [duplicate]
(23 answers)
Closed 9 years ago.
how to split received values of an array into a new array?
so from this:
[["+","+","+"],["-","+","+"],["*","+","+"],["\/","+","+"],
to this:
["+"],["+"],["+"],["-"],["+"],["+"],["*"],["+"],["+"],
can someone help me?
Flatten your array by looping through it
$aFlattened = array();
foreach($aOriginalArray AS $aOperators){
$aFlattened = array_merge($aFlattened, $aOperators);
}

Categories