Search a multi-dimensional array for certain values - php

I have a multi-dimensional array in the following format:
[0] = (
'id' => '1',
'type' => 'fish',
'owner' => 'bob',
)
[1] = (
'id' => '2',
'type' => 'cat',
'owner' => 'mary',
)
[2] = (
'id' => '3',
'type' => 'dog',
'owner' => 'larry',
)
[3] = (
'id' => '2',
'type' => 'cat',
'owner' => 'fred',
)
I would like to search for a value, and they return an array that contains all keys from matching arrays and looks like this on a search for type=cat:
[0] = (
'id' => '2',
'type' => 'cat',
'owner' => 'mary',
)
[1] = (
'id' => '2',
'type' => 'cat',
'owner' => 'fred',
)
I know I'm trying to treat the array as a database, but in this case it's dynamic data that doesn't need to be stored once the program ends.
Any advice?

Loop through the array:
function loopAndFind($array, $index, $search){
$returnArray = array();
foreach($array as $k=>$v){
if($v[$index] == $search){
$returnArray[] = $v;
}
}
return $returnArray;
}
//use it:
$newArray = loopAndFind($oldArray, 'type', 'cat');

you must iterate array like:
foreach ($array as $i => $values) {
print "$i {\n";
foreach ($values as $key => $value) {
print " $key => $value\n";
}
print "}\n";
}
and then check for 'type' key value....
then record which is matched must copy it to new array...

Related

Create data set in php (loop)

