I have this array:
Array
(
[8008] => Array
(
[main_mob_vnum] => 8008
[0] => Array
(
[drop_mob_vnum] => 50300
[drop_mob_count] => 2
[drop_mob_percent] => 110.00
)
[1] => Array
(
[drop_mob_vnum] => 39030
[drop_mob_count] => 2
[drop_mob_percent] => 85.00
)
)
[8009] => Array
(
[main_mob_vnum] => 8009
[0] => Array
(
[drop_mob_vnum] => 50300
[drop_mob_count] => 4
[drop_mob_percent] => 110.00
)
[1] => Array
(
[drop_mob_vnum] => 50125
[drop_mob_count] => 1
[drop_mob_percent] => 75.00
)
)
[8010] => Array
(
[main_mob_vnum] => 8009
[0] => Array
(
[drop_mob_vnum] => 50125
[drop_mob_count] => 4
[drop_mob_percent] => 110.00
)
)
)
I want to make a function like
function searchArray($array,$vnum)
if $vnum is == with drop_mob_vnum take the previously array and put all this array keys on new array
For example, searchArray($array,50300) should return an array like: array(8008,8009) because 8010 doesn't have a drop_mob_vnum == 50300
I know StackOverflow requires that I post my problem with code, but I don't know where to start. If someone could help me, I would appreciate it. Thank you!
This function should do the trick:
function find_vnum($array,$value)
{
$result = array();
foreach ($array AS $k1 => $v1)
{
foreach ($v1 AS $k2 => $v2)
{
if ($k2 === 'main_mob_vnum') {
continue;
}
foreach ($v2 AS $k3 => $v3)
{
if ($k3 !== 'drop_mob_vnum') {
continue;
}
if (($v3 === $value) && !in_array($k1, $result)) {
$result[] = $k1;
}
}
}
}
return $result;
}
Given your sample data:
print_r(find_vnum($data,50300));
// Array
// (
// [0] => 8008
// [1] => 8009
// )
You can see a working demo here.
Related
I have a multidimensional associative array which has a set of array. I want to change my array index value from some array value.
I already tried some array functions but my array also contains some null array so laravel function keyBy not give me wanted result.
$arr1=array(0 =>array(),1=>array(0=>array('quan'=>10,'handle' => 'baroque'),1 =>array('quan'=>20,'handle' => 'baroque')),
2 =>array (0 =>array('quan' => 5,'handle' => 'adidas')));
My expected result array must be like this
$arr2=array(0 =>array(),'baroque'=>array(0=>array('quan'=>10,'handle' => 'baroque'),1 =>array('quan'=>20,'handle' => 'baroque')),
'adidas' =>array (0 =>array('quan' => 5,'handle' => 'adidas')));
You can use the classic foreach. Check if the handle on element 0 exists using isset, if it does, use that as the key.
$arr1 = //...
$result = array();
foreach($arr1 as $key => $val) {
if (is_array($val) && isset($val[0]["handle"])) $result[ $val[0]["handle"] ] = $val;
else $result[$key] = $val;
}
$result will be:
Array
(
[0] => Array
(
)
[baroque] => Array
(
[0] => Array
(
[quan] => 10
[handle] => baroque
)
[1] => Array
(
[quan] => 20
[handle] => baroque
)
)
[adidas] => Array
(
[0] => Array
(
[quan] => 5
[handle] => adidas
)
)
)
You can use without condition by grouping at the handle as key directly.
$result = [];
foreach ($arr as $key => $value) {
if (!empty($value)) {
foreach ($value as $key1 => $value1) {
$result[$value1['handle']][] = $value1;
}
} else {
$result[] = $value;
}
}
Demo
Output:-
Array
(
[0] => Array
(
)
[baroque] => Array
(
[0] => Array
(
[quan] => 10
[handle] => baroque
)
[1] => Array
(
[quan] => 20
[handle] => baroque
)
)
[adidas] => Array
(
[0] => Array
(
[quan] => 5
[handle] => adidas
)
)
)
Try this..
$res = [];
foreach($x as $key => $value)
{
if(empty($value))
{
$res[] = $value;
}
else
{
foreach($value as $v => $k)
{
if(array_key_exists($k['handle'],$res))
{
$res[$k['handle']][] = ['quan' => $k['quan'],'handle' => $k['handle']];
}
else
{
$res[$k['handle']][0] = ['quan' => $k['quan'],'handle' => $k['handle']];
}
}
}
}
The result is going to be like this.
Array
(
[0] => Array
(
)
[baroque] => Array
(
[0] => Array
(
[quan] => 10
[handle] => baroque
)
[1] => Array
(
[quan] => 20
[handle] => baroque
)
)
[adidas] => Array
(
[0] => Array
(
[quan] => 5
[handle] => adidas
)
)
)
Here is the array,
array(
[0] => Array
(
[IdRedeemProduct] => Item-A
[RedeemOptions] => Array
(
[0] => Array
(
[Points] => 1000
)
[1] => Array
(
[Points] => 2000
)
[2] => Array
(
[Points] => 43000
)
)
[ProductType] => 1
)
[1] => Array
(
[IdRedeemProduct] => Item-B
[RedeemOptions] => Array
(
[0] => Array
(
[Points] => 6200
)
[1] => Array
(
[Points] => 53000
)
)
[ProductType] => 1
)
)
most of the usort examples are just 2 level dimension array. I couldn't find any example for 3 level.
In this case i wanted to sort the smallest points to show first. Item-A will be the first and Item-B will be the 2nd.
foreach ($filteredResults as $key => $row)
{
foreach ($row['RedeemOptions'] as $key2 => $option) {
$vc_array_name[$key] = $option['Points'];
}
}
array_multisort($vc_array_name, SORT_ASC, $filteredResults);
this is working...
Try this:
function sort_2d_desc($array, $key) {
usort($array, function($a, $b) use ($key) {
return strnatcasecmp($b[$key], $a[$key]);
});
return $array;
}
$a = [];
foreach($arr as $key => $val){
$a[$key] = $this->sort_2d_desc($val['RedeemOptions'], 'Points');
}
$newArr = [];
foreach($arr as $key => $val){
$newArr[] = ['IdRedeemProduct' => $val['IdRedeemProduct'], 'RedeemOptions' => $a, 'ProductType' => $val['ProductType']];
}
print_r($newArr);
I have the following multidimensional array:
Array
(
[0] => Array
(
)
[1] => Array
(
[0] => Array
(
[id] => 74
[RecordGUID] => 9BD28E1E-99EB-D4E7-CC2C-AB6F5905BCDA
[Type] => DENTAL
[App_Service] => a:4:{i:151;s:6:"AAMCAS";i:152;s:3:"DDS";i:154;s:11:"APP SVC TWO";i:155;s:6:"AADSAS";}
)
[1] => Array
(
[id] => 73
[RecordGUID] => A5146CFF-5D17-FB1A-D831-E6835D0A04DD
[Type] => MED
[App_Service] => a:1:{i:151;s:6:"AAMCAS";}
)
[2] => Array
(
[id] => 75
[RecordGUID] => 0C253109-07E7-151A-0277-19EAC025C2E6
[Type] => PHYSICAL THERAPY
[App_Service] => a:1:{i:153;s:8:"PHYSTHER";}
)
)
[2] => Array
(
[0] => Array
(
[id] => 155
[RecordGUID] => 5DF76F3E-2F0C-FD63-B58F-027A61E9BC11
[AppService] => AADSAS
[AppService_types] =>
)
[1] => Array
(
[id] => 151
[RecordGUID] => 3B503CFC-AB80-C06B-C4C4-8EE548FFC7BF
[AppService] => AAMCAS
[AppService_types] =>
)
[2] => Array
(
[id] => 154
[RecordGUID] => 753D95F2-6733-AE27-8F2E-48685DC796C0
[AppService] => APP SVC TWO
[AppService_types] =>
)
[3] => Array
(
[id] => 152
[RecordGUID] => 0D3C9435-64DD-9079-C0F4-D543DFFA0E10
[AppService] => DDS
[AppService_types] =>
)
[4] => Array
(
[id] => 153
[RecordGUID] => 0D196967-21AF-ADDA-920E-F12938DACADB
[AppService] => PHYSTHER
[AppService_types] =>
)
)
)
I want to find the Type key that equals MED and then grab the App_Service value right below it.
I'm a bit stuck. Any help would be greatly appreciated. Thanks in advance.
That is not multi-dimensional, it is a nested structure. Arrays are associative i.e. maps in PHP.
To find that value you can do the following (where $x is your array)
foreach($x as $entry) {
foreach($entry as $subentry) {
if ($subentry['Type'] == "MED") {
echo $subentry['App_service'];
}
}
}
Try this out:
foreach($MDArray as $array) {
if(array_key_exists("Type",$array) {
foreach($array as $k => $v) {
if($k == "MED") {
return $array['AppService'];
}
}
}
}
Replace $MDArray with the name of your multi-dimensional array.
If your data can have several arrays with type MED, try the following recursive function:
function findByType($array, $val, &$result) {
foreach ($array AS $k => $v) {
if (is_array($v)) {
if (isset($v['Type']) && $v['Type'] == $val) {
$result[] = $v['App_Service'];
} else {
findByType($v, $val, $result);
}
}
}
}
$result = array();
findByType($data, 'MED', $result);
var_dump($result);
Something simple usually gets the job done.
foreach( $array[1] as $find )
{
if( array_search( 'MED', $find ) )
{
echo $find['App_Service'];
}
}
Or, if you prefer a recursive function to check through the entire array:
function findRecursive( $type, $array )
{
foreach( $array as &$section )
{
if( is_array( $section ) )
{
if( in_array( $type, $section ) )
{
return $section['App_Service'];
}
elseif( ( $test = findRecursive( $type, $section ) ) !== FALSE )
{
return $test;
}
}
}
return false;
}
The function would be useful to reuse for different types and if your array may not have the same format.
Edit
Fixed my function. Doesn't require array_search anyway.
How to arrange this array by last inner index ( 0, 1, 2 ) and get the value of the last inner index as the value of each second index:
Array
(
[text] => Array
(
[grid] => Array
(
[0] => 3
[1] => 4
[2] => 5
)
[image] => Array
(
[0] =>
[1] =>
[2] =>
)
[align] => Array
(
[0] => left
[1] => right
[2] => left
)
[title] => Array
(
[0] =>
[1] =>
[2] =>
)
[content] => Array
(
[0] =>
[1] =>
[2] =>
)
)
)
And have the results as below:
Array
(
[text] => Array
(
[0] => Array
(
[grid] => 3
[image] =>
[align] => left
[title] =>
[content] =>
)
[1] => Array
(
[grid] => 4
[image] =>
[align] => right
[title] =>
[content] =>
)
[2] => Array
(
[grid] => 5
[image] =>
[align] => left
[title] =>
[content] =>
)
)
)
This will do the work
function restructure($arr){
$newArr = array();
foreach($arr as $k => $v){
foreach($v as $k1 => $v1){
foreach($v1 as $k2 => $v2){
$newArr[$k][$k2][$k1] = $v2;
}
}
}
return $newArr;
}
As SiGanteng suggested, i dont see other ways than a for/foreach loop:
function buildArray($source, $key = false)
{
// Build the new array
$new_array = array();
// Define groups
$groups = $key === false ? array_keys($source) : array($key);
foreach($groups AS $group)
{
// Get the keys
$keys = array_keys($array[$group]);
// Count the values
$num_entries = count($array[$group][$keys[0]]);
for($i = 0; $i < $num_entries; $i++)
{
foreach($keys AS $key)
{
$new_array[$group][$i][$key] = $array[$group][$key][$i];
}
}
}
return $new_array;
}
This allow you to define the key you need to build; If not specified, the function build the array for every key.
This should work.
function formatit($arr) {
$new = array();
foreach($arr as $k=>$v) {
foreach($v as $k1=>$v1) {
$new[$k1][$k] = $v1;
}
}
return $new;
}
Tested. Call it as
$arr['text'] = formatit($arr['text']);
http://ideone.com/rPzuR
So this is an example:
Array (
[0] => Array ( [title] => Title_1 [checkout] => 1 [no_gateway] => 0 )
[1] => Array ( [title] => Title_2 [checkout] => 1 [no_gateway] => 1 )
[2] => Array ( [title] => Title_3 [checkout] => 0 [no_gateway] => 0 )
[3] => Array ( [title] => Title_4 [checkout] => 1 [no_gateway] => 1 )
[4] => Array ( [title] => Title_5 [checkout] => 0 [no_gateway] => 0 )
[5] => Array ( [title] => Title_6 [checkout] => 1 [no_gateway] => 0 )
)
I need to print out all values under [title] key having [checkout] => 1 & [no_gateway] => 0
In my case it should looks like
Title_1
Title_6
Please help php-beginner :) Thanks!
foreach($array as $row) {
if ($row['checkout'] && !$row['no_gateway']) {
print $row['title'];
}
}
foreach ($items as $item) {
if($item['checkout'] == 1 && $item['no_gateway'] == 0) {
echo $item['title'];
}
}
assuming your array is called $items
print_r(
array_map(function ($a) { return $a["title"]; },
array_filter($original,
function ($a) { return $a["checkout"] && !$a["no_gateway"]; }
)
)
);
You tagged the question with the answer: foreach
// assuming $arr is the array containing the values from the example
foreach ($arr as $record) {
if ($record['checkout'] && !$record['no_gateway']) {
echo $record['title'], "\n";
}
}
foreach( $array as $value ) {
if( $value["checkout"] == 1 && $value["no_gateway"] == 0 ) {
print $value["title"].PHP_EOL;
}
}