Remove whitespace or something from array php - php

Try to remove these whitespace array form this
Im also trying to remove whitespace with preg_replace, explode, trim
`Array
(
[0] => s
[1] =>
[2] => s
[3] =>
[4] => a
[5] =>
[6] => i
[7] =>
[8] => 2
[9] =>
[10] => 2
)`

You can use array_filter to remove empty array elements
$arr = array('s','','s','','a','i','',2,'',2);
$arr = array_filter($arr);
echo "<pre>";
print_r( $arr );
echo "</pre>";
This will result to:
Array
(
[0] => s
[2] => s
[4] => a
[5] => i
[7] => 2
[9] => 2
)
Or you can use trim as callback if there are multiples spaces. Like:
$arr = array('s',' ','s',' ','a','i',' ',2,'',2);
$arr = array_filter($arr,'trim');
echo "<pre>";
print_r( $arr );
echo "</pre>";
Will get the same result.
Doc: array_filter

Related

Build array by string name OR make multiple array by string values

I want to build a array or multiple array by breaking the main array , and my array is like ,
Array
(
[0] => string1
[1] => 1
[2] => 2
[3] => 3
[4] => 66
[5] => 34
[6] => string1
[7] => aww
[8] => brr
[9] => string3
[10] => xas
)
So basically by the value 'string1' i want to make a new array or first array which has only those three values (1,2,3) and same for string2 and string3,So each array has its values(three).
Please help me to build this.
Note: those all string names will be static.
Thank you in advance.
Result should me like:
string1 array:
<pre>Array
(
[1] => 1
[2] => 2
[3] => 3
[4] => 66
[5] => 34
)
string2 array:
<pre>Array
(
[1] => aww
[2] => brr
)
string3 array:
<pre>Array
(
[1] => xas
)
This I think will get you what you want.
It does assume that the first entry in the old array will be a keyword!
$old = array('string1',1,2,3,66,34,'string2','aww','brr','string3','xas');
$new = array();
$keywords = array('string1', 'string2', 'string3');
$last_keyword = '';
foreach ($old as $o) {
if ( in_array($o, $keywords) ) {
$last_keyword = $o;
} else {
$new[$last_keyword][] = $o;
}
}
print_r($new);
It creates a new array like this
Array
(
[string1] => Array
(
[0] => 1
[1] => 2
[2] => 3
[3] => 66
[4] => 34
)
[string2] => Array
(
[0] => aww
[1] => brr
)
[string3] => Array
(
[0] => xas
)
)
However I still maintain that it would be better to go back to where the original array gets created and look to amend that process rather than write a fixup for it

Delete empty value from array php

