PHP Search Array in Array

PHP search array in array

with foreach ?

function searchMyCoolArray($arrays, $key, $search) {
$count = 0;

foreach($arrays as $object) {
if(is_object($object)) {
$object = get_object_vars($object);
}

if(array_key_exists($key, $object) && $object[$key] == $search) $count++;
}

return $count;
}

echo searchMyCoolArray($input, 'info_type_id', 4);

Filter 2d array to keep all rows which contain a specific value

You could loop then use array_search()

$array = array(...); // Your array
$x = 10;

foreach ($array as $key => $value) {
if (array_search($x, $value)) {
echo 'Found on Index ' . $key . '</br>';
}
}

Or if you need the arrays with those index

$array = array(...); // Your array
$x = 10;
$result = array(); // initialize results

foreach ($array as $key => $value) {
if (array_search($x, $value)) {
$result[] = $array[$key]; // push to result if found
}
}

print_r($result);

Get key from first array on Search Array in Array PHP

  • You can use in_array to search for the specified serial.
  • I renamed some of your variables to make it clearer.
  • Used break to break out of the loop once we know we've found what we're looking for.
  • assuming you define $serial above your foreach

http://sandbox.onlinephpfunctions.com/code/ca34f236ca2d19af8c098120de1707f14bed285a

<?php

$groep = array(
1 => array("111","222"),
2 => array("333","444")
);

$groepje = 0;
$serial= '333';

foreach($groep as $key => $data) {

if (in_array($serial, $data)) {
$groepje = $key;
break;
}
}

// 2
echo $groepje;

Most efficient way to search for object in an array by a specific property's value

You can iterate that objects:

function findObjectById($id){
$array = array( /* your array of objects */ );

foreach ( $array as $element ) {
if ( $id == $element->id ) {
return $element;
}
}

return false;
}

Edit:

Faster way is to have an array with keys equals to objects' ids (if unique);

Then you can build your function as follow:

function findObjectById($id){
$array = array( /* your array of objects with ids as keys */ );

if ( isset( $array[$id] ) ) {
return $array[$id];
}

return false;
}

PHP multidimensional array search by value

function searchForId($id, $array) {
foreach ($array as $key => $val) {
if ($val['uid'] === $id) {
return $key;
}
}
return null;
}

This will work. You should call it like this:

$id = searchForId('100', $userdb);

It is important to know that if you are using === operator compared types have to be exactly same, in this example you have to search string or just use == instead ===.

Based on angoru answer. In later versions of PHP (>= 5.5.0) you can use one-liner.

$key = array_search('100', array_column($userdb, 'uid'));

Here is documentation: http://php.net/manual/en/function.array-column.php.

php - deep search array of arrays and return only matching elements

Try this code.

function getParentStackComplete( $search, $stack ){

$results = array();

foreach( $stack as $item ){

if( is_array( $item ) ){

if( array_filter($item, function($var) use ($search) { return ( !is_array( $var ) )? stristr( $var, $search ): false; } ) ){
//echo 'test';
$results[] = $item;
continue;
}else if( array_key_exists('asset_info', $item) ){
$find_assets = array();
foreach( $item['asset_info'] as $k=>$v ){
//echo 'abc ';

if( is_array( $v ) && array_filter($v, function($var) use ($search) { return stristr($var, $search); }) ){
$find_assets[] = $v;
}
}
if( count( $find_assets ) ){
$temp = $item;
$temp['asset_info'] = $find_assets;
$results[] = $temp;
}
}
}
}

return $results;
}

PHP Search Array column for match

Since you have an nested Array you need two iterations:

$filtered = array();
$rows = Your Array;
foreach($rows as $index => $columns) {
foreach($columns as $key => $value) {
if ($key == 'id' && $value == '1') {
$filtered[] = $columns;
}
}
}

This should do the job.

in_array() and multidimensional array

in_array() does not work on multidimensional arrays. You could write a recursive function to do that for you:

function in_array_r($needle, $haystack, $strict = false) {
foreach ($haystack as $item) {
if (($strict ? $item === $needle : $item == $needle) || (is_array($item) && in_array_r($needle, $item, $strict))) {
return true;
}
}

return false;
}

Usage:

$b = array(array("Mac", "NT"), array("Irix", "Linux"));
echo in_array_r("Irix", $b) ? 'found' : 'not found';

PHP: Check if an array contains all array values from another array

Look at array_intersect().

$containsSearch = count(array_intersect($search_this, $all)) === count($search_this);

Or for associative array, look at array_intersect_assoc().

Or for recursive compare of sub-arrays, try:

<?php

namespace App\helpers;

class Common {
/**
* Recursively checks whether $actual parameter includes $expected.
*
* @param array|mixed $expected Expected value pattern.
* @param array|mixed $actual Real value.
* @return bool
*/
public static function intersectsDeep(&$expected, &$actual): bool {
if (is_array($expected) && is_array($actual)) {
foreach ($expected as $key => $value) {
if (!static::intersectsDeep($value, $actual[$key])) {
return false;
}
}
return true;
} elseif (is_array($expected) || is_array($actual)) {
return false;
}
return (string) $expected == (string) $actual;
}
}

Search in Array of Arrays

I would do this:

//my_array being your multidimensional array
//my_id being the id you're looking for
$index = null;
for($i=0;$i<count($my_array);$i++)
{
//is the id in this sub array?
if($my_array[$i]['user_id'] == $my_id)
{
//get the index you're searching for
$index = $i;
}
}
//check if an index was found
if($index != null)
{
//display the index you're searching for
echo $index;
}

It's a little bit long but it allows you to do add stuff to the code if you need to.



Related Topics



Leave a reply



Submit