$list[7362][0]['value'] = 'apple';
$list[7362][1]['value'] = 'orange';
$list[9215][0]['value'] = 'lemon';
I want key for value 'orange'. I tried with array_search and array_column, but obviously I have issue array_column.
$key = array_search('orange', array_column($list, 'value'));
as described
PHP multidimensional array search by value
but my case is slighly different. Key should return 7362.
You can try something like this:
<?php
$list = array();
$list[7362][0]['value'] = 'apple';
$list[7362][1]['value'] = 'orange';
$list[9215][0]['value'] = 'lemon';
foreach ($list as $keynum=>$keyarr) {
foreach ($keyarr as $key=>$index) {
if (array_search('orange', $index) !== false) {
echo "orange found in $key >> $keynum";
}
}
}
?>
You can choose to just echo out echo $keynum; for your purpose.
Loop through the arrays and find out where you find orange.
You can refactor that a bit into a function like this:
<?php
function getKeys($list, $text) {
foreach ($list as $keynum=>$keyarr) {
foreach ($keyarr as $key=>$index) {
if (array_search($text, $index) !== false) {
return "$text found in $key >> $keynum";
}
}
}
return "not found";
}
$list = array();
$list[7362][0]['value'] = 'apple';
$list[7362][1]['value'] = 'orange';
$list[9215][0]['value'] = 'lemon';
echo getKeys($list, 'lemon');
?>
echo getKeys($list, 'lemon'); will give you lemon found in 0 >> 9215.
echo getKeys($list, 'orange'); will give you orange found in 1 >> 7362.
echo getKeys($list, 'apple'); will give you apple found in 0 >> 7362.
It's nested to far for the array_column at that level, so just loop:
foreach($list as $k => $v) {
if(in_array('orange', array_column($v, 'value'))) {
$key = $k;
break;
}
}
If there can be more than one then create an array and don't break:
$key[] = $k;
//break;
Related
I have two array i.e $arr1 and $arr2 where I want to find missing value of $arr1 which is not present in $arr2 without using function like array_diff(), count(), explode(), implode() etc. So, How can I do this? Please help me.
code:
<?php
$arr1 = array('2','3','4','5');
$arr2 = array('1','6','7','8');
$array = array_diff($arr1,$arr2);
print_r($arr2);
?>
First approach:-
$missingValuesArray = array();
foreach($arr1 as $arr){
if(!in_array($arr,$arr2)){
$missingValuesArray[] = $arr;
}
}
print_r($missingValuesArray);
Output:- https://3v4l.org/UBS9G
Second approach:-
$missingValuesArray = array();
foreach($arr1 as $arr){
$counter = 0;
foreach($arr2 as $ar){
if($arr != $ar){
$counter++;
}
}
if($counter == sizeof($arr2)){
$missingValuesArray[] = $arr;
}
}
print_r($missingValuesArray);
Output:- https://3v4l.org/Uu6Ob
Requirement can be achieved by :
$arr1 = array('2','3','4','5');
$arr2 = array('1','6','7','8');
$diff = array();
$diff = $arr1;
$arrayDiff = array();
foreach($arr1 AS $value) {
foreach($arr2 AS $val) {
if ($value == $val) {
$arrayDiff[] = $value;
continue;
}
}
}
foreach ($arrayDiff AS $k=>$v) {
if (($key = array_search($v, $diff)) !== false) {
unset($diff[$key]);
}
}
print_r($diff);
I am trying to expand/multiply an associative array N times and get all possible key combinations.
To do it manually for two times, I would do this:
$copy = $array;
foreach ($array as $key1=>$tmp1) {
foreach ($copy as $key2=>$tmp2) {
$combos[] = array($key1,$key2);
}
}
for expanding three times:
$copy = $copy2 = $arr1;
foreach ($arr1 as $key1=>$qd1) {
foreach ($copy as $key2=>$qd2) {
foreach ($copy2 as $key3=>$qd3) {
$combos[] = array($key1,$key2,$key3);
}
}
}
how would one do this for n-times? $combos should have n-elements for each element as shown.
I looked at the other questions, but it is not quite the same here.
Finally got it:
$array = ['1' => [3, 4], '2' => [5, 6]];
$depth = 2;
function florg ($n, $elems) {
if ($n > 0) {
$tmp_set = array();
$res = florg($n-1, $elems);
foreach ($res as $ce) {
foreach ($elems as $e) {
array_push($tmp_set, $ce . $e);
}
}
return $tmp_set;
}
else {
return array('');
}
}
$output = florg($depth, array_keys($array));
What I did is basically extract the first level keys with array_keys and then created all possibles combinations thanks to https://stackoverflow.com/a/19067650/4585634.
Here's a working demo: http://sandbox.onlinephpfunctions.com/code/94c74e7e275118cf7c7f2b7fa018635773482fd5
Edit: didn't see David Winder comment, but that's basically the idea.
$array = ['coke.','fanta.','chocolate.'];
foreach ($array as $key => $value) {
if (strlen($value)<6) {
$new[] = $value." ".$array[$key+1];
} else {
$new[] = $value;
}
}
This code doesn't have the desired effect, in fact it doesn't work at all. What I want to do is if an array element has string length less than 5, join it with the next element. So in this case the array should turn into this:
$array = ['coke. fanta.','chocolate.'];
$array = ['coke.','fanta.','chocolate.', 'candy'];
$new = [];
reset($array); // ensure internal pointer is at start
do{
$val = current($array); // capture current value
if(strlen($val)>=6):
$new[] = $val; // long string; add to $new
// short string. Concatenate with next value
// (note this moves array pointer forward)
else:
$nextVal = next($array) ? : '';
$new[] = trim($val . ' ' . $nextVal);
endif;
}while(next($array));
print_r($new); // what you want
Live demo
With array_reduce:
$array = ['coke.', 'fanta.', 'chocolate.', 'a.', 'b.', 'c.', 'd.'];
$result = array_reduce($array, function($c, $i) {
if ( strlen(end($c)) < 6 )
$c[key($c)] .= empty(current($c)) ? $i : " $i";
else
$c[] = $i;
return $c;
}, ['']);
print_r($result);
demo
<pre>
$array = ['coke.','fanta.','chocolate.'];
print_r($array);
echo "<pre>";
$next_merge = "";
foreach ($array as $key => $value) {
if($next_merge == $value){
continue;
}
if (strlen($value)<6) {
$new[] = $value." ".$array[$key+1];
$next_merge = $array[$key+1];
} else {
$new[] = $value;
}
}
print_r($new);
</pre>
Updated Code after adding pop after chocolate.
<pre>
$array = ['coke.','fanta.','chocolate.','pop'];
print_r($array);
echo "<br>";
$next_merge = "";
foreach ($array as $key => $value) {
if($next_merge == $value){
continue;
}
if (strlen($value)<6 && !empty($array[$key+1])) {
$new[] = $value." ".$array[$key+1];
$next_merge = $array[$key+1];
} else {
$new[] = $value;
}
}
print_r($new);
<pre>
You need to skip the iteration for the values that you have already added.
$array = ['coke.', 'fanta.', 'chocolate.'];
$cont = false;
foreach ($array as $key => $value) {
if ($cont) {
$cont = false;
continue;
}
if (strlen($value) < 6 && isset($array[$key+1])) {
$new[] = $value.' '.$array[$key+1];
$cont = true;
}
else {
$new[] = $value;
}
}
print_r($new);
hi i have an array of about 20/30 items big.
i need to have it loop threw the array and echo out only the items with the text p1 in them.
the array looks like so
"lolly","lollyp1","top","topp1","bum","bump1","gee","geep1"
and so on
i have tried to use something like this
foreach ($arr as $value) {
$needle = htmlspecialchars($_GET["usr"]);
$ret = array_keys(array_filter($arr, function($var) use ($needle){
return strpos($var, $needle) !== false;
}));
but all this gives me is a blank page or 1s
how can i have it echo out the items with p1 in them ?
Try This:
$needle = htmlspecialchars($_GET["usr"]);
$rtnArray = array();
foreach ($arr as $value) {
$rtnArray = strpos($value,$needle);
};
return $rtnArray;
If your trying to write directly to the page the lose the $rtnarray and echo:
$needle = htmlspecialchars($_GET["usr"]);
foreach ($arr as $value) {
echo strpos($value,$needle);
};
To only show ones with 'p1' then filter:
$needle = htmlspecialchars($_GET["usr"]);
foreach ($arr as $value) {
$temp = strpos($value,$needle);
if($temp > 1){
echo $value;
}
};
Using a direct loop with string-comparison would be a simple way to go here:
$needle = $_GET['usr'];
$matches = array();
foreach ($arr as $key => $value) {
if (strpos($value, $needle) !== false) {
$matches[] = $key;
}
}
The use of array_filter() in your post should work, pending the version of PHP you're using. Try updating to use a separate / defined function:
function find_needle($var) {
global $needle;
return strpos($var, $needle) !== false;
}
$ret = array_keys(array_filter($arr, 'find_needle'));
Codepad Example of the second sample
I have an array in PHP and would like to use foreach to process entries skipping [0], to process [1], [2], etc.
Thank you
you can use array_slice
$array = array(1,2,3);
foreach (array_slice($array,1) as $value ) {
echo $value;
}
If you don't mind losing first element you can use array_shift
array_shift($array);
foreach ( $array as $value ) {
echo $value;
}
Output
23
$i = 0;
foreach ($ar as $value) {
if ($i > 0) {
// code here
}
$i++;
}
You can keep a variable for this:
$firstSkipped = false;
foreach ($arr as $value) {
if (!$firstSkipped) {
$firstSkipped = true;
continue;
}
// code here
}
Or you could just use a regular for loop, setting the beginning counter to 1:
for ($i = 1, $count = count($arr); $i < $count; $i++) {
// code here
}
You can remove the first entry from an array with array_shift.
$array = array("a","b","c");
array_shift($array);
foreach ($array as $values)
{
echo $values; //bc
}
Try this:
$arr = array(0,1,2,3,4,5);
unset($arr[0]);
foreach($arr as $value) {
echo $value;
echo "<br />";
}
This would delete first entry from array, so it would not skip as you asked, but anyway you can try this...