I have this array, and I need to delete the empty value and just keep the other values.
Array
(
[12] => Array
(
[0] => 12
[1] => Philippines
[2] => 94,013,200
[3] => Mid-2010
[4] => 0.0136
)
[13] => Array
(
[0] =>
[1] =>
[2] =>
[3] =>
[4] =>
)
You can use array_map and array_filter functions for removing empty values from multi-dimensional array.
Solution:
$array = array_filter(array_map('array_filter', $yourArr));
Example:
$yourArr[12] = array('12','Philippines');
$yourArr[13] = array('','');
$array = array_filter(array_map('array_filter', $yourArr));
echo "<pre>";
print_r($array);
Result:
Array
(
[12] => Array
(
[0] => 12
[1] => Philippines
)
)
Use array_map() and array_filter()
$result = array_map('array_filter', $a)
array_filter() removes blank elements from array in this case.
array_map() function calls a function on every array element, in this cause, it calls array_filter() and removes empty elements.
Working Code:
<?php
$a = array(12 => array(12, 'Philippines', '94,013,200', 'Mid-2010', '0.0136'), 13 => array('', '', '', '', ''));
$result = array_map('array_filter', $a);
echo "<pre>";
print_r($result);
echo "</pre>";
?>

preg_match_all and umlets

I am using preg_match_all to filter out strings
The string which I have supplied in preg_match_all is
$text = "Friedric'h Wöhler"
after that I use
preg_match_all('/(\"[^"]+\"|[\\p{L}\\p{N}\\*\\-\\.\\?]+)/', $text, $arr, PREG_PATTERN_ORDER);
and the result i get when I print $arr is
Array
(
[0] => Array
(
[0] => friedric
[1] => h
[2] => w
[3] => ouml
[4] => hler
)
[1] => Array
(
[0] => friedric
[1] => h
[2] => w
[3] => ouml
[4] => hler
)
)
Somehow the ö character is replaced by ouml which I am not really sure how to figure this out
I am expecting following result
Array
(
[0] => Array
(
[0] => Friedric'h
[1] => Wöhler
)
)
Per nhahtdh's comment:
$text = "Friedric'h Wöhler";
preg_match_all('/"[^"]+"|[\p{L}\p{N}*.?\\\'-]+/u', $text, $arr, PREG_PATTERN_ORDER);
echo "<pre>";
print_r($arr);
echo "</pre>";
Gives
Array
(
[0] => Array
(
[0] => Friedric'h
[1] => Wöhler
)
)
If you think preg_match_all() is messy, you could take a look at pattern():
$p = '"[^"]+"|[\p{L}\p{N}*.?\\\'-]+'; // automatic delimiters
$text = "Friedric'h Wöhler";
$result = pattern($p)->match($text)->all();

Match array elements beginning with letters specified in another array in PHP

I have the following array:
Array
(
[0] => BCD
[1] => ACE
[2] => AHP
[3] => BGH
[4] => ART
[5] => COT
[6] => ARG
[7] => BGT
)
I need to match all elements whose first letter is in the following array:
Array
(
[0] => B
[1] => A
)
to get:
Array
(
[0] => ACE
[1] => AHP
[2] => BGH
[3] => ART
[4] => ARG
[5] => BGT
)
Short of looping through the whole array, how do I do this in PHP? Is there a built-in PHP array function for this or a combination of so? The order does not matter for both keys and values of the resulting array. Thanks much.
You can use array_filter for these operations:
$array = array('CBD', 'NHN', 'NHP', 'WHC', 'NND', 'CQN', 'WST', 'WVT');
$whitelist = array('W', 'N');
$filtered = array_filter($array, function($val) use ($whitelist) {
// check if first letter is in the whitelist array
if (in_array($val{0}, $whitelist)) {
return $val;
}
return false;
});
Output:
Array
(
[1] => NHN
[2] => NHP
[3] => WHC
[4] => NND
[6] => WST
[7] => WVT
)

build multidimensional array from string php

EDIT: Here is a portion of $preparedstring:
555555,Jones,Brian,NYC,1000,2011-10-21 00:00:00,Check,1542,0, ,Check, ,0, ,Check, ,; 6666666,Miler,Christopher,Chicago,1000,2011-10-26 00:00:00,Check,6406,0, ,Check, ,0, ,Check, ,;
I am trying to convert a HTML table to a multidimensional array. I have converted the table into a long string, each cell being delimited with a comma and each row being delimited with a semicolon.
I am not exactly sure how to build the multidimensional array from this string. This is what I have tried so far:
<?php
$outerARR = explode(";", $preparedstring);
$arr = array
(
foreach ($outerARR as $arrvalue) {
$innerarr = explode(",", $arrvalue);
$innerarr[0]=>array
(
$innerarr[];
)
}
);
?>
this gives me a syntax error near the
$arr = array
(
opening parenthesis.
Your approach to solving the problem is sadly very wrong, though there are many solutions to your problem, I would use something like the below.
How does the code work?
First we use explode to split our string up in smaller chunks, ; is our delimiter.
We pass this newly created array to array_map as it's second parameter.
array_map takes two parameters, the first one is a function that will be called for every member of the second paramater (which should be an array).
Inside our callback to array_map we use explode to once again split out the values, now with , as our delimiter.
$data = "1,2,3;4,5,6;7,8,9";
$ret = array_map (
function ($_) {return explode (',', $_);},
explode (';', $data)
);
print_r ($ret);
output
Array
(
[0] => Array
(
[0] => 1
[1] => 2
[2] => 3
)
[1] => Array
(
[0] => 4
[1] => 5
[2] => 6
)
[2] => Array
(
[0] => 7
[1] => 8
[2] => 9
)
)
It doesn't work, why?
Probably because you are using a version of PHP prior to 5.3, if so you can use this snippet instead:
function explode_by_comma ($_) {
return explode (',', $_);
}
$ret = array_map ('explode_by_comma', explode (';', $data));
<?php
//explode first dimension of the array to create an array of rows
$outerARR = explode(";", $preparedstring);
$arr = array();
//iterate through the newly created array
foreach ($outerARR as $arrvalue) {
//explode this row into columns
$innerarr = explode(",", $arrvalue);
//add the newly created array of columns to the output array as a new index
$arr[] = $innerarr;
}
?>
You're close, but arrays don't work that way. You can't put a foreach inside an array constructor like that. It should look like this:
$outerARR = explode(";", $preparedstring);
$arr = array();
foreach ($outerARR as $arrvalue){
$innerarr = explode(",", $arrvalue);
$arr[] = $innerarr;
}
Demo: http://codepad.org/I5wFFczR
$outerARR = explode(";", $preparedstring);
$a = array();
$y=0;
foreach ($outerARR as $arrvalue){
$x=0;
$innerarr = explode(",", $arrvalue);
foreach($innerarr as $v){
$a[$y][$x++] = $v;
}
$y++;
}
print_r($a);
Array
(
[0] => Array
(
[0] => 555555
[1] => Jones
[2] => Brian
[3] => NYC
[4] => 1000
[5] => 2011-10-21 00:00:00
[6] => Check
[7] => 1542
[8] => 0
[9] =>
[10] => Check
[11] =>
[12] => 0
[13] =>
[14] => Check
[15] =>
[16] =>
)
[1] => Array
(
[0] => 6666666
[1] => Miler
[2] => Christopher
[3] => Chicago
[4] => 1000
[5] => 2011-10-26 00:00:00
[6] => Check
[7] => 6406
[8] => 0
[9] =>
[10] => Check
[11] =>
[12] => 0
[13] =>
[14] => Check
[15] =>
[16] =>
)
)

Categories