Outer Array Index - arrays

Array looks like below :
Array
(
[0] => Array
(
[memberid] => 5203
)
[1] => Array
(
[memberid] => 494
)
[2] => Array
(
[memberid] => 1053
)
[3] => Array
(
[memberid] => 1081
)
)
How can i find-out the outer array index (such as 0,1,2) using inner array.

You can iterate the outer array, and save the key if the current inner array value matches like this:
$outerKey = null;
foreach($outerArray as $key => $member){
if($member['memberid'] == 1081){
$outerKey = $key;
break;
}
}
echo $outerKey; // it will print 3

Related

How to remove first dimension of an array without loosing keys?

How can i remove the first dimansion of a multidemensional Array without loosing the keys?
i have an Array that have multiple arrays inside
the firstkey is a Date and the secondkey is the hour.
my Output is:
Array
(
[0] => Array
(
[firstkey] => Array
(
[secondkey] => Array
(
[0] => 7
[1] => 8
)
)
)
[1] => Array
(
[firstkey] => Array
(
[secondkey] => Array
(
[0] => 7
[1] => 8
)
)
)
)
and i want this:
Array
(
[firstkey] => Array
(
[secondkey] => Array
(
[0] => x
[1] => y
[2] => z
[3] => r
)
)
)
i also tried array_merge_recursive() but instead of putting the values to the secondkey it creates a new array with an incremental key
okay found a solution on:
PHP : multidimensional array merge recursive
function array_merge_recursive_ex(array $array1, array $array2)
{
$merged = $array1;
foreach ($array2 as $key => & $value) {
if (is_array($value) && isset($merged[$key]) && is_array($merged[$key])) {
$merged[$key] = array_merge_recursive_ex($merged[$key], $value);
} else if (is_numeric($key)) {
if (!in_array($value, $merged)) {
$merged[] = $value;
}
} else {
$merged[$key] = $value;
}
}
return $merged;
}
and with this function i can merge that mutlidemensional array without loosing keys:
$newarray =[];
foreach($array as $firstkey){
$newarray = array_merge_recursive_ex($newarray, $firstkey);
}

cakephp find all primary column as key

When I fire this:
$this->model->find('all',array());
I get an array with the data of the model:
Array
(
[0] => Array
(
// some data
)
[1] => Array
(
// some data
)
[2] => Array
(
// some data
)
...
)
Now is it possible instead of 0,1,2 to get the id as key for each of the data?
For example:
Array
(
[365] => Array
(
[model] => Array
(
[id] => 365
// some data
)
)
[442] => Array
(
[model] => Array
(
[id] => 442
// some data
)
)
[1000] => Array
(
[model] => Array
(
[id] => 1000
// some data
)
)
...
)
I know that find('list') can do something like that but only for 2 fields max (one key and one value).
you can build it maybe this code can do it :
$final_array = array();
foreach($returned_array as $k => $v){
$final_array[$v['id']] = $v;
}
print_r($final_array);
It's still an after-find operation but take a look at Set::combine (http://book.cakephp.org/2.0/en/core-utility-libraries/set.html) as a more elegant solution. Something like:
$newArray = Set::combine($yourArray, '{n}.Model.id');
/* $newArray now looks like:
Array
(
[365] =>
[442] =>
[1000] =>
)
*/
(more or less just the the docs example) might work in your case.

How to sort an array created by the directory_helper in CodeIgniter

