PHP object to array sorting by associative key - php

Good morning, I'm just shook about it but, php has more than 10 ways to sort an array but I can't find one to sort by desired key dynamically.
I cannot set something like some uksorts because it will be filled dynamically. I'm using smarty on the front (which does not have array sort functions) and I'm trying to make a callable static to resort and print sorted arrays on one specific point.
I've applied some logic on it and I think it would be something like this.
Smarty:
{assign var='key' value='category_id'}
{assign var='direction' value='normal'}
{assign var='sortedItem' value=''}
{foreach $object.prop item="item"}
{sortedItem = Class::arraySortBy($item, $key, $direction)}
<a href="{$item['link']}">
{$item['name']}
</a>
{foreach}
PHP:
public static function arraySortBy($elemGroup, $sortBy, $direction) {
if(is_object($elemGroup)){
$elemGroup = (array) $elemGroup;
}
if ($direction == 'normal') {
// here is where i want to sort the elemGroup array by the key i want, as the key is product_id and i want to sort by category_id
} else if ($direction == 'reverse'){
// here the same but in reverse order
} else {
error_log('Direction not properly set.');
}
return $elemGroup;
}
the fact is that i want to re-order the objects bu category_id, and second by product_id IE:
itemA{product_id=1, category_id=1}
itemB{product_id=2, category_id=2}
itemC{product_id=3, category_id=1}
itemE{product_id=4, category_id=1}
itemD{product_id=5, category_id=2}
Result:
itemA
itemB
itemC
itemE
itemD
expected result
itemA
itemC
itemE
itemB
itemD
Is there a way to make this with PHP sorting functions or it has to be custom?
Thanks

Why can't you use uasort?
function custom_array_sort($arr, $sorts)
{
// Either pass an array of sorts, or every argument after the first one.
$sorts = is_array($sorts) ? $sorts : array_slice(func_get_args(), 1);
uasort($arr, function ($a, $b) use (&$arr, $sorts) {
for ($i = 0; $i < count($sorts); $i++) {
if ($a[$sorts[$i]] == $b[$sorts[$i]]) {
if (isset($sorts[$i + 1])) {
$arr = custom_array_sort($arr, array_slice($sorts, 1));
} else {
return 0;
}
} else {
return $a[$sorts[$i]] - $b[$sorts[$i]];
}
}
});
return $arr;
}
Live demo
This works by first comparing the category_id fields.
If these are the same then we compare the product_id. The subtraction is used so that the smaller of the product_ids is sorted before the bigger one.
If the category_ids are not the same then we do the same operation on the category_ids as we did above for product_id.
To implement this into your view, follow this documentation
$smarty->register_function('custom_array_sort', 'custom_array_sort_wrapper');
function custom_array_sort_wrapper($params, &$smarty)
{
if (empty($params['arr'])) {
$arr = [];
} else {
$arr = $params['arr'];
}
if (empty($params['sorts'])) {
$sorts = [];
} else {
$sorts = $params['sorts'];
}
return custom_array_sort($arr, $sorts);
}
This can then be used in your views in the following way:
{custom_array_sort arr=$object sorts=['category_id', 'product_id']}
The advantage of this new implementation is that you can specify as many columns to sort as you like.
It also means that you can specify different columns to sort for different arrays.
If you want to sort by more columns then just add another column name to the $sorts array.

You can you try this function.
function array_sort($array, $on, $order=SORT_ASC){
$new_array = array();
$sortable_array = array();
if (count($array) > 0) {
foreach ($array as $k => $v) {
if (is_array($v)) {
foreach ($v as $k2 => $v2) {
if ($k2 == $on) {
$sortable_array[$k] = $v2;
}
}
} else {
$sortable_array[$k] = $v;
}
}
switch ($order) {
case SORT_ASC:
asort($sortable_array);
break;
case SORT_DESC:
arsort($sortable_array);
break;
}
foreach ($sortable_array as $k => $v) {
$new_array[$k] = $array[$k];
}
}
return $new_array;
}
Example:
Array Value:-
$item_array = array (
'itemA' => array('product_id'=>1, 'category_id'=>1),
'itemB' => array('product_id'=>2, 'category_id'=>2),
'itemC' => array('product_id'=>3, 'category_id'=>1),
'itemD' => array('product_id'=>4, 'category_id'=>1),
'itemE' => array('product_id'=>5, 'category_id'=>2)
);
Use function:-
$itme_list = array_sort($item_array, 'category_id', SORT_ASC);
print_r($itme_list);
Output:
Array
(
[itemA] => Array
(
[product_id] => 1
[category_id] => 1
)
[itemC] => Array
(
[product_id] => 3
[category_id] => 1
)
[itemD] => Array
(
[product_id] => 4
[category_id] => 1
)
[itemB] => Array
(
[product_id] => 2
[category_id] => 2
)
[itemE] => Array
(
[product_id] => 5
[category_id] => 2
)
)
Note: Reference link :-
Sort PHP multi-dimensional array based on key?
How to sort multidimensional array by multiply keys?

