How do you replace all non matches from one array that are not defined within the other array, i have kind of got working but its not exactly right. as i will show you.
the result is, but wrong.
- - £ 8 - - - - - - - -
The required result should be
£ 8 - -
this is how my code is
$vals_to_keep = array(8, 'y', '£');
$replace_if_not_found = array('£', 8, '#', 't'); // replace if not in above array
$result = '';
foreach ($replace_if_not_found as $d) {
foreach ($vals_to_keep as $ok) {
if(strcmp($d, $ok) == 0){
$result .= $d . " ";
}else
$result .= str_replace($d, $ok ,'-') . " ";
}
}
echo $result;
use in_array http://php.net/manual/en/function.in-array.php
foreach ($replace_if_not_found as $d) {
if (in_array($d, $vals_to_keep))
$result .= $d . " ";
else
$result .= str_replace($d, $ok ,'-') . " ";
}
You could loop over all of the items in $replace_if_not_found replacing them with -, or not, as appropriate.
Using closure in PHP 5.3 or above
$result = array_map(function($item) use ($vals_to_keep) {
return in_array($item, $vals_to_keep, TRUE) ? $item : '-';
}, $replace_if_not_found);
echo implode(' ', $result);
Using a foreach loop
$result = array();
foreach ($replace_if_not_found as $item) {
if (in_array($item, $vals_to_keep, TRUE)) {
$result[] = $item;
} else {
$result[] = '-';
}
}
echo implode(' ', $result;
Related
I have to check a array of string data .
For example this is the string :
$keywords="today, tomorroy, world, is a good day , a good day is, today"
I have to check if is single word or multi word.
If is multi word i have to order and leave only one for example : is a good day ; a good day is i have to leave only : is a good day . This words i have to store in other array.
In the end i have to have only this results:
This is my code:
$keywords = "";
$singleword="";
$multiword="";
foreach ($response->getResults() as $result) {
$keywords .= $result->getText()->getValue() . ",";
if(count(explode(' ', $keywords)) > 1) {
$multiword++;
}
$singleword++;
}
return $keywordsgenerated;
I need to return : today, tomorroy, world, is a good day
Please can you help me to fix , i'm new in php.
EDIT
I think you were looking for something like this:
<?php
$keywords="today, tomorrow, world, is a good day , a good day is, today";
$arr = array_unique(explode(",",$keywords));
$words = [];
foreach($arr as $key=>$a) {
$words_explode = explode(" ", $a);
foreach($words_explode as $w) {
$words[] = $w;
}
}
$new_words = array_unique($words);
$result = implode(" ", $new_words);
foreach($arr as $item) {
$result = str_replace($item,$item.",",$result);
}
$result = trim($result,", ");
var_dump($result);
UPDATE:
I think? I got what you were looking for?
$keywords = "hansgrohe,hansgrohe focus,küchenarmatur,hansgrohe metris";
$arr = explode(',', $keywords);
$already = [];
$result = '';
foreach($arr as $word) {
$subword = explode(" ", $word);
foreach($subword as $actual_word) {
if (!in_array($actual_word, $already)) {
$result .= $actual_word . ' ';
$already[] = $actual_word;
}
}
}
$result = rtrim($result);
//would result in hansgrohe focus küchenarmatur metris
echo $result;
UPDATE2:
If you want to know number of occurences of each word.
<?php
$keywords = "hansgrohe,hansgrohe focus,küchenarmatur,hansgrohe metris";
$arr = explode(",", $keywords);
$already = [];
$nrwords = [];
$result = '';
foreach($arr as $word) {
$subword = explode(" ", $word);
foreach($subword as $actual_word) {
if (!in_array($actual_word, $already)) {
$result .= $actual_word . ' ';
$already[] = $actual_word;
$nrwords[$actual_word] = 0;
}
$nrwords[$actual_word]++;
}
}
$result = rtrim($result);
//would result in hansgrohe focus küchenarmatur metris
echo $result;
//would show how many of each word that exists
echo '<pre>';
print_r($nrwords);
echo '</pre>';
I want to cut text in array but I have no idea to cut this
I try strstr() but it not true.
I try
$ff='';
while($row = mysqli_fetch_array($result,MYSQLI_ASSOC)){
$ff .= $row['fav'] . ",";
}
if( strpos( $ff, "_" )) {
$text = strstr($ff, '_');
echo $text;
}
$ff ='A_0089,A_5677,B_4387,A_B_5566,'
I want output show
0089,5677,4387,B_5566,
Here is one example, using substr() with strpos():
$ff='A_0089,A_5677,B_4387,A_B_5566';
$items = explode(',', $ff);
foreach($items as $item) {
echo substr($item, strpos($item, '_')) . "\n";
}
The above code returns:
_0089
_5677
_4387
_B_5566
You're better off not building a string, but building an array. The way you build the string you have a dangling comma, which you do not want.
$ff = array();
while($row = mysqli_fetch_array($result,MYSQLI_ASSOC)){
$ff[] = $row['fav'];
}
foreach($ff as $item) {
echo substr($item, strpos($item, '_')) . "\n";
}
Based on your desire to keep the commas and create a string:
$ff='A_0089,A_5677,B_4387,A_B_5566,';
$items = explode(',', $ff);
foreach($items as $item) {
$new[] = substr($item, strpos($item, '_'));
}
$newFF = implode(',', $new);
echo $newFF;
returns:
_0089,_5677,_4387,_B_5566,
Probably this is what you are looking for
<?php
function test_alter(&$item1)
{
$pattern = '/^[A-Z]{1}[_]{1}/';
$item1 =preg_replace($pattern,"",$item1);
}
$ff="A_0089,A_5677,B_4387,A_B_5566,";
$nff=explode(",",$ff);
array_walk($nff, 'test_alter');
echo implode(",",$nff);
?>
I am adding the names of regions to a a variable using the below code (shortened). Everything works as intended, except for the sort function which throws an error saying that it requires an array instead of a string.
How can I still manage to sort the content of my variable alphabetically ?
$regions = '';
$countR = 1;
foreach ($objR->days as $days) {
if($days->dateMatch == "Yes" && !empty($days->regions)) {
foreach(explode(',', $days->regions) as $r){
$regions .= str_replace(" / ", ", ", $r)) . "<br />";
$countR++;
}
}
}
sort($regions);
Try this:
You should use array for storage.
$regions = array();
$countR = 1;
foreach ($objR->days as $days) {
if($days->dateMatch == "Yes" && !empty($days->regions)) {
foreach(explode(',', $days->regions) as $r){
$region = str_replace(" / ", ", ", $r)) . "<br />";
array_push($regions,$region);
$countR++;
}
}
}
sort($regions);
I have two function and need to merge the string that it returned if possible.
Here's my functions:
function qty($numbers) {
$numbers = array($this->strips_qty, $this->lancet_qty, $this->battery_qty,
$this->misc_qty);
function qty($numbers) {
foreach ($numbers as $stock) {
if ($stock)
$result .= $stock . '<br>';
}
return $result;
}
if ($result = qty($numbers))
return '(' . $result . ')';
}
function parts_requested($params) {
$params = array($this->meter, $this->strips, $this->lancets, $this->control,
$this->ld, $this->battery, $this->misc);
function parts_requested($params) {
foreach ($params as $part) {
if ($part)
$result .= $part . ',';
}
return $result;
}
if ($result = parts_requested($params))
return 'OK TO SEND: ' . $result;
}
where it says OK TO SEND... i would like to merge the result from the qty() into the return on the parts_requested() right after the words OK TO SEND.
Any help would be greatly appreciated. Thanks in advance.
I don't know if understand it well but i'll do it like this:
return 'OK TO SEND: ' . $result . qty($numbers);
Or you can do it with an array and the merge it outside:
$result = array();
$result[]= parts_requested($params);
$result[]= qty($numbers);
echo implode($result,'');
Updated answer as requested
Change the definition of the second function as:
function parts_requested($params, $numbers) {
Then at part_requested change the line where you add the comma to:
`$result .= qty($numbers) . $part . ',';`
But i don't see where numbers is being updated every time the loop repeats.
There are better solutions to what you're doing, but try something like this:
// example outputs
$parts = 'OK TO SEND: 10CTNOVAMAX,743014';
$quantities = '5<br>6<br>';
$parts = array_split($parts, ': ');
$parts = array_split($parts[1], ',');
$quantities = array_split($quantities, '<br>');
$result = 'OK TO SEND: ';
foreach($parts as $key->$value) {
$result .= '('.$quantities[$key].') '.$value;
}
Not sure I fully understood the question and why you have repeating functions, but it seems as simple as:
function qty($numbers) {
$numbers = array($this->strips_qty, $this->lancet_qty, $this->battery_qty,
$this->misc_qty);
$result = '';
function qty($numbers) {
foreach ($numbers as $stock) {
if ($stock)
$result .= $stock . '<br>';
}
return $result;
}
if ($result = qty($numbers))
return '(' . $result . ')';
}
function parts_requested($params, $numbers) {
$params = array($this->meter, $this->strips, $this->lancets, $this->control,
$this->ld, $this->battery, $this->misc);
$result = '';
function parts_requested($params) {
foreach ($params as $part) {
if ($part)
$result .= $part . ',';
}
return $result;
}
if ($result = parts_requested($params))
return 'OK TO SEND: ' . qty($numbers) . $result;
}
Note that I also had to define $result at the beginning of each function and that you'd have to call your function like this: parts_requested($params, $numbers); and that function will call the other one and insert the results.
Can someone help me with some code or instructions on how to walk recursively an array and when reaching the last element print the full path to it? A simple echo will work because I will adapt the code to some other function I'm developing.
The function doesn't need to figure the array dimension because this param will be passed:
Example:
$depth = 8;
$array[1][3][5][6][9][5][8][9];
When function reachs the 8th element it print all the path to it:
//print path
'1 -> 3 -> 5 -> 6 -> 9 -> 5 -> 8 -> 9'
As I said, only printing in this format will work cause I will implement the code into some other function.
array keys can have the same value. Obviously not the same value in the same sequence for the entire arary.
Updated:
Walk recursively function:
$someArray[1][2][3] = 'end';
$someArray[1][2][6] = 'end';
$someArray[1][3][6] = 'end';
$someArray[4][3][7] = 'end';
function listArrayRecursive(&$array_name, $ident = 0){
if (is_array($array_name)){
foreach ($array_name as $k => &$v){
if (is_array($v)){
for ($i=0; $i < $ident * 10; $i++){ echo " "; }
echo $k . " : " . "<br>";
listArrayRecursive($v, $ident + 1);
}else{
for ($i=0; $i < $ident * 10; $i++){ echo " "; }
echo $k . " : " . $v . "<br>";
}
}
}else{
echo "Variable = " . $array_name;
}
}
listArrayRecursive($someArray);
Will print:
1 :
2 :
3 : end
6 : end
3 :
6 : end
4 :
3 :
7 : end
Now, how can I also print the path of the array everytime it reaches the end? For example:
1 :
2 :
3 : end : path -> 1,2,3
6 : end : path -> 1,2,6
3 :
6 : end : path -> 1,3,6
4 :
3 :
7 : end : path -> 4,3,7
EDITED CODE ADDING A THIRD PARAM TO RECORD THE PATH:
$someArray[1][2][3] = 'end';
$someArray[1][2][6] = 'end';
$someArray[1][3][6] = 'end';
$someArray[4][3][7] = 'end';
$someArray[3][2] = 'end';
function listArrayRecursive(&$array_name, $ident = 0, $path = null){
foreach ($array_name as $k => &$v){
if (is_array($v)){
for ($i=0; $i < $ident * 10; $i++){ echo " "; }
echo $k . " : " . "<br>";
$path .= $k . ', ';
listArrayRecursive($v, $ident + 1, $path);
}else{
for ($i=0; $i < $ident * 10; $i++){ echo " "; }
echo $k . " : " . $v . ' - path -> ' . $path . "<br>";
}
}
}
listArrayRecursive($someArray);
Will print:
1 :
2 :
3 : end - path -> 1, 2,
6 : end - path -> 1, 2,
3 :
6 : end - path -> 1, 2, 3,
4 :
3 :
7 : end - path -> 1, 4, 3,
3 :
2 : end - path -> 1, 4, 3,
You could employ a RecursiveIteratorIterator (docs) to take the hard work out of recursing through the arrays.
function listArrayRecursive($someArray) {
$iterator = new RecursiveIteratorIterator(new RecursiveArrayIterator($someArray), RecursiveIteratorIterator::SELF_FIRST);
foreach ($iterator as $k => $v) {
$indent = str_repeat(' ', 10 * $iterator->getDepth());
// Not at end: show key only
if ($iterator->hasChildren()) {
echo "$indent$k :<br>";
// At end: show key, value and path
} else {
for ($p = array(), $i = 0, $z = $iterator->getDepth(); $i <= $z; $i++) {
$p[] = $iterator->getSubIterator($i)->key();
}
$path = implode(',', $p);
echo "$indent$k : $v : path -> $path<br>";
}
}
}
This example is to give you idea, not to solve the actual task.
function recursiveSearch($array,$search){
foreach($array as $key=>$val){
if($val==$search)return $key;
$x=recursiveSearch($array[$key],$search);
if($x)return $key.' -> '.$x;
}
}
echo recursiveSearch($array,'search');
If no match is found, null is returned.
$a= array(1,2,3,4,5,6);
$val = end($a);
print_array($a,$val);
function print_array(&$arr, $val)
{
if ($val === false)
return;
$curr = prev($arr);
print_array($arr,$curr);
echo $val;
}
I just wrote a function that makes recursive looping a bit easier:
Similar to array_walk_recursive but with some extra functionality
public static function walk($array, $callback, $custom = null, $recursive = false, $info = [])
{
$r = $recursive;
if (gettype($r) === 'integer') {
$r--;
}
$info['depth'] = empty($info)?1:$info['depth'] + 1;
$info['count'] = count($array);
$info['i'] = 1;
foreach($array as $k => $v) {
if (is_array($v) && $r > 0) {
$array[$k] = static::walk($v, $callback, $custom, $r, $info);
} else {
$array[$k] = $callback($v, $k, $custom, $info);
}
$info['i'] ++;
}
return $array;
}
public static function walkable($v, $k, $custom, $info)
{
if (is_string($v)) {
return $v." [ custom: {$custom['key']} ] [ level: ".$info['depth'].' | No '.$info['i'].' of '.$info['count']." ]";
}
return $v;
}
Called like so:
$result = Namespace\ClassName::walk($array, ['Namespace\ClassName', 'walkable'], ['key'=>'value'], true);
Setting recursive to false will only evaluate the first level.
Setting recursive to true will cause it to traverse the entire array.
Setting recursive to an integer will cause it to only traverse to that depth.
Walkable functions can be referenced or passed to callback as anonymous function.
(expects: value, key, custom, info)
The returned value replace the current value.
Custom data can be passed and some additional info is provided for you.
You can expand on the walk function if you need additional info.
I had similar problem. Here is a Depth-First Search-ish solution(no path depth included, it reaches until the very end of the array). Comment the 'if' statement if u don't want to include the value:
$output = array();
retrievePath($someArray, $output);
function retrievePath($someArray, array &$pathKeeper)
{
if(!is_array($someArray)){ // $someArray == "end"
$element = array_pop($pathKeeper) ?? '';// if the array is empty pop returns null, we don't want that
array_push($pathKeeper, $element . '->'. $someArray);
} else{
end($someArray);//we want to get the last element from the array so we move the internal pointer to it's end
$endElKey = key($someArray);//take the key where the pointer is
reset($someArray);
foreach($someArray as $key=>$value){
$element = array_pop($pathKeeper);
array_push($pathKeeper, $element === null ? $key : $element . '->' . $key);// we don't want '->' at the beginning
retrievePath($value, $pathKeeper);
if($key != $endElKey) //we check whether this is not the last loop
array_push($pathKeeper, $element);
}
}
}
<?php
function printListRecursive($a, $var='', $i = 0) {
if (!is_array($a)) {
$var .= $a;
return $var;
}
$string = "";
foreach ($a as $k => $value) {
$string .= str_repeat(" ", $i) .' - '. $k . ':';
if (!is_array($value)) {
$string .= $value . '<br />';
} else {
$string .= '<br />';
$string .= printListRecursive($value, $var, $i + 1);
}
}
return $string;
}
$test_array = [
'America' => [
'Argentina' => 'Buenos Aires',
'Peru' => 'Lima'
],
'Europe' => [
'Ireland' => 'Dublin',
'France' => 'Paris',
'Italy' => 'Rome'
]
];
$result = printListRecursive($test_array);
echo $result;
?>
Check code here
I came up with the following function based on #salathe's one. It returns an array where each element is an array containing the leaf at index 0 and the array of the path keys at index 1:
function loosenMultiDimensionalArrayPathForEachVal($array) {
$iterator = new \RecursiveIteratorIterator(new \RecursiveArrayIterator($array), \RecursiveIteratorIterator::SELF_FIRST);
$iterator->rewind();
$res = [];
foreach ($iterator as $v) {
$depth = $iterator->getDepth();
for ($path = array(), $i = 0, $z = $depth; $i <= $z; $i++) {
$path[] = $iterator->getSubIterator($i)->key();
}
$leaf = $array;
foreach ($path as $pathKey) {
$leaf = $leaf[$pathKey];
}
if (!is_array($leaf)) {
$res[] = [
$v,
$path
];
}
}
return $res;
}
The main reason I implemented this one is that $iterator->hasChildren() returns true if the current iterated leaf is an object. Therefore, I wouldn't be able to get the path of it that way.
I found this solution, which also keeps into account if elements of the structure are arrays:
$file_contents=file_get_contents("data.json");
$json_dump=json_decode($file_contents);
printPath($json_dump, '', "" ,"");
function printPath($the_array, $path, $prevType) {
// Parse all elements of a structure
// and print full PHP path to each one.
foreach($the_array as $key => $value) {
if(is_array($value)) {
// Array element cannot be directly printed: process its items as objects:
printPath($value, $path . $key , "array");
} else {
if (!is_object($value)) { // If the element is not an object, it can be printed (it's a leaf)
if(is_string($value)) {
$finalValue = '"' . $value . '"';
} else {
$finalValue = $value;
}
if($prevType == "array") {
// If array element, add index in square brackets
echo($path . "[" . $key . "] = " . $finalValue . "<br>");
} else {
echo($path . $key . " = " . $finalValue . "<br>");
}
} else { // else store partial path and iterate:
if($prevType == "array") {
// Path of array element must contain element index:
printPath($value, $path . "[" . $key . "]->" , "dummy");
} else {
printPath($value, $path . $key . "->", "dummy");
}
}
}
}
}
Example output:
status->connections->DSS-25->band = "X"
status->connections->DSS-25->endAt = "2019-11-20T20:40:00.000Z"
status->connections->DSS-25->startAt = "2019-11-20T12:40:00.000Z"
geometry[0]->obs[0]->name = "UDSC64"
geometry[0]->obs[0]->hayabusa2->azm = 90.34
geometry[0]->obs[0]->hayabusa2->alt = -20.51
In case anybody is interested, here it is the port to Javascript:
function iterate(obj, stack, prevType) {
for (var property in obj) {
if ( Array.isArray(obj[property]) ) {
//console.log(property , "(L=" + obj[property].length + ") is an array with parent ", prevType, stack);
iterate(obj[property], stack + property , "array");
} else {
if ((typeof obj[property] != "string") && (typeof obj[property] != "number")) {
if(prevType == "array") {
//console.log(stack + "[" + property + "] is an object, item of " , prevType, stack);
iterate(obj[property], stack + "[" +property + "]." , "object");
} else {
//console.log(stack + property , "is " , typeof obj[property] , " with parent ", prevType, stack );
iterate(obj[property], stack + property + ".", "object");
}
} else {
if(prevType == "array") {
console.log(stack + "[" + property + "] = "+ obj[property]);
} else {
console.log(stack + property , " = " , obj[property] );
}
}
}
}
}
iterate(object, '', "File")
You can add a third parameter which holds the actual path as String. At the end you can output it then.