So I'm using the directory_map function in the Directory Helper and I'm wondering how I can edit that function (or maybe extend it or something) so that it sorts the multidimensional array it gives me.
Here is the array it currently produces;
Array
(
[publications] => Array
(
[policy_documents] => Array
(
[_careers] => Array
(
[0] => careers.pdf
)
[_background_quality_reports] => Array
(
[0] => industry.pdf
[1] => international.pdf
[2] => departmental_resources.pdf
[3] => contracts.pdf
[4] => research_and_development.pdf
[5] => trade.pdf
)
[_pre_release_access_list] => Array
(
[0] => pre_release_access_list.pdf
)
)
[people] => Array
(
[health] => Array
(
[very_serious_injuries] => Array
(
[_1_january_2013] => Array
(
[0] => 1_january_2013.pdf
)
)
)
[military] => Array
(
[quarterly_manning_report] => Array
(
[_1_january_2013] => Array
(
[0] => 1_january_2013.pdf
)
)
[monthly_manning_report] => Array
(
[_20110201_1_february_2011] => Array
(
[0] => 1_february_2011.xls
[1] => key_points.html
[2] => 1_february_2011.pdf
)
[_20110301_1_march_2011] => Array
(
[0] => 1 March 2011.pdf
)
[_20110501_1_may_2011] => Array
(
[0] => 1 May 2011.pdf
)
[_20110401_1_april_2011] => Array
(
[0] => 1 April 2011.pdf
)
)
)
[civilian] => Array
(
[civilian_personnel_report] => Array
(
[_1_april_2012] => Array
(
[0] => 1_april_2012.pdf
)
[_1_october_2012] => Array
(
[0] => 1_october_2012.pdf
)
[_1_january_2013] => Array
(
[0] => 1_january_2013.pdf
[1] => key_points.html
)
[_1_july_2012] => Array
(
[0] => 1_july 2012.pdf
)
)
)
[search_and_rescue] => Array
(
[monthly] => Array
(
[_1_February_2013] => Array
(
[0] => 1_february_2013.pdf
)
)
[annual] => Array
(
[_2012] => Array
(
[0] => 2012.pdf
)
)
[quarterly] => Array
(
[_q3_2012] => Array
(
[0] => q3_2012.pdf
)
)
)
)
[estate] => Array
(
)
)
)
That's a little messy but you get the idea. Any attempt to wrap a sort() or an asort() around the variable in my model leads to an error. Which is why I think I might have to edit this function or maybe create me a new one...
You have to split up the array sort the files and directories separately since the dir names are in the keys and the file names are in the values.
$dir_map = dir_map_sort(directory_map('folder/name'));
/**
* Sorts the return of directory_map() alphabetically
* directories listed before files
*
* Example:
* a_dir/
* b_dir/
* a_file.dat
* b_file.dat
*/
function dir_map_sort($array)
{
$dirs = array();
$files = array();
foreach ($array as $key => $val)
{
if (is_array($val)) // if is dir
{
// run dir array through function to sort subdirs and files
// unless it's empty
$dirs[$key] = (!empty($array)) ? dir_map_sort($val) : $val;
}
else
{
$files[$key] = $val;
}
}
ksort($dirs); // sort by key (dir name)
asort($files); // sort by value (file name)
// put the sorted arrays back together
// swap $dirs and $files if you'd rather have files listed first
return array_merge($dirs, $files);
}
OR
/**
* Sorts the return of directory_map() alphabetically
* with directories and files mixed
*
* Example:
* a_dir/
* a_file.dat
* b_dir/
* b_file.dat
*/
function dir_map_sort($array)
{
$items = array();
foreach ($array as $key => $val)
{
if (is_array($val)) // if is dir
{
// run dir array through function to sort subdirs and files
// unless it's empty
$items[$key] = (!empty($array)) ? dir_map_sort($val) : $val;
}
else
{
$items[$val] = $val;
}
}
ksort($items); // sort by key
return $items;
}
You can order in SQL select statement (ORDER BY XXX) or in controller use one of these methods at
[1]: http://php.net/manual/en/array.sorting.php php.net

Associative array sort numeric

I have an associative array in following format.I need to sort the array in descending order and sort the array with same value as ascending order.
$numArray = array();
$numArray[0]['Word'] = 'One';
$numArray[0]['Number'] = 10.00;
$numArray[1]['Word'] = 'Two';
$numArray[1]['Number'] = 10.00;
$numArray[2]['Word'] = 'Three';
$numArray[2]['Number'] = 10.00;
$numArray[3]['Word'] = 'Four';
$numArray[3]['Number'] = 30.00;
$numArray[4]['Word'] = 'Five';
$numArray[4]['Number'] = 20.00;
$numArray[5]['Word'] = 'Six';
$numArray[5]['Number'] = 10.00;
$numArray[6]['Word'] = 'Seven';
$numArray[6]['Number'] = 20.00;
$numArray[7]['Word'] = 'Eight';
$numArray[7]['Number'] = 20.00;
Please help me to sort the array in following order.
Array
(
[0] => Array
(
[Word] => Four
[Number] => 30
)
[1] => Array
(
[Word] => Five
[Number] => 20
)
[2] => Array
(
[Word] => Seven
[Number] => 20
)
[3] => Array
(
[Word] => Eight
[Number] => 20
)
[4] => Array
(
[Word] => One
[Number] => 10
)
[5] => Array
(
[Word] => Two
[Number] => 10
)
[6] => Array
(
[Word] => Three
[Number] => 10
)
[7] => Array
(
[Word] => Six
[Number] => 10
)
)
foreach ($numArray as $key => $row) {
$array[$key] = $row['Number'];
}
array_multisort($array, SORT_DESC, $numArray);
This little php code will at least sort your array desc according to the Number.
guess thats half of what you want .

Convert 3 dimension to 2 dimension

I got a query result like this : -
Array(
[0] => Array
(
[0] => Array
(
[id] => 1
[name] => Japan
)
)
[1] => Array
(
[0] => Array
(
[id] => 2
[name] => Nepal
)
)
}
How can I convert it to
Array
(
[0] => Japan
[1] => Nepal
)
The query used to get first array is 'select id , name from country;'
Please help me.
foreach ($element as $array) {
$element = $element[0]["name"];
}
Or,
function extractName($element) {
return $element[0]["name"];
}
$newArray = array_map("extractName", $array);
Extract!
$newArray = Set::extract($oldArray, '{n}.0.name');
More here.

Resources