Related

How do I get the value of the first occurrence of array_walk_recursive in php

I have a deep multidimensional array that I am needing to extract the value of a specific key. I have found that the array_walk_recursive function will be my best option. I only need the first occurrence.
My array looks like this - (except much more complicated)
Array (
[vehicle info] => Array (
[one] => Array (
[submodel] => LX
[engine] => 2.3
)
[two] => Array (
[color] => blue
[year] => 2007
[wheels] => 4
)
[three] => Array (
[submodel] => LX
[make] => Ford
[model] => F-150
[offroad] => No
)
)
)
The issue here is, submodel is in both one and three. Additionally, the array is not consistent, so I must use array_walk_recursive to search through it for the matching key, then return the value for that key.
Here is my current code -
array_walk_recursive ($array, (function ($item, $key) {
$wanted = "submodel";
if ($key === $wanted) {
echo ("$key is $item");
}
}));
The above returns submodel is LXsubmodel is LX.
Bonus Question!!
How can I search for multiple keys and return the first corresponding value for each of those? I was thinking putting all wanted keys in an array, then do a foreach loop, but don't quite know how to structure this. I am new to php.
array_walk_recursive() is the appropriate native function to call for this task. Keep track of which keys have already been declared in the result array and ensure that they are never overwritten.
Code: (Demo)
$needles = ['submodel', 'offroad'];
$result = [];
array_walk_recursive(
$array,
function($value, $key) use ($needles, &$result) {
if (
in_array($key, $needles)
&& !isset($result[$key])
) {
$result[$key] = "$key is $value";
}
}
);
var_export($result);
Output:
array (
'submodel' => 'submodel is LX',
'offroad' => 'offroad is No',
)
If your application has performance concerns, then the native function becomes less attractive because it will always iterate the entire input array's structure -- even after all sought keys are encountered. If you want to "break early" (short circuit), then you will need to design your own recursive function which will return when all sought keys are found.
Code: (Demo)
$soughtKeys = array_flip(['submodel', 'offroad']);
function earlyReturningRecursion(array $array, array $soughtKeys, array &$result = []): array
{
foreach ($array as $key => $value) {
if (!array_diff_key($soughtKeys, $result)) { // check if result is complete
return $result;
} elseif (is_array($value)) {
earlyReturningRecursion($value, $soughtKeys, $result);
} elseif (isset($soughtKeys[$key]) && !isset($result[$key])) {
$result[$key] = "$key is $value";
}
}
return $result;
}
var_export(earlyReturningRecursion($array, $soughtKeys));
// same output as the first snippet
I would start by setting the values you want to null, and then only saving them if they haven't been found yet, by checking is_null(). I haven't tested this code, but it should look something like this:
$submodel = null;
array_walk_recursive ($array, (function ($item, $key) {
$wanted = "submodel";
if ($key === $wanted && is_null($submodel)) {
echo ("$key is $item");
$submodel = $item;
}
}));
array_walk_recursive() has the defect of not allowing to return matching results however in PHP 7 you could use an anonymous function and a variable to store the matching value.
$matching = null;
$wanted = "submodel";
array_walk_recursive ($array, function ($item, $key) use ($wanted, $matching) {
if (($key === $wanted) && is_null($matching)) {
$matching = $item;
}
});
As far as there is no way to return early from array_walk_recursive(), I'd suggest to create a function to find the first occurrence of $wanted:
$arr = [
'vehicle info' => [
'one' => ['submodel' => 'LX', 'engine' => '2.3'],
'two' => ['color' => 'blue', 'year' => '2007', 'wheels' => '4'],
'three' => ['submodel' => 'LX', 'make' => 'Ford', 'model' => 'F-150', 'offroad' => 'No'],
],
];
function find($needle, $haystack, $found = '')
{
foreach ($haystack as $key => $value) {
if ($found) {
break;
}
if ($key === $needle) {
$found = "{$needle} is {$value}";
break;
}
if (is_array($value)) {
$found = find($needle, $value, $found);
}
}
return $found;
}
$wanted = 'submodel';
$result = find($wanted, $arr);
var_dump($result); // string(14) "submodel is LX"
Live demo
Update: to search for multiple keys you'll need to do it in a loop:
$multiple_keys = array('submodel', 'year');
foreach ($multiple_keys as $wanted) {
var_dump(find($wanted, $arr));
}
// Output:
// string(14) "submodel is LX"
// string(12) "year is 2007"
Live demo