Hello i am trying to create data set like
Expected output:-
Array
(
[0] => Array
(
[sku] => sku
[variant_option_one_name] => Color
[variant_option_one_value] => Cyan
),
[1] => Array
(
[sku] => sku
[variant_option_one_name] => Color
[variant_option_one_value] => Red
)
)
but i am not sure what is missing in code.
Here is the code
$array = array(
0 => array(
'id_product_attribute' => '17615',
'id_product' => '2295',
'reference' => '',
'available_date' => '0000-00-00',
'vend_id' => null,
'id_shop' => '1',
'id_attribute_group' => '1',
'is_color_group' => '1',
'group_name' => 'Color',
'attribute_name' => 'Cyan',
'id_attribute' => '1',
),
1 => array(
'id_product_attribute' => '17616',
'id_product' => '2295',
'reference' => '',
'available_date' => '0000-00-00',
'vend_id' => null,
'id_shop' => '1',
'id_attribute_group' => '1',
'is_color_group' => '1',
'group_name' => 'Color',
'attribute_name' => 'Red',
'id_attribute' => '21',
),
);
$ids = array();
foreach ($array as $combinations) {
$ids['sku'] = 'sku';
$ids['variant_option_one_name'] = $combinations['group_name'];
$ids['variant_option_one_value'] = $combinations['attribute_name'];
}
print_r($ids);//
Here i am getting
Array
(
[sku] => sku
[variant_option_one_name] => Color
[variant_option_one_value] => Red
)
The above output i am getting. Seems like data is overwrite
Any correction to get both the data ?
I do not get both the colors in array. It
Thankyou
You're absolutely right, the values are being overwritten each time.
What you need to do is, each time you loop you should create a new array containing your values, and then assign that array to a new index inside the main array (so you get an array of arrays, like the expected output you've shown):
foreach ($array as $combinations) {
$arr = array();
$arr['sku'] = 'sku';
$arr['variant_option_one_name'] = $combinations['group_name'];
$arr['variant_option_one_value'] = $combinations['attribute_name'];
$ids[] = $arr;
}
Live Demo: http://sandbox.onlinephpfunctions.com/code/3a0cf7f8cbb994ef4192c1e23493bef397785937
foreach ($array as $combinations) {
array_push ($ids, [
'sku' => 'sku',
'variant_option_one_name' => $combinations['group_name'],
'variant_option_one_value' => $combinations['attribute_name']
]);
}

Rebuilding php array

I am trying to rebuild php array, which I can get with one query from database.
To build lists and add items I need the array to be grouped and rebuild.
From query I receive array like this:
array (
0 =>
array (
'item_id' => '1',
'item_name' => 'aaa',
'group_id' => '7',
'group_name' => 'first'
),
1 =>
array (
'item_id' => '2',
'item_name' => 'bbb',
'group_id' => '7',
'group_name' => 'first'
),
2 =>
array (
'item_id' => '3',
'item_name' => 'ccc',
'group_id' => '9',
'group_name' => 'second'
),
3 =>
array (
'item_id' => '4',
'item_name' => 'ddd',
'group_id' => '9',
'group_name' => 'second'
)
);
I need to rearrange this array to following, which I can use for js parsing:
array(
array ('group_id' => '7', 'group_name' => 'first', 'items' => array (
0 => array (
'item_id' => '1',
'item_name' => 'aaa',
),
1 => array (
'item_id' => '2',
'item_name' => 'bbb',
)
)
),
array ('id' => '8', 'name' => 'second', 'items' => array (
0 =>
array (
'item_id' => '3',
'item_name' => 'ccc',
),
1 =>
array (
'item_id' => '4',
'item_name' => 'ddd',
)
)
)
);
I have stuck with following code:
function rebuild($groupby, $param, $data) {
$newarray = array();
foreach($data as $key => $val) {
if(array_key_exists($groupby, $val)){
$newarray[$val[$groupby]] = $val[$param];
$newarray['items'][] = $val;
}else{
$newarray[""][] = $val;
}
}
return $newarray;
}
$show = rebuild('group_id', 'group_name', $data);
echo "<pre>" . var_export($show, true) . "</pre>";
Any advise would appreciated.
I always like to use the unique identifier (here, the group ID), as array key in the resulting array - that makes it easier to associate the right items to the right new entry, without having to “search” the new array for the entry with the matching identifier.
$newarray = array();
foreach($data as $item) {
$newarray[$item['group_id']]['group_id'] = $item['group_id'];
$newarray[$item['group_id']]['group_name'] = $item['group_name'];
$newarray[$item['group_id']]['items'][] = [
'item_id' => $item['item_id'],
'item_name' => $item['item_name']
];
}
$newarray = array_values($newarray); // resets the array keys to a zero-based index
var_dump($newarray);
group_id and group_name get overwritten in the result array elements all the time - but since we access the correct entry by the group id already, that does not matter. They will always be “overwritten” with the values they already have, so that does no harm.
And item_id and item_name simply get added to the items array of the entry identified by the group id.

Inserting a new key if a particular key value matches in both the arrays

$array1 = array(
[0] => array(
'id' => 'gdye6378399sjwui39',
'name' => 'Plate 1'
),
[1] => array(
'id' => 'xyz6378399sjwui39',
'name' => 'Plate 2'
),
[2] => array(
'id' => 'tr2e6378399sjwui39',
'name' => 'Plate 3'
)
)
and another array
$array2 = array(
[0] => array(
'id' => 'gdye6378399sjwui39',
'ETA' => '8'
),
[1] => array(
'id' => 'tr2e6378399sjwui39',
'ETA' => '9'
)
[2] => array(
'id' => 'xyz6378399sjwui39',
'ETA' => '5'
)
)
I want to compare the two arrays. I am doing it like this way:-
if(!empty($array2))
{
foreach($array1 as $ck => $cl)
{
foreach($array12 as $ued){
if($cl['id'] == $ued['id'])
{
$array1[$ck]['ETA'] = $ued['ETA'];
break;
}
}
}
What are the other better ways to do this? The order of the two arrays may vary, and so does the size.
If you index the second array by the id (using array_column()) you can get away without using the inner foreach() and just use isset()...
$match = array_column($array2, null, 'id');
foreach ( $array1 as $ck=>$cl) {
if ( isset($match[$cl['id']]) ) {
$array1[$ck]['ETA'] = $match[$cl['id']]['ETA'];
}
}
print_r($array1);

delete subarrays from multiple array by equal values of key PHP

i faced with problem and i hope that you can help
I have array like this (but with hundreds of subarrays):
Array
(
[0] => Array
(
[id] => 211
[name] => Name
[description] => Desc
[Link] => http://link/211
[previewUrl] => https://link/id885364?mt=8
[payout] => 0.30
[image] => http://link/ios.png
[categories] => Array
(
[0] => iOS
[1] => Games
)
)
[1] => Array
(
[id] => 2
[name] => Name
[description] => Desc
[Link] => http://link/211
[previewUrl] => https://link/id885364?mt=8
[payout] => 2
[image] => http://link/ios.png
[categories] => Array
(
[0] => iOS
[1] => Games
)
)
)
I need to find all subarrays that equals by 'previewUrl' and then find among them one with max value of 'payout' and delete others with smaller value.
Thank you!
Loop through the original array ($arr) collecting the maximum payout values in a temporary array ($max_arr). When a higher payout is found replace the previous higher payout in the temporary array and delete it in the original array. When a lower or equal payout is found delete it.
<?php
$arr = array(array('id' => 211, 'name' => 'Name', 'description' => 'Desc', 'Link' => 'http://link/211', 'previewUrl' => 'https://link/id885364?mt=8', 'payout' => '0.30', 'image' => 'http://link/ios.png', 'categories' => array('0' => 'iOS', '1' => 'Games')), array('id' => 2, 'name' => 'Name', 'description' => 'Desc', 'Link' => 'http://link/211', 'previewUrl' => 'https://link/id885364?mt=8', 'payout' => '2', 'image' => 'http://link/ios.png', 'categories' => array('0' => 'iOS', '1' => 'Games')), array('id' => 11, 'name' => 'Name', 'description' => 'Desc', 'Link' => 'http://link/211', 'previewUrl' => 'https://link/id885364?mt=7', 'payout' => '3', 'image' => 'http://link/ios.png', 'categories' => array('0' => 'iOS', '1' => 'Games')), array('id' => 1, 'name' => 'Name', 'description' => 'Desc', 'Link' => 'http://link/211', 'previewUrl' => 'https://link/id885364?mt=7', 'payout' => '1', 'image' => 'http://link/ios.png', 'categories' => array('0' => 'iOS', '1' => 'Games')));
$max_arr = array(); // temporary array
foreach ( $arr as $key => $value ) {
if ( !isset($max_arr[$value['previewUrl']]) ) {
$max_arr[$value['previewUrl']] = array_merge(array('key' => $key), $value);
}
else {
// higher payout
if ( $max_arr[$value['previewUrl']]['payout'] < $value['payout'] ) {
unset($arr[$max_arr[$value['previewUrl']]['key']]);
$max_arr[$value['previewUrl']] = array_merge(array('key' => $key), $value);
}
else { unset($arr[$key]); } // lower or equal payout
}
}
?>
You can try with the following:
class MyArrayParser{
var $preview_url;
var $max_payout;
function filter_preview_url($subarray)
{
return $subarray["previewUrl"] == $this->preview_url;
}
function filter_payout($subarray)
{
return $subarray["payout"] == $this->max_payout;
}
function search_max_payout_by_previewUrl($big_array,$preview_url)
{
$this->preview_url = $preview_url;
$filtered_array = array_filter($big_array,array($this,"filter_preview_url")); //Only subarrays with previewUrl == $preview_url
function payout_extract($subarray)
{
return $subarray["payout"];
}
$payouts_list = array_map("payout_extract",$filtered_array);
if(count($payouts_list)==0) //PreviewUrl not found
return array();
$this->max_payout = max($payouts_list);
$only_max_payout_list = array_filter($filtered_array,array($this,"filter_payout"));
return $only_max_payout_list;
}
}
$obj = new MyArrayParser();
$filtered_array = $obj->search_max_payout_by_previewUrl($my_big_array,"....previewUrl...."));
Not necessarily fast, but it's pretty easy to understand.
$a = array(
array ('id' => 211,'previewUrl' => 'https://link/id885364?mt=8','payout' => 0.30),
array ('id' => 2,'previewUrl' => 'https://link/id885364?mt=8','payout' => 2));
$searchUrl = 'https://link/id885364?mt=8';
$to_delete = array();
$max_payout = -1;
$max_key = "";
// Loop through array, looking at previewUrls that match.
foreach ($a as $key => $subarray) {
if ($subarray['previewUrl'] == $searchUrl) {
// Save all matches to an array for deletion.
array_push($to_delete, $key);
// Find the element with the highest payout.
if ($subarray['payout'] > $max_payout || $max_payout == -1) {
$max_payout = $subarray['payout'];
$max_key = $key;
}
}
}
// Remove the element with the highest payout.
if (($key = array_search($max_key, $to_delete)) !== false) {
unset($to_delete[$key]);
}
//print $max_payout;
//print $max_key;
//print_r($to_delete);
// Finally, delete all the elements flagged for deletion.
foreach ($to_delete as $key) {
unset($a[$key]);
}
print_r($a);

Searching in a multi-dimensional array (PHP)

I'm having a problem lately that's driving me crazy. I have a multi-dimensional array like this:
$a = array(
'db' => array(
'0' => array(
'id' => '1',
'name' => 'test',
'cat' => array(
'a' => '15',
'b' => '20',
'c' => '30'
),
'canvas' => '2'
),
'1' => array(
'id' => '2',
'name' => 'test2',
'cat' => array(
'a' => '15',
'b' => '20',
'c' => '30'
),
'canvas' => '2'
)
'2' => array(
'id' => '3',
'name' => 'test',
'cat' => array(
'a' => '50',
'b' => '40',
'c' => '90'
),
'canvas' => '1'
)
)
);
And i want to search on it using a function like this: search('canvas = 1');
That would return all the arrays, child of db, that have a key canvas with the value of 1. Or, for example:
search('a = 15');
Would return all arrays that have a key, child of cat, named a and with a value of 15.
$a = array(
'db' => array(
'0' => array(
'id' => '1',
'name' => 'test',
'cat' => array(
'a' => '15',
'b' => '20',
'c' => '30'
),
'canvas' => '2'
),
'1' => array(
'id' => '2',
'name' => 'test2',
'cat' => array(
'a' => '15',
'b' => '20',
'c' => '30'
),
'canvas' => '2'
),
)
);
//checks if array $array contains element with $searchKey key, and $searchVal value
function arrayContains($array, $searchVal, $searchKey) {
if (!is_array($array))
return false;
foreach ($array as $key => $value) {
if ($key === $searchKey && $searchVal === $value)
return true;
if (is_array($value) && arrayContains($value, $searchVal, $searchKey))
return true;
}
return false;
}
function search($a, $search) {
list($searchKey, $searchVal) = explode('=', $search);
$result = array();
foreach($a as $val) {
if (arrayContains($val, $searchVal, $searchKey))
$result[] = $val;
}
return $result;
}
print_r(search($a['db'], "a=15"));
print_r(search($a['db'], "canvas=1"));
Which produces this output(outputs sub-arrays of $a['db'] which contain searched key=>value pair):
Array
(
[0] => Array
(
[id] => 1
[name] => test
[cat] => Array
(
[a] => 15
[b] => 20
[c] => 30
)
[canvas] => 2
)
[1] => Array
(
[id] => 2
[name] => test2
[cat] => Array
(
[a] => 15
[b] => 20
[c] => 30
)
[canvas] => 2
)
)
Array
(
[0] => Array
(
[id] => 3
[name] => test
[cat] => Array
(
[a] => 50
[b] => 40
[c] => 90
)
[canvas] => 1
)
)
Just check the below link if this can help you -
http://php.net/manual/en/function.array-search.php
It contains detailed documentation of php function array_search() and various user codes for searching in multi-dimensional array along with user reviews.
function search($array, $canvas)
{
$result = array();
foreach ($array as $k1 => $v1) {
foreach ($v1 as $k2 => $v2) {
if ($v2['canvas'] == $canvas) {
$result[] = $array[$k1][$k2];
}
}
}
return $result;
}
// $a = your array
print_r(search($a, 1));

Categories