creating array through class object - php

I want to create a multidimensional array through class object. Don't know the syntax. Kindly guide me regarding that.
class gridcell {
var $place;
var $latitude;
$spatial=array();
$temporal=array();
$taxi=array();
$raiwind = new gridell();
$raiwind->place("raiwind");
$raiwind->latitude("31.4279");
Now I wanted to put the following array in the $raiwind->$spatial
What would be its synatx. Different objects have different elements in $spatial array.
spatial(
array(
array(
'lda',
'31.4104',
3
),
array(
'ali',
'31.3998',
3
),
array(
'multan',
'31.4675',
10
)
)
);

Simple as this:
$raiwind->spatial[] = array(
array('lda','31.4104',3),
array('ali','31.3998',3),
array('multan','31.4675',10)
);

Related

Searching through arrays and Creating a custom array in PHP

I having an issue merging arrays. I have 3 arrays that show particular data. I need a way to search through all three arrays and if the record matches the date I need to store it in a new custom array.
I have 3 arrays that need to merge them using the date
The arrays are as follows.
// ARRAY 1
$transactions = array(
array(
"date"=>"2021-03-01",
"trans_count"=>100,
"trans_amount"=>5300
),
array(
"date"=>"2021-03-02",
"trans_count"=>95,
"trans_amount"=>5035
),
array(
"date"=>"2021-03-03",
"trans_count"=>105,
"trans_amount"=>5565
)
);
// ARRAY 2
$overdrafts = array(
array(
"date"=>"2021-03-01",
"od_amount"=>500
),
array(
"date"=>"2021-03-02",
"od_amount"=>1000
),
);
// ARRAY 3
$payouts= array(
array(
"date"=>"2021-03-02",
"amount"=>2300
)
);
I tried to write a function but I got stuck. The end goal is to collect all records from all 3 arrays and coming up with one array.
the expected result should look like this.
$merged_arr = array(
array(
"date"=>"2021-03-01",
"type"=>"transactions",
"trans_count"=>100,
"trans_amount"=>5300
),
array(
"date"=>"2021-03-01",
"type"=>"overdrafts",
"od_amount"=>500,
),
array(
"date"=>"2021-03-02",
"type"=>"transactions",
"trans_count"=>95,
"trans_amount"=>5035
),
array(
"date"=>"2021-03-02",
"type"=>"overdrafts",
"od_amount"=>1000
),
array(
"date"=>"2021-03-02",
"type"=>"payout",
"od_amount"=>2300
),
);
Not 100% sure of how to add the type for each of the different types automatically in a nice way, I'd suggest just adding the types directly on the initial arrays, or looping through each before merging them together.
As for the merging and sorting it's pretty easy and can be done with built-in functionality.
$merged_arr = array_merge($transactions, $overdrafts, $payouts);
usort($merged_arr, function ($left, $right) {
return new DateTime($left['date']) <=> new DateTime($right['date']);
});
// Demo merged array.
print "<pre>";
var_export($merged_arr);
The sort function will work with PHP 7 and up.

Defining array in array in array - php

I am trying to create the following array dynamically:
$aSettings = array( "text"=>
array( "icon_type" =>
array(
"name"=>"icon_type",
"method"=>"dropdown",
"option"=>"",
"default"=>""
),
"column" =>
array(
"name"=>"column_count",
"method"=>"dropdown",
"default"=>"1"
)
)
)
I am not sure how to declare the array into the array.
I have the following example code:
$aSettings=array();
$aSetting_type['text']=array();
$aSetting_name['icon_type']=array();
$aSetting_name['column']=array();
$aSetting_values1=array('name'=>'icon_type','method'=>'dropdown','option'=>'','default'=>'');
$aSetting_values2=array('name'=>'column_count','method'=>'dropdown','default'=>1);
I guess I am overlooking something very simple, but how do I put all these arrays into each other?
I want to be able to call a value from the array as:
$aSettings['text']['column']['name'];
Any ideas?
You could do:
$aSettings['text']['icon_type'] = $aSetting_values1;
$aSettings['text']['column'] = $aSetting_values2;
If you need it more dynamic, you could use variables like so:
$type = 'text';
$name1 = 'icon_type';
$aSettings[$type][$name1] = $aSetting_values1;
Collect the array is reverse lower to higher order, it would be easy to collect without confusions, for example
$icon_type = array(
"name"=>"icon_type",
"method"=>"dropdown",
"option"=>"",
"default"=>""
);
$column = array(
"name"=>"column_count",
"method"=>"dropdown",
"default"=>"1"
);
$text = array(
"icon_type" => $icon_type,
"column" => $column
);
$aSettings = array(
"text"=> $text
);
Once you collect array like this you can easily access any element in the array i.e. echo $aSettings['text']['column']['name'];

PHP map named array to another named array

I have the following code for generating a new array:
$languages = array_keys(['French'=>4, 'Spanish'=>2, 'German'=>6, 'Chinese'=>8]);
function generateLanguageRules($language)
{
return ["seatsAllocatedFor$language"=>"numeric|max:300"];
}
array_map('generateLanguageRules', $languages);
//Output:
array(
0 => array(
'seatsAllocatedForFrench' => 'numeric|max:300'
),
1 => array(
'seatsAllocatedForSpanish' => 'numeric|max:300'
),
2 => array(
'seatsAllocatedForGerman' => 'numeric|max:300'
),
3 => array(
'seatsAllocatedForChinese' => 'numeric|max:300'
)
)
I'm wondering if there is an easier way to output a flat array, instead of a nested one? I'm using Laravel. Are there maybe some helper functions that could do this?
UPDATE:
One possible Laravel specific solution:
$languages = array_keys(['French'=>4, 'Spanish'=>2, 'German'=>6, 'Chinese'=>8]);
$c = new Illuminate\Support\Collection($languages);
$c->map(function ($language){
return ["seatsAllocatedFor$language"=>"numeric|max:300"];
})->collapse()->toArray();
I dont know if laravel has a built-in method for that, (haven't used it yet). But alternatively, you could use RecursiveArrayIterator in conjunction to iterator_to_array() to flatten it and assign it. Consider this example:
$languages = array_keys(['French'=>4, 'Spanish'=>2, 'German'=>6, 'Chinese'=>8]);
function generateLanguageRules($language) {
return ["seatsAllocatedFor$language"=>"numeric|max:300"];
}
$data = array_map('generateLanguageRules', $languages);
$data = iterator_to_array(new RecursiveIteratorIterator(new RecursiveArrayIterator($data)));
echo "<pre>";
print_r($data);
echo "</pre>";
Sample Output:
Array
(
[seatsAllocatedForFrench] => numeric|max:300
[seatsAllocatedForSpanish] => numeric|max:300
[seatsAllocatedForGerman] => numeric|max:300
[seatsAllocatedForChinese] => numeric|max:300
)

What multi dimensional array needed to store 3 levels of information?

I'm working on a PHP script to hold a lot of information.
Lets try to explain our situation!
I have actually 33 different stations.
For each of that 33 stations I have 5 different categories.
And for each of that 33 stations with each 5 different categories i have 37 different values per category.
Do I need an 2d of 3d array for store this information in it ?
Thanks you!
Something like this will work, just add more data as needed:
$station_array =
array(
'station1' => array(
'cat1' => array ('val1','val2','val3'),
'cat2' => array ('val1','val2','val3'),
'cat3' => array ('val1','val2','val3')
),
'station2' => array (
'cat1' => array ('val1','val2','val3'),
'cat2' => array ('val1','val2','val3'),
'cat3' => array ('val1','val2','val3')
),
'station3' => array (
'cat1' => array ('val1','val2','val3'),
'cat2' => array ('val1','val2','val3'),
'cat3' => array ('val1','val2','val3')
)
);
Sounds like a job for a relational database!
But you're correct in your initial assumption. You will need a 3-dimensional array to hold your information because your data has 3 tiers: the stations, the categories, and the values.
A php array will be fine for this
$MyArray = array('Station1' => array('Category1' =>
array('Value1'=> 1000,'Value2'=> 1001),
'Category2' => array('Value1' => 2332)), etc...
'Station2' => array('Category1' =>
array('Value1'=> 1000,'Value2'=> 1001),
'Category2' => array('Value1' => 2332)), etc
etc
);
Once you pass more than two dimensions in an associative array, it's good to start considering using objects to store your information. Objects make it a lot easier to understand how things are organized, they can enforce validation restrictions on your data (make sure it's in the form it should be), and you can call functions on the data to manipulate it (instead of having random external functions manipulating your entire array). An example would be:
class CategoryValue {
var $val; // your value
function __construct($val) {
$this->val = $val;
}
}
class Category {
var $values = array(); // an array of CategoryValue objects
function addValue(CategoryValue $val) {
$this->values[] = $val;
}
}
class Station {
var $categories = array(); // an array of Category objects
function addCategory(Category $category) {
$this->categories[] = $category;
}
}
well, all depends on how you want to acheive, if you dont need to loop through the values, but you just want to store data and alway know whay you want to get, you could use hashes for that, the table would look like:
$data = array(
md5('StationX'.'CategoryY'.'PropertyZ') => 'ValueU',
md5('StationA'.'CategoryB'.'PropertyC') => 'ValueQ'
);
This way you can get the data right away and dont have to bother to check if you initialised CategoryB array for StationA when you want to add value for PropertyZ
php stores associative arrays as hastables technically
... thats all if you really insist on not using databases ;)

Associative array (PHP)

I do not know what does it mean when it says: (it is from hook_block_view code for drupal)
$block['content'] = array(
'#theme' => 'node_recent_block',
'#nodes' => $nodes,
);
I know $block['content'] is an associative array, also I know that $node is Full node object, Contains data that may not be safe.But about #theme, #nodes and 'node_recent_block.
Can someone please tell me what do they mean.
I searched a lot but I did not find out what does it mean when there is a # before name of a key.
Thank you
$block is an associative array, in which the element "content" is also an associative array.
Another way to define this would be:
$block = array(
'content' => array(
'#theme' => 'node_recent_block',
'#nodes' => $nodes,
),
);
Inside an associative array => is an assignment.
'content' is an array where '#theme' = 'node_recent_block' and '#nodes' = $nodes
Edit:
You could also assign the values like this:
$block['content']['#theme'] = 'node_recent_block';
$block['content']['#nodes'] = $nodes;

Categories