cleaner way to pull dynamic nested elements in multidimensional array for sorting

Is there a cleaner way to extract a nested value from a 3 level deep multidimensional array, where i want to pull the result stacked inside the 3rd level, hwoever, i want to keep this dynamic so i can also grab elem from 2nd or 4th level by using an array as a parameter to determine this.
what im trying to do in the end is SORT using this element, but i cant find a way to conveniently indicate the element chain except for this way which i had to create myself:
public function keyBySubElement($nestedArray, array $subElemStack){
//essentially the loop below is doing this, but it is dynamic now so a user can specify different nested levels in the $subElemStack param.
//$nestedValue = $nestedArray[$subElemStack[0]][$subElemStack[1]];
foreach($subElemStack as $nestedElement){
if(isset($nestedValue) && is_array($nestedValue))
{
$nestedValue = $nestedValue[$nestedElement];
}
else
{
$nestedValue = $nestedArray[$nestedElement];
}
}
return $nestedValue;
}
e.g. to use this method:
assume the following data
$searchResults = array(
0 => array(
'title' => 'one',
array(
'ratings' => array(
'count' => '1'
)
)
),
1 => array(
'title' => 'two',
array(
'ratings' => array(
'count' => '5'
)
)
),
2 => array(
'title' => 'three',
array(
'ratings' => array(
'count' => '2'
)
)
),
);
foreach($searchResults as $k => $v){
$count = $this->keyBySubElement($v, array('ratings','count'));
$sortData[$k] = $count;
}
this gives me something like this
array(4) {
[0]=>
int(1)
[1]=>
int(5)
[2]=>
int(2)
}
now that i have access to my sub-sub elements value, tied in with its top level parent key, i can use it to sort the top level array by key using my new array $sortData as the reference key which can be reordered by the sub elements value that i want to sort with. i was next going just re-sort the original array by the new key values or something.
i saw a couple potential good examples, but i wasn't able to make them work. those examples are as follows:
[PHP sort: user function][1]
e.g. 1)
http://php.net/manual/en/function.sort.php#99419
e.g. 2)
Sort php multidimensional array by sub-value
e.g. 3)
/**
* Sort a 2 dimensional array based on 1 or more indexes.
*
* msort() can be used to sort a rowset like array on one or more
* 'headers' (keys in the 2th array).
*
* #param array $array The array to sort.
* #param string|array $key The index(es) to sort the array on.
* #param int $sort_flags The optional parameter to modify the sorting
* behavior. This parameter does not work when
* supplying an array in the $key parameter.
*
* #return array The sorted array.
*/
public function msort($array, $key, $sort_flags = SORT_REGULAR) {
if (is_array($array) && count($array) > 0) {
if (!empty($key)) {
$mapping = array();
foreach ($array as $k => $v) {
$sort_key = '';
if (!is_array($key)) {
$sort_key = $v[$key];
} else {
// #TODO This should be fixed, now it will be sorted as string
foreach ($key as $key_key) {
$sort_key .= $v[$key_key];
}
$sort_flags = SORT_STRING;
}
$mapping[$k] = $sort_key;
}
asort($mapping, $sort_flags);
$sorted = array();
foreach ($mapping as $k => $v) {
$sorted[] = $array[$k];
}
return $sorted;
}
}
return $array;
}
e.g. 4)
/**
* #param $array
* #param $cols
* #return array
*/
public function array_msort($array, $cols)
{
$colarr = array();
foreach ($cols as $col
=> $order) {
$colarr[$col] = array();
foreach ($array as $k => $row) {
$colarr[$col]['_'.$k] = strtolower($row[$col]);
}
}
$eval = 'array_multisort(';
foreach ($cols as $col => $order) {
$eval .= '$colarr[\''.$col.'\'],'.$order.',';
}
$eval = substr($eval,0,-1).');';
eval($eval);
$ret = array();
foreach ($colarr as $col => $arr) {
foreach ($arr as $k => $v) {
$k = substr($k,1);
if (!isset($ret[$k])) $ret[$k] = $array[$k];
$ret[$k][$col] = $array[$k][$col];
}
}
return $ret;
}
Since the data structure that they return is rather ugly and not condusive to sorting, my first move would be to reformat it into something that can be easily sorted. For example:
# create a new key, 'ratings', and put the contents of [0][ratings][count] in it
foreach ($searchResults as &$s) {
print_r($s);
# you could use your keybysubelement function to retrieve the value here
# rather than hardcoding it
$s['ratings'] = $s[0]['ratings']['count'];
unset($s[0]);
}
print_r($searchResults);
resulting data structure:
Array
(
[0] => Array
(
[title] => one
[ratings] => 1
)
[1] => Array
(
[title] => two
[ratings] => 5
)
[2] => Array
(
[title] => three
[ratings] => 2
)
)
It's then easy to create a sort function that will operate on this array to sort it according to the value in 'ratings':
# create a closure that will sort by a given key and in a given direction
# by default the order is ascending
function by_key($key, $dir = 'asc') {
return function ($a, $b) use ($key, $dir) {
if ($a[$key] > $b[$key]) {
if ($dir === 'asc')
return 1;
return -1;
}
elseif ($a[$key] < $b[$key]) {
if ($dir === 'asc')
return -1;
return 1;
}
return 0;
};
}
# sort by ratings, descending, using uasort and the custom search function:
uasort( $searchResults, by_key('ratings','desc') );
# print the results
foreach ($searchResults as $i) {
echo $i['title'] . ', ' . $i['ratings'] . PHP_EOL;
}
array order after sort:
two, 5
three, 2
one, 1
Sort by title:
uasort( $searchResults, by_key('title') );
Output:
one, 1
three, 2
two, 5

