How to retrieve data from multiple arrays - php

I have two arrays that was converted from csv file.
The first csv line looks like this:
franchise_id,franchise_name,phone,website,email,region_codes;
1,"Abbott, Hackett and O`Conner",1-648-177-9510,auto-service.co/bw-319-x,Lupe-2485#auto-service.co,"36101,36055,36071";
The second csv line looks like this:
postal_code,region_code,city,state,region;
14410,36055,Adams Basin,NY,Monroe;
I converted these lines to arrays like this:
//Region Array
$region_lines = explode(PHP_EOL, $region_mappings_string);
$region_array = array();
foreach ($region_lines as $region_line) {
$region_array[] = str_getcsv($region_line);
}
//Franchise Array
$franchise_lines = explode(PHP_EOL, $franchises_string);
$franchise_array = array();
foreach ($franchise_lines as $franchise_line) {
$franchise_array[] = str_getcsv($franchise_line);
}
After that I got the result like this for Region:
Array
(
[0] => Array
(
[0] => postal_code
[1] => region_code
[2] => city
[3] => state
[4] => region;
)
[111] => Array
(
[0] => 14410
[1] => 36055
[2] => Adams Basin
[3] => NY
[4] => Monroe;
)
[112] => Array
(
[0] => 14617
[1] => 36055
[2] => Rochester
[3] => NY
[4] => Monroe;
)
And for franchise:
Array
(
[0] => Array
(
[0] => franchise_id
[1] => franchise_name
[2] => phone
[3] => website
[4] => email
[5] => region_codes;
)
[1] => Array
(
[0] => 1
[1] => Abbott, Hackett and O`Conner
[2] => 1-648-177-9510
[3] => auto-service.co/bw-319-x
[4] => Lupe-2485#auto-service.co
[5] => 36101,36055,36071;
)
What I need to do is to look for 14410 postal code from the first array, get the region_code and look for this region_code in second array and then output the results in PHP. How can this be done?

Will have to loop over franchises array and run simple comparison
function getDataByRegionCode($code, $franchiseArray) {
foreach ($franchiseArray as $key => $data) {
if(isset($data[5])){
$codes = explode(',',$data[5]);
if(in_array($code,$codes)) return $data;
}
}
return NULL;
}
print_r(getDataByRegionCode(14410,$franchiseArray));

Related

Combine some php sub-arrays, with condition

$routes array:
[admin/login] => Array
(
[0] => POST|AJAX
[1] => admin/login
[2] => admin/login
)
[admin/logout] => Array
(
[0] => POST
[1] => admin/logout
[2] => admin/logout
)
[admin/dashboard] => Array
(
[0] => GET
[1] => admin/dashboard
[2] => admin/dashboard
)
[admin/products] => Array
(
[0] => GET
[1] => admin/products/[:pag]
[2] => admin/products/all
)
[admin/products/add] => Array
(
[0] => GET
[1] => admin/product/add
[2] => admin/products/add
)
[admin/products/insert] => Array
(
[0] => POST
[1] => admin/product/insert
[2] => admin/products/insert
)
[admin/products/show] => Array
(
[0] => GET
[1] => admin/product/[i:id]
[2] => admin/products/show
)
[admin/products/edit] => Array
(
[0] => GET
[1] => admin/product/[i:id]/edit
[2] => admin/products/edit
)
[admin/products/update] => Array
(
[0] => POST
[1] => admin/product/update
[2] => admin/products/update
)
[admin/products/delete] => Array
(
[0] => POST
[1] => admin/product/delete
[2] => admin/products/delete
)
[admin/categories] => Array
(
[0] => GET
[1] => admin/categories
[2] => admin/categories/all
)
[admin/categories/add] => Array
(
[0] => GET
[1] => admin/category/add
[2] => admin/categories/add
)
[admin/categories/insert] => Array
(
[0] => POST
[1] => admin/category/insert
[2] => admin/categories/insert
)
[admin/categories/show] => Array
(
[0] => GET
[1] => admin/category/[i:id]
[2] => admin/categories/show
)
[admin/categories/edit] => Array
(
[0] => GET
[1] => admin/category/[i:id]/edit
[2] => admin/categories/edit
)
[admin/categories/update] => Array
(
[0] => POST
[1] => admin/category/update
[2] => admin/categories/update
)
[admin/categories/delete] => Array
(
[0] => POST
[1] => admin/category/delete
[2] => admin/categories/delete
)
[admin/api] => Array
(
[0] => GET
[1] => admin/api
[2] => admin/api/all
)
[admin/filter/sessions] => Array
(
[0] => POST|AJAX
[1] => admin/filter/sessions
[2] => admin/filters/sessions
)
This is $admin_routes array (build from $routes):
[login] => Array
(
[0] => admin/login
)
[logout] => Array
(
[0] => admin/logout
)
[dashboard] => Array
(
[0] => admin/dashboard
)
[products] => Array
(
[0] => admin/products
[1] => admin/products/add
[2] => admin/products/insert
[3] => admin/products/show
[4] => admin/products/edit
[5] => admin/products/update
[6] => admin/products/delete
)
[api] => Array
(
[0] => admin/api
)
[filter] => Array
(
[0] => admin/filter/sessions
)
[categories] => Array
(
[0] => admin/categories
[1] => admin/categories/add
[2] => admin/categories/insert
[3] => admin/categories/show
[4] => admin/categories/edit
[5] => admin/categories/update
[6] => admin/categories/delete
)
I need some help from you guys about $admin_routes.
I want to combine all sub-arrays from $admin_routes, from same area, which has only one element, in a new sub-array with key name formed from their key names.
Also i want the new sub-array to have same 'position' in line.
My imperfect solution:
// creating $admin_routes
$admin_routes = [];
foreach ($routes as $name => $route) {
if (preg_match('/^admin\/(.*)$/', $name))
$admin_routes[explode('/', $name)[1]][] = $name;
}
// imperfect solution for what i want
$single = false;
$waiting = [];
foreach ($admin_routes as $section => $routes) {
if (count($routes) == 1) { // remember all sub-arrays, with one single element, from same area
$single = true;
$waiting[$section] = $routes;
}
else if ($single) { // if found all sub-arrays, with one single element, from same area
$name = '';
$subarray = [];
foreach ($waiting as $section => $routes) {
// creating only one sub-array
$name .= ($section . ' ');
$subarray = array_merge($subarray, $routes);
unset($admin_routes[$section]);
}
// the problem is, sub-array it's added at the end of $admin_routes
// (not where sub-array's elements were before)
$admin_routes[$name] = $subarray;
$single = false;
$waiting = [];
}
}
The problem is, the new sub-arrays are placed at the end.
If you have any idea, please help me. Thx.
Using your actual result $admin_routes, you could loop through them and rebuild the desired array:
$real_routes = []; // The new array
$singles_key = ''; // The key for combined sub-arrays
$singles_routes = []; // The routes for combined sub-arrays
foreach ($admin_routes as $name => $r) {
/* If this route array has a single entry,
add its name and value to our temp vars */
if (count($r) === 1) {
$singles_key .= $name . ' ';
$singles_routes = array_merge($singles_routes, $r);
} else {
/* If then a route array has multiple value,
save the combined singles and then save this one */
if (!empty($singles_key)) {
$real_routes[trim($singles_key)] = $singles_routes;
$singles_key = '';
$singles_routes = [];
}
$real_routes[$name] = $r;
}
}
/* If the loop ends but the temp vars aren't empty,
save the lasts combined singles */
if (!empty($singles_key)) {
$real_routes[trim($singles_key)] = $singles_routes;
}
// Then to output:
echo '<pre>' . print_r($real_routes, true) . '</pre>';

get count of entries in multi-dimensional array with comma separated values- php

How to get count of entries in an array below?
I tried Count arrays comma separated values and didnot get desired solution.Please suggest a method.
For below sample count is 11.
$sample= Array (
[0] => Array (
[0] => Array ( [attendance] => 2012SD71,2010SD94 )
[1] => Array ( [attendance] => 2003SD18,2003SD19 )
[2] => Array ( [attendance] => 2003SD23,2003SD28 ))
[1] =>
[2] =>
[3] =>
[4] =>
[5] =>
[6] =>
[7] =>
[8] =>
[9] =>
[10] =>
[11] =>
[12] =>
[13] => Array (
[0] => Array ( [attendance] => 2012SD81,2010SD84 )
[1] => Array ( [attendance] => 2003SD18,2003SD19,2004SD14 ) ) [14] =>
[15] =>
);
A simple recursive function should work that uses explode() and array_merge():
function recurse($array,&$new)
{
foreach($array as $key => $value) {
if(is_array($value)) {
recurse($value,$new);
}
else {
if(!empty($value)) {
$exp = array_filter(explode(',',$value));
$new = array_merge($new,$exp);
}
}
}
}
# Create a storage array
$new = array();
# Run the recursive function
recurse($sample,$new);
# Show count
print_r(count($new));

Build array by string name OR make multiple array by string values

I want to build a array or multiple array by breaking the main array , and my array is like ,
Array
(
[0] => string1
[1] => 1
[2] => 2
[3] => 3
[4] => 66
[5] => 34
[6] => string1
[7] => aww
[8] => brr
[9] => string3
[10] => xas
)
So basically by the value 'string1' i want to make a new array or first array which has only those three values (1,2,3) and same for string2 and string3,So each array has its values(three).
Please help me to build this.
Note: those all string names will be static.
Thank you in advance.
Result should me like:
string1 array:
<pre>Array
(
[1] => 1
[2] => 2
[3] => 3
[4] => 66
[5] => 34
)
string2 array:
<pre>Array
(
[1] => aww
[2] => brr
)
string3 array:
<pre>Array
(
[1] => xas
)
This I think will get you what you want.
It does assume that the first entry in the old array will be a keyword!
$old = array('string1',1,2,3,66,34,'string2','aww','brr','string3','xas');
$new = array();
$keywords = array('string1', 'string2', 'string3');
$last_keyword = '';
foreach ($old as $o) {
if ( in_array($o, $keywords) ) {
$last_keyword = $o;
} else {
$new[$last_keyword][] = $o;
}
}
print_r($new);
It creates a new array like this
Array
(
[string1] => Array
(
[0] => 1
[1] => 2
[2] => 3
[3] => 66
[4] => 34
)
[string2] => Array
(
[0] => aww
[1] => brr
)
[string3] => Array
(
[0] => xas
)
)
However I still maintain that it would be better to go back to where the original array gets created and look to amend that process rather than write a fixup for it

Output of all result of grouped array

I hope you could help me on how should I make this output be done.
CSV file
Department,Name,Employee No.,Date Time
LMS,"Bach, Jerome",102,6/30/2014 12:23
MTS,"Lorvia, Christine",103,6/16/2014 9:31
SSS,Jannah Curtis,104,6/16/2014 8:45
SSS,Jannah Curtis,104,6/28/2014 14:29
ITM,Sassy Mica,105,6/17/2014 9:12
ITM,Sassy Mica,105,6/17/2014 20:43
ITM,Sassy Mica,105,6/18/2014 9:12
I already grouped the department and this is the output
[MTS] => Array
(
[103] => Array
(
[0] => MTS
[1] => Lorvia Christine
[2] => 103
[3] => 6/16/2014 9:31
)
)
[SSS] => Array
(
[104] => Array
(
[0] => SSS
[1] => Jannah Curtis
[2] => 104
[3] => 6/28/2014 14:29
)
)
[ITM] => Array
(
[105] => Array
(
[0] => ITM
[1] => Sassy Mica
[2] => 105
[3] => 6/18/2014 9:12
)
)
but I want an output that will result all her date/time record under the element [3].
Ex.
[ITM] => Array
(
[105] => Array
(
[0] => ITM
[1] => Sassy Mica
[2] => 105
[3] => 6/17/2014 9:12
=> 6/17/2014 20:43
=> 6/18/2014 9:12
)
)
While the time record is sorted.
You don't have a choice. You need to process them accordingly. The array format you desire is invalid, since they cannot share the same key, just create another dimension for the time. Example:
$data = array();
$handle = fopen('sample.csv', 'r');
while(!feof($handle)) {
$row = fgetcsv($handle, '4096');
if(!isset($data[$row[0]][$row[2]])) {
// simple assignment
$data[$row[0]][$row[2]] = array($row[0], $row[1], $row[2], array($row[3],));
} else {
// process
$data[$row[0]][$row[2]][3][] = $row[3]; // push it inside instead of assigning a new one
$temp = $data[$row[0]][$row[2]][3]; // temporary storage
$temp = array_map(function($var){
return strtotime($var); // convert to unix time
}, $temp);
sort($temp); // sort ascending
$data[$row[0]][$row[2]][3] = array_map(function($var){
return date('m/d/Y H:i', $var); // return to old format with sorted values
}, $temp);
}
}
array_shift($data); // remove the first (the header)
echo '<pre>';
print_r($data);
Should yield something like:
Array
(
[LMS] => Array
(
[102] => Array
(
[0] => LMS
[1] => Bach, Jerome
[2] => 102
[3] => Array
(
[0] => 6/30/2014 12:23
)
)
)
[MTS] => Array
(
[103] => Array
(
[0] => MTS
[1] => Lorvia, Christine
[2] => 103
[3] => Array
(
[0] => 6/16/2014 9:31
)
)
)
[SSS] => Array
(
[104] => Array
(
[0] => SSS
[1] => Jannah Curtis
[2] => 104
[3] => Array
(
[0] => 06/16/2014 08:45
[1] => 06/28/2014 14:29
)
)
)
[ITM] => Array
(
[105] => Array
(
[0] => ITM
[1] => Sassy Mica
[2] => 105
[3] => Array
(
[0] => 06/17/2014 09:12
[1] => 06/17/2014 20:43
[2] => 06/18/2014 09:12
)
)
)
)

PHP: Combining 2 Array into 1 array with a common index

username(
[0] => 'andrew';
[1] => 'teddy';
[2] => 'bear';
)
email(
[0] => 'andrew#andrew.com';
[1] => 'teddy#teddy.com';
[2] => 'bear#bear.com';
)
I got 2 Array coming in from post. I am processing this with PHP.
I would like to combine the array so it looks like this.
So I can use a loop on the array to insert a query on a database.
[1] => Array (
[0] => 'andrew';
[1] => 'andrew#andrew.com';
)
[2] => Array (
[0] => 'teddy';
[1] => 'teddy#teddy.com';
)
[3] => Array (
[0] => 'bear';
[1] => 'bear#bear.com';
)
Take a look at array_combine()
If that doesn't solve your problem, you can always just go with a simple loop:
foreach($usernameArray as $k=>$val)
{
if(array_key_exists($k, $emailArray))
{
$combinedArray[$k] = array($val, $emailArray[$k]);
}
}
You need something like:
$res = array ();
for($i=0;$i<count($username);$i++) {
$res[$i][0] = $username[$i];
$res[$i][1] = $email[$i];
}

Categories