How to find object in php array and delete it?

Here is print_r output of my array:
Array
(
[0] => stdClass Object
(
[itemId] => 560639000019
[name] => Item no1
[code] => 00001
[qty] => 5
[id] => 2
)
[1] => stdClass Object
(
[itemId] => 470639763471
[name] => Second item
[code] => 76347
[qty] => 9
[id] => 4
)
[2] => stdClass Object
(
[itemId] => 56939399632
[name] => Item no 3
[code] => 39963
[qty] => 6
[id] => 7
)
)
How can I find index of object with [id] => 4 in order to remove it from array?
$found = false;
foreach($values as $key => $value) {
if ($value->id == 4) {
$found = true;
break;
}
}
if ($found) unset($values[$key]);
This is considered to be faster then any other solution since we only iterate the array to until we find the object we want to remove.
Note: You should not remove an element of an array while iterating so we do it afterwards here.
foreach($parentObj AS $key=>$element){
if ($element->id == THE_ID_YOU_ARE_LOOKING_FOR){
echo "Gottcha! The index is - ". $key;
}
}
$parentObj is obviously your root array - the one that holds all the others.
We use the foreach loop to iterate over each item and then test it's id property against what ever value you desire. Once we have that - the $key that we are on is the index you are looking for.
use array_search:
$a = new stdClass;
$b = new stdClass;
$a->id = 1;
$b->id = 2;
$arr = array($a, $b);
$index = array_search($b, $arr);
echo $index;
// prints out 1
try this
foreach($array AS $key=>$object){
if($object['id'] == 4){
$key_in_array = $key;
}
}
// chop it from the original array
array_slice($array, $key_in_array, 1);
Another way to achieve the result is to use array_filter.
$array = array(
(object)array('id' => 5),
(object)array('id' => 4),
(object)array('id' => 3)
);
$array = array_filter($array, function($item) {
return $item->id != 4;
});
print_r($array);
Here's my solution. Given, it is a bit hackish, but it will get the job done.
search(array $items, mixed $id[, &$key]);
Returns the item that was found by $id. If you add the variable $key it will give you the key of the item found.
function search($items, $id, &$key = null) {
foreach( $items as $item ) {
if( $item->id == $id ) {
$key = key($item);
return $item;
break;
}
}
return null;
}
Usage
$item = search($items, 4, $key);
unset($items[$key]);
Note: This could be modified to allow a custom key and return multiple items that share the same value.
I've created an example so you can see it in action.
A funny alternative
$getIdUnset = function($id) use ($myArray)
{
foreach($myArray as $key => $obj) {
if ($obj->id == $id) {
return $key;
}
}
return false;
};
if ($unset = $getIdUnset(4)) {
unset($myArray[$unset]);
}
Currently php does not have any supported function for this yet.
So refer to Java's Vector, or jQuery's $.inArray(), it would simply be:
public function indexOf($object, array $elementData) {
$elementCount = count($elementData);
for ($i = 0 ; $i < $elementCount ; $i++){
if ($object == $elementData[$i]) {
return $i;
}
}
return -1;
}
You can save this function as a core function for later.
In my case, this my array as $array
I was confused about this problem of my project, but some answer here helped me.
array(3) {
[0]=> float(-0.12459619130796)
[1]=> float(-0.64018439966448)
[2]=> float(0)
}
Then use if condition to stop looping
foreach($array as $key => $val){
if($key == 0){ //the key is 0
echo $key; //find the key
echo $val; //get the value
}
}
I know, after so many years this could be a useless answer, but why not?
This is my personal implementation of a possible index_of using the same code as other answers but let the programmer to choose when and how the check will be done, supporting also complex checks.
if (!function_exists('index_of'))
{
/**
* #param iterable $haystack
* #param callable $callback
* #param mixed|null &$item
* #return false|int|string
*/
function index_of($haystack, $callback, &$item = null)
{
foreach($haystack as $_key => $_item) {
if ($callback($_item, $_key) === true) {
$item = $_item;
return $_key;
}
}
return false;
}
}
foreach( $arr as $k=>&$a) {
if( $a['id'] == 4 )
unset($arr[$k]);
}

php find max in 2 dimension array but return another element('id') of the array

here is my array sample $data[][];
Array( [0] => Array ( [id] => 1349
[rating1] => 1.9378838029981E-7
[rating2] => 1.1801796607774 )
[1] => Array ( [id] => 1350
[rating1] => 5.5499981876923E-7
[rating2] => 1.5121329727308 )
[2] => Array ( [id] => 1377
[rating1] => 0.00023952225410117
[rating2] => 2.1947077830236 )
[3] => Array ( [id] => 1378
[rating1] => 0.00022982302863634
[rating2] => 2.2135588326622 )
[4] => Array ( [id] => 1379
[rating1] => 0.00026272979843585
[rating2] => 2.2388295595073 )
[5] => Array ( [id] => 1380
[rating1] => 0.0002788640872546
[rating2] => 2.1815325502993 )
)
I want to find max($data[][rating?]) but return $data[id][max_rating?] i.e. id associated with the max value.
Finding the max was easy for one particular, say rating1, I used array_reduce as follows (this is inspired from this SO ):
$max = array_reduce($data, function($a, $b) {
return $a > $b['rating1'] ? $a : $b['rating1'];
});
Now I have two questions :
1. How can I extend above array_reduce to include rating2 ? I have other ratingX as well.
2. I do not want the max value, rather the $data[][id] associated with the max.
I am not so much concerned about Q1, but the second one is important as I don't want to search through the array again to get associated $data[][id].
One line of thought is to use array_map instead of array_reduce, but I couldn't come up with a version which will pass on both [id] and [rating?]. Also, there are complications when I try to max() multiple rating? at one shot, as each rating will have different max, which in turn associates with different [id].
EDIT : Just to be clear, I want all the respective ids associated with respective max of each rating?
assuming your array is unsorted you have to loop through it at least once (either manually or using builtin functions). i'd use the following code:
$array = array(
array( 'id' => 1349, 'sudhi_rating1' => 1.9378838029981E-7, 'sudhi_rating2' => 1.1801796607774 ),
array( /* … */ ),
/* … */
);
$idx_max = 0;
foreach($array as $idx => $item) {
if($item['sudhi_rating1'] > $array[$idx_max]['sudhi_rating1'])
$idx_max = $idx;
}
echo "Rating 1 has max value at id ", htmlspecialchars($array[$idx_max]['id']);
you can extend the code to check multiple ratings at once (make $idx_max an array itself and add more ifs):
$idx_max = array (
'sudhi_rating1' => 0,
'sudhi_rating2' => 0,
/* … */ );
foreach($array as $idx => $item) {
foreach($idx_max as $rating => &$max) {
if($item[$rating] > $array[$max][$rating])
$max = $idx;
}
}
foreach($idx_max as $rating => $max)
echo 'Max value for ', htmlspecialchars($rating), ' has id ', htmlspeciachars($array[$max]['id']);
$max = array_reduce($data, function($a, $b) {
if (is_null($a)) return $b;
return max($a['rating1'],$a['rating2'])>max($b['rating1'],$b['rating2']) ? $a : $b;
});
Result: no Entries $max= NULL otherwise $max['id'] is the id with the max rating
Alternatively this generic code
$max = array_reduce($data, function($a, $b) {
if (is_null($a)) return $b;
return maxRating($a)>maxRating($b) ? $a : $b;
});
function maxRating($row){
return (max(array_intersect_key($row,array_flip(array_filter(array_keys($row),function ($item) { return strstr($item,'rating')!==FALSE;})))));
}
Will find for all ratings of the form rating?
EDIT -- The code was trying to answer Q1 here is the answer for just Q2
$max = array_reduce($data, function($a, $b) {
if (is_null($a)) return $b;
return $a['rating1']>$b['rating1'] ? $a : $b;
});
EDIT2 -- This is a generic solution for any number of rating? columns
$ratingKeys=array_filter(array_keys($data[0]),function ($item) { return strstr($item,'rating')!==FALSE;});
$max = array_reduce($data,function($a,$b) use (&$ratingKeys) {
if (is_null($a)) {
$a=array();
foreach($ratingKeys as $key) {
$a[$key]=$b[$key];
$a[$key.'_id'] = $b['id'];
}
return $a;
}
foreach($ratingKeys as $key) {
if ($a[$key]<$b[$key]) {
$a[$key]=$b[$key];
$a[$key.'_id']=$b['id'];
}
}
return $a;
});
This code results in
array(4) {
["rating1"]=> float(0.0002788640872546)
["rating1_id"]=> int(1380)
["rating2"]=> float(2.2388295595073)
["rating2_id"]=> int(1379)
}
EDIT 3 -- If you change the format of the input array to use id as the array key, you can massively simplify
$max=array_reduce(array_keys($data),function ($a,$b) use (&$data) {
if (is_null($a)) $a=array();
foreach(array_keys($data[$b]) as $item) {
if (!isset($a[$item]) {
$a[$item] = $b;
} else {
if ($data[$a[$item]][$item]) < $data[$b][$item]) $a[$item]=$b;
}
return $a;
}
});
This code results in
array(2) {
["rating1"]=> int(1380)
["rating2"]=> int(1379)
}

PHP foreach with Nested Array?

I have a nested array in which I want to display a subset of results. For example, on the array below I want to loop through all the values in nested array[1].
Array
(
[0] => Array
(
[0] => one
[1] => Array
(
[0] => 1
[1] => 2
[2] => 3
)
)
[1] => Array
(
[0] => two
[1] => Array
(
[0] => 4
[1] => 5
[2] => 6
)
)
[2] => Array
(
[0] => three
[1] => Array
(
[0] => 7
[1] => 8
[2] => 9
)
)
)
I was trying to use the foreach function but I cannot seem to get this to work. This was my original syntax (though I realise it is wrong).
$tmpArray = array(array("one",array(1,2,3)),array("two",array(4,5,6)),array("three",array(7,8,9)));
foreach ($tmpArray[1] as $value) {
echo $value;
}
I was trying to avoid a variable compare on whether the key is the same as the key I want to search, i.e.
foreach ($tmpArray as $key => $value) {
if ($key == 1) {
echo $value;
}
}
Any ideas?
If you know the number of levels in nested arrays you can simply do nested loops. Like so:
// Scan through outer loop
foreach ($tmpArray as $innerArray) {
// Check type
if (is_array($innerArray)){
// Scan through inner loop
foreach ($innerArray as $value) {
echo $value;
}
}else{
// one, two, three
echo $innerArray;
}
}
if you do not know the depth of array you need to use recursion. See example below:
// Multi-dementional Source Array
$tmpArray = array(
array("one", array(1, 2, 3)),
array("two", array(4, 5, 6)),
array("three", array(
7,
8,
array("four", 9, 10)
))
);
// Output array
displayArrayRecursively($tmpArray);
/**
* Recursive function to display members of array with indentation
*
* #param array $arr Array to process
* #param string $indent indentation string
*/
function displayArrayRecursively($arr, $indent='') {
if ($arr) {
foreach ($arr as $value) {
if (is_array($value)) {
//
displayArrayRecursively($value, $indent . '--');
} else {
// Output
echo "$indent $value \n";
}
}
}
}
The code below with display only nested array with values for your specific case (3rd level only)
$tmpArray = array(
array("one", array(1, 2, 3)),
array("two", array(4, 5, 6)),
array("three", array(7, 8, 9))
);
// Scan through outer loop
foreach ($tmpArray as $inner) {
// Check type
if (is_array($inner)) {
// Scan through inner loop
foreach ($inner[1] as $value) {
echo "$value \n";
}
}
}
foreach ($tmpArray as $innerArray) {
// Check type
if (is_array($innerArray)){
// Scan through inner loop
foreach ($innerArray as $value) {
echo $value;
}
}else{
// one, two, three
echo $innerArray;
}
}
Both syntaxes are correct. But the result would be Array. You probably want to do something like this:
foreach ($tmpArray[1] as $value) {
echo $value[0];
foreach($value[1] as $val){
echo $val;
}
}
This will print out the string "two" ($value[0]) and the integers 4, 5 and 6 from the array ($value[1]).
As I understand , all of previous answers , does not make an Array output,
In my case :
I have a model with parent-children structure (simplified code here):
public function parent(){
return $this->belongsTo('App\Models\Accounting\accounting_coding', 'parent_id');
}
public function children()
{
return $this->hasMany('App\Models\Accounting\accounting_coding', 'parent_id');
}
and if you want to have all of children IDs as an Array , This approach is fine and working for me :
public function allChildren()
{
$allChildren = [];
if ($this->has_branch) {
foreach ($this->children as $child) {
$subChildren = $child->allChildren();
if (count($subChildren) == 1) {
$allChildren [] = $subChildren[0];
} else if (count($subChildren) > 1) {
$allChildren += $subChildren;
}
}
}
$allChildren [] = $this->id;//adds self Id to children Id list
return $allChildren;
}
the allChildren() returns , all of childrens as a simple Array .
I had a nested array of values and needed to make sure that none of those values contained &, so I created a recursive function.
function escape($value)
{
// return result for non-arrays
if (!is_array($value)) {
return str_replace('&', '&', $value);
}
// here we handle arrays
foreach ($value as $key => $item) {
$value[$key] = escape($item);
}
return $value;
}
// example usage
$array = ['A' => '&', 'B' => 'Test'];
$result = escape($array);
print_r($result);
// $result: ['A' => '&', 'B' => 'Test'];
array(
"IT"=>
array(
array('id'=>888,'First_name'=>'Raahul','Last_name'=>'Pandey'),
array('id'=>656,'First_name'=>'Ravi','Last_name'=>'Teja'),
array('id'=>998,'First_name'=>'HRX','Last_name'=>'HRITHIK')
),
// array(
"DS"=>
array(
array('id'=>87,'First_name'=>'kalia','Last_name'=>'Pandey'),
array('id'=>6576,'First_name'=>'Raunk','Last_name'=>'Teja'),
array('id'=>9987,'First_name'=>'Krish','Last_name'=>'HRITHIK')
)
// )
)
);
// echo "";
// print_r($a);
echo "";
print_r($a);
?>
////how to get id in place of index value....

Categories