I have an array of TLDs and prices, and now I want to be able to add a classification i.e. 'Australian','New Zealand','Industry' to the domains but I am having troubles adding the extra dimension.
The array I have is
$domains = array(
'.com.au' => '19.98',
'.melbourne' => '90.00',
'.academy' => '45.00',
'.accountants' => '120.00',
'.ac.nz' => '36.75');
$domains = array(
'.com.au' => array(
'country' => 'Australia',
'sector' => 'Industry',
'price' => '19.98'
),
);
Is this a beginning on what you're looking for ?
Is this code ok for you?
<?php
$domains = array(
'.com.au' => '19.98',
'.melbourne' => '90.00',
'.academy' => '45.00',
'.accountants' => '120.00',
'.ac.nz' => '36.75');
$newDomains = [];
foreach($domains as $key=>$value){
if($key == '.com.au'){
$newDomains[$key]['TLD'] = $value;
$newDomains[$key]['Industry'] = 'blabal';
}else{
$newDomains[$key] = $value;
}
}
echo '<pre>';
var_dump($newDomains);
echo '</pre>';
?>
Or even:
$domains = array(
'.com.au' => '19.98',
'.melbourne' => '90.00',
'.academy' => '45.00',
'.accountants' => '120.00',
'.ac.nz' => '36.75');
$industryArray = array(
'.com.au' => 'blabla'
);
$newDomains = [];
foreach($domains as $key=>$value){
if(isset($industryArray[$key])){
$newDomains[$key]['TLD'] = $value;
$newDomains[$key]['Industry'] = $industryArray[$key];
}else{
$newDomains[$key] = $value;
}
}
echo '<pre>';
var_dump($newDomains);
echo '</pre>';
The result is:
array(5) {
[".com.au"]=>
array(2) {
["TLD"]=>
string(5) "19.98"
["Industry"]=>
string(6) "blabla"
}
[".melbourne"]=>
string(5) "90.00"
[".academy"]=>
string(5) "45.00"
[".accountants"]=>
string(6) "120.00"
[".ac.nz"]=>
string(5) "36.75"
}
Related
I need to extract data from elements with keys that start with foo. from the below array:
[
'name' => 'Bar',
'location' => 'Baz',
'foo.2021-02-01' => '50000.00',
'foo.2021-03-01' => '50000.00',
'foo.2021-04-01' => '50000.00',
'foo.2021-05-01' => '',
]
After identifying qualifying keys, I need to create a new indexed array of associative rows using the date substring from the original keys like so:
[
['date' => '2021-02-01', 'value' => '50000.00'],
['date' => '2021-03-01', 'value' => '50000.00'],
['date' => '2021-04-01', 'value' => '50000.00'],
['date' => '2021-05-01', 'value' => ''],
]
I've been able to extract the keys like so:
$keys = array_keys($theData[0]);
foreach ( $keys as $key ) {
if ( preg_match( '/foo.*/', $key ) ) {
$line = explode('.', $key);
$item[]['name'] = $line[1];
}
}
but I'm losing the values.
I then tried looping through the array manually and rebuilding the desired outcome, but the keys will change so I don't know how future-proof that would be.
Is there a wildcard approach I can take to achieve this?
You almost had it:
<?php
$theData = [
'name' => 'Bar',
'location' => 'Baz',
'foo.2021-02-01' => '50000.00',
'foo.2021-03-01' => '50000.00',
'foo.2021-04-01' => '50000.00',
'foo.2021-05-01' => ''
];
$item = [];
// No need for array_keys(), foreach() can already do this
foreach( $theData as $key => $value )
{
// check if the key starts with foo.
// Regular expressions are heavy; if you'd like then substitute with:
// if ( substr( $key, 0, 4 ) === 'foo.' )
if ( preg_match( '/^foo\\./', $key ) )
{
// foo. is 4 chars long so substring from the fourth index till the end
$item[] = [
'date' => substr( $key, 4 ),
'value' => $value
];
}
}
var_dump( $item );
Output:
array(4) {
[0]=>
array(2) {
["date"]=>
string(10) "2021-02-01"
["value"]=>
string(8) "50000.00"
}
[1]=>
array(2) {
["date"]=>
string(10) "2021-03-01"
["value"]=>
string(8) "50000.00"
}
[2]=>
array(2) {
["date"]=>
string(10) "2021-04-01"
["value"]=>
string(8) "50000.00"
}
[3]=>
array(2) {
["date"]=>
string(10) "2021-05-01"
["value"]=>
string(0) ""
}
}
A simple loop, checking for the key starting with foo. and then a little code to replace foo. in the key with nothing will do the trick
If you have PHP8 or >
$arr = [
'name' => 'Bar',
'location' => 'Baz',
'foo.2021-02-01' => '50000.00',
'foo.2021-03-01' => '50000.00',
'foo.2021-04-01' => '50000.00',
'foo.2021-05-01' => ''
];
$new = [];
foreach ($arr as $k => $v){
if ( str_starts_with( $k , 'foo.' ) ) {
$new[] = ['date' => str_replace('foo.', '', $k), 'value' => $v];
}
}
print_r($new);
RESULT
Array
(
[0] => Array
([date] => 2021-02-01, [value] => 50000.00)
[1] => Array
([date] => 2021-03-01, [value] => 50000.00)
[2] => Array
([date] => 2021-04-01, [value] => 50000.00)
[3] => Array
([date] => 2021-05-01, [value] => )
)
Alternatively, for PHP versions prior to PHP8
$new = [];
foreach ($arr as $k => $v){
if ( strpos( $k , 'foo.') !== FALSE && strpos( $k , 'foo.') == 0 ) {
$new[] = ['date' => str_replace('foo.', '', $k), 'value' => $v];
}
}
Using str_starts_with and explode
$arr = [];
foreach ($theData as $k => $v){
if (str_starts_with($k, "foo."))
$arr[] = ["date" => explode(".", $k)[1], "value" => $v];
}
var_dump($arr);
sscanf() is an ideal function to call which will both check for qualifying strings and extract the desired trailing date value. It doesn't use regex, but it does require a placeholder %s to target the date substring. If a given string doesn't qualify, no element is pushed into the result array.
Code: (Demo) (without compact())
$result = [];
foreach ($array as $key => $value) {
if (sscanf($key, 'foo.%s', $date)) {
// $result[] = ['date' => $date, 'value' => $value];
$result[] = compact(['date', 'value']);
}
}
var_export($result);
If you remove the optional technique of using compact(), this solution makes fewer function calls than all other answers on this page.
I would probably only use regex if I wanted to strengthen the validation for qualifying key strings. (Demo)
$result = [];
foreach ($array as $key => $value) {
if (preg_match('~^foo\.\K\d{4}-\d{2}-\d{2}$~', $key, $m)) {
$result[] = ['date' => $m[0], 'value' => $value];
}
}
var_export($result);
I have a project with a list of filters having some given options. Each option should contain the next parent's children. So basically the entire array must be parsed from the end to the beginning of it, but I am not sure how would be the best to do it in PHP.
The initial list looks like this:
Model
- Paper basic
- Paper standard
Format
- 12x7x3 cm
- 15x15x5 cm
Color
- White
- Blue
- Red
Visually, what I need to do is this:
<table border="1" style="width: 100%;">
<tr><td>Paper basic</td><td>12x7x3 cm</td><td>White</td></tr>
<tr><td></td><td></td><td>Blue</td></tr>
<tr><td></td><td></td><td>Red</td></tr>
<tr><td></td><td>15x15x5 cm</td><td>White</td></tr>
<tr><td></td><td></td><td>Blue</td></tr>
<tr><td></td><td></td><td>Red</td></tr>
<tr><td>Paper standard</td><td>12x7x3 cm</td><td>White</td></tr>
<tr><td></td><td></td><td>Blue</td></tr>
<tr><td></td><td></td><td>Red</td></tr>
<tr><td></td><td>15x15x5 cm</td><td>White</td></tr>
<tr><td></td><td></td><td>Blue</td></tr>
<tr><td></td><td></td><td>Red</td></tr>
</table>
PHP Array:
$parents = array();
$parent = array();
$parent['id'] = 1;
$parent['name'] = 'Model';
$parent['slug'] = 'model';
$parent['type'] = 'filter';
$parent['children'] = array();
$child = array();
$child['id'] = 1;
$child['name'] = 'Paper basic';
$child['slug'] = 'paper-basic';
$child['type'] = 'option';
array_push($parent['children'], $child);
$child = array();
$child['id'] = 2;
$child['name'] = 'Paper standard';
$child['slug'] = 'paper-standard';
$child['type'] = 'option';
array_push($parent['children'], $child);
array_push($parents, $parent);
$parent = array();
$parent['id'] = 2;
$parent['name'] = 'Format';
$parent['slug'] = 'format';
$parent['type'] = 'filter';
$parent['children'] = array();
$child = array();
$child['id'] = 3;
$child['name'] = '12x7x3 cm';
$child['slug'] = '12x7x3-cm';
$child['type'] = 'option';
array_push($parent['children'], $child);
$child = array();
$child['id'] = 4;
$child['name'] = '15x15x5 cm';
$child['slug'] = '15x15x5-cm';
$child['type'] = 'option';
array_push($parent['children'], $child);
array_push($parents, $parent);
$parent = array();
$parent['id'] = 3;
$parent['name'] = 'Color';
$parent['slug'] = 'color';
$parent['type'] = 'filter';
$parent['children'] = array();
$child = array();
$child['id'] = 5;
$child['name'] = 'White';
$child['slug'] = 'white';
$child['type'] = 'option';
array_push($parent['children'], $child);
$child = array();
$child['id'] = 6;
$child['name'] = 'Blue';
$child['slug'] = 'blue';
$child['type'] = 'option';
array_push($parent['children'], $child);
$child = array();
$child['id'] = 7;
$child['name'] = 'Red';
$child['slug'] = 'red';
$child['type'] = 'option';
array_push($parent['children'], $child);
array_push($parents, $parent);
echo '<pre>';
print_r($parents);
echo '</pre>';
array result:
Array
(
[0] => Array
(
[id] => 1
[name] => Model
[slug] => model
[type] => filter
[children] => Array
(
[0] => Array
(
[id] => 1
[name] => Paper basic
[slug] => paper-basic
[type] => option
)
[1] => Array
(
[id] => 2
[name] => Paper standard
[slug] => paper-standard
[type] => option
)
)
)
[1] => Array
(
[id] => 2
[name] => Format
[slug] => format
[type] => filter
[children] => Array
(
[0] => Array
(
[id] => 3
[name] => 12x7x3 cm
[slug] => 12x7x3-cm
[type] => option
)
[1] => Array
(
[id] => 4
[name] => 15x15x5 cm
[slug] => 15x15x5-cm
[type] => option
)
)
)
[2] => Array
(
[id] => 3
[name] => Color
[slug] => color
[type] => filter
[children] => Array
(
[0] => Array
(
[id] => 5
[name] => White
[slug] => white
[type] => option
)
[1] => Array
(
[id] => 6
[name] => Blue
[slug] => blue
[type] => option
)
[2] => Array
(
[id] => 7
[name] => Red
[slug] => red
[type] => option
)
)
)
)
I tried something like this, but it's only working for the next items but the next items should contain the children of their next one:
for ($j = 0; $j <= count($parents); $j++)
{
if (isset($parents[$j+1]['children']))
{
array_push($parents[$j]['children'], $parents[$j+1]['children']);
}
}
I come up with a solution without the need of nested loop for all parents array. Just nested loop for cells in rows and work for as many parents as you can have. Make use of modulo to determine the content of each row.
$total_rows = 1;
foreach($parents as $parent)
{
$total_rows *= count($parent["children"]);
}
$rows = '';
for($row = 0;$row < $total_rows;$row++)
{
$cells = '';
$count_product = 1;
for($col = count($parents) - 1;$col >= 0;$col--)
{
$parent = $parents[$col];
$cells = '<td>' . ((($row % $count_product) == 0) ? $parent["children"][($row/$count_product)%count($parent["children"])]["name"] : "") . '</td>' . $cells;
$count_product *= count($parent["children"]);
}
$rows .= '<tr>'.$cells.'<tr>';
}
echo '<table>'.$rows.'</table>';
Here's some (til now ugly) code, that does what you need:
echo "<table border=1>";
foreach($parents[0]['children'] as $model) {
echo "<tr>";
echo "<td>".$model['name']."</td>";
$firstFormatDone=false;
foreach($parents[1]['children'] as $format) {
if($firstFormatDone) {
echo "<tr><td></td>";
}
echo "<td>".$format['name']."</td>";
$firstFormatDone=true;
$firstColorDone=false;
foreach($parents[2]['children'] as $color) {
if($firstColorDone) {
echo "<tr><td></td><td></td>";
}
echo "<td>".$color['name']."</td>";
$firstColorDone=true;
echo "<tr>";
}
}
}
echo "</table>";
As you can see, there's a pattern that should be put into a recoursive function (because now you said, that the amount of hierarchies isn't fixed)
Personally I do not like to have a couple of foreaches within each other.
Perhaps you are looking for this: If it is not, please let me know.
$parents = [];
$parent = [
'id' => 1,
'name' => 'Model',
'slug' => 'model',
'type' => 'filter',
'children' => [],
];
$child = [
'id' => 1,
'name' => 'Paper basic',
'slug' => 'paper-basic',
'type' => 'option',
];
array_push($parent['children'], $child);
$child = [
'id' => 2,
'name' => 'Paper standard',
'slug' => 'paper-standard',
'type' => 'option',
];
array_push($parent['children'], $child);
array_push($parents, $parent);
$parent = [
'id' => 2,
'name' => 'Format',
'slug' => 'format',
'type' => 'filter',
'children' => [],
];
$child = [
'id' => 3,
'name' => '12x7x3 cm',
'slug' => '12x7x3-cm',
'type' => 'option',
];
array_push($parent['children'], $child);
$child = [
'id' => 4,
'name' => '15x15x5 cm',
'slug' => '15x15x5-cm',
'type' => 'option',
];
array_push($parent['children'], $child);
array_push($parents, $parent);
$parent = [
'id' => 3,
'name' => 'Color',
'slug' => 'color',
'type' => 'filter',
'children' => [],
];
$child = [
'id' => 5,
'name' => 'White',
'slug' => 'white',
'type' => 'option',
];
array_push($parent['children'], $child);
$child = [
'id' => 6,
'name' => 'Blue',
'slug' => 'blue',
'type' => 'option',
];
array_push($parent['children'], $child);
$child = [
'id' => 6,
'name' => 'Red',
'slug' => 'red',
'type' => 'option',
];
array_push($parent['children'], $child);
array_push($parents, $parent);
foreach ($parents as $parent)
{
foreach ($parent as $child)
{
if (is_array($child))
{
foreach ($child as $result)
{
echo '<pre>';
var_dump($result);
echo '</pre>';
}
}
}
}
Outcome:
array(4) {
["id"]=>
int(1)
["name"]=>
string(11) "Paper basic"
["slug"]=>
string(11) "paper-basic"
["type"]=>
string(6) "option"
}
array(4) {
["id"]=>
int(2)
["name"]=>
string(14) "Paper standard"
["slug"]=>
string(14) "paper-standard"
["type"]=>
string(6) "option"
}
array(4) {
["id"]=>
int(3)
["name"]=>
string(9) "12x7x3 cm"
["slug"]=>
string(9) "12x7x3-cm"
["type"]=>
string(6) "option"
}
array(4) {
["id"]=>
int(4)
["name"]=>
string(10) "15x15x5 cm"
["slug"]=>
string(10) "15x15x5-cm"
["type"]=>
string(6) "option"
}
array(4) {
["id"]=>
int(5)
["name"]=>
string(5) "White"
["slug"]=>
string(5) "white"
["type"]=>
string(6) "option"
}
array(4) {
["id"]=>
int(6)
["name"]=>
string(4) "Blue"
["slug"]=>
string(4) "blue"
["type"]=>
string(6) "option"
}
array(4) {
["id"]=>
int(6)
["name"]=>
string(3) "Red"
["slug"]=>
string(3) "red"
["type"]=>
string(6) "option"
}
Related to your HTML. You could do this:
foreach ($parents as $parent)
{
foreach ($parent as $child)
{
if (is_array($child))
{
foreach ($child as $result): ?>
<tr>
<td><?php echo $result['name'] ;?></td>
<td><?php echo $result['slug'] ;?></td>
<td><?php echo $result['type'] ;?></td>
</tr>
<?php endforeach;
}
}
}
Documentation:
http://php.net/manual/en/control-structures.foreach.php // foreach
http://php.net/manual/en/function.is-array.php // is_array
I have been working on this for long and I got it working as I want but I think there is simpler solution.
So far I got this:
$menu = array(
0 => (object) array(
'cat_id' => 1,
'cat_parent' => 0,
'cat_name' => 'domov',
'cat_href' => 'domov',
'cat_subcategories' => array()
),
1 => (object) array(
'cat_id' => 2,
'cat_parent' => 0,
'cat_name' => 'clanky',
'cat_href' => 'clanky',
'cat_subcategories' => array(
0 => (object) array(
'cat_id' => 9,
'cat_parent' => 2,
'cat_name' => 'apple clanky',
'cat_href' => 'apple',
'cat_subcategories' => array(
0 => (object) array(
'cat_id' => 11,
'cat_parent' => 9,
'cat_name' => 'iphone clanky',
'cat_href' => 'iphone',
'cat_subcategories' => array()
),
1 => (object) array(
'cat_id' => 12,
'cat_parent' => 9,
'cat_name' => 'macbook clanky',
'cat_href' => 'macbook',
'cat_subcategories' => array()
)
)
),
1 => (object) array(
'cat_id' => 10,
'cat_parent' => 2,
'cat_name' => 'microsoft clanky',
'cat_href' => 'microsoft',
'cat_subcategories' => array()
),
)
),
2 => (object) array(
'cat_id' => 3,
'cat_parent' => 0,
'cat_name' => 'produkty',
'cat_href' => 'produkty',
'cat_subcategories' => array()
),
3 => (object) array(
'cat_id' => 4,
'cat_parent' => 0,
'cat_name' => 'vyrobcovia',
'cat_href' => 'vyrobcovia',
'cat_subcategories' =>
array(
0 => (object) array(
'cat_id' => 5,
'cat_parent' => 4,
'cat_name' => 'apple',
'cat_href' => 'apple',
'cat_subcategories' => array(
0 => (object) array(
'cat_id' => 7,
'cat_parent' => 5,
'cat_name' => 'iphone',
'cat_href' => 'iphone',
'cat_subcategories' => array()
),
1 => (object) array(
'cat_id' => 8,
'cat_parent' => 5,
'cat_name' => 'macbook',
'cat_href' => 'macbook',
'cat_subcategories' => array()
)
)
),
1 => (object) array(
'cat_id' => 6,
'cat_parent' => 4,
'cat_name' => 'microsoft',
'cat_href' => 'microsoft',
'cat_subcategories' => array()
),
)
),
);
function generate_menu($menu, $level = 1, $tmp_array = array())
{
foreach($menu as $key => $c)
{
$menu_item = get_menu_elements($c, $level);
$tmp_array = array_replace_recursive($tmp_array, $menu_item);
foreach($c->cat_subcategories as $_key => $_c)
{
$menu_item = get_menu_elements($_c, $level+1);
$tmp_array = array_replace_recursive($tmp_array, $menu_item);
foreach($_c->cat_subcategories as $__key => $__c)
{
$menu_item = get_menu_elements($__c, $level+2);
$tmp_array = array_replace_recursive($tmp_array, $menu_item);
}
}
}
return $tmp_array;
}
function get_menu_elements($c, $level = 1)
{
$level_var = "level_".($level);
$output = array(
$level_var => array()
);
$output[$level_var][$c->cat_id] = array(
'parent' => $c->cat_parent,
'html' => $c->cat_name
);
return $output;
}
In the end I should have multidimensional array with:
array(3) {
["level_1"]=>
array(4) {
[1]=>
array(2) {
["parent"]=>
int(0)
["html"]=>
string(5) "domov"
}
[2]=>
array(2) {
["parent"]=>
int(0)
["html"]=>
string(6) "clanky"
}
[3]=>
array(2) {
["parent"]=>
int(0)
["html"]=>
string(8) "produkty"
}
[4]=>
array(2) {
["parent"]=>
int(0)
["html"]=>
string(10) "vyrobcovia"
}
}
["level_2"]=>
array(4) {
[9]=>
array(2) {
["parent"]=>
int(2)
["html"]=>
string(12) "apple clanky"
}
[10]=>
array(2) {
["parent"]=>
int(2)
["html"]=>
string(16) "microsoft clanky"
}
[5]=>
array(2) {
["parent"]=>
int(4)
["html"]=>
string(5) "apple"
}
[6]=>
array(2) {
["parent"]=>
int(4)
["html"]=>
string(9) "microsoft"
}
}
["level_3"]=>
array(4) {
[11]=>
array(2) {
["parent"]=>
int(9)
["html"]=>
string(13) "iphone clanky"
}
[12]=>
array(2) {
["parent"]=>
int(9)
["html"]=>
string(14) "macbook clanky"
}
[7]=>
array(2) {
["parent"]=>
int(5)
["html"]=>
string(6) "iphone"
}
[8]=>
array(2) {
["parent"]=>
int(5)
["html"]=>
string(7) "macbook"
}
}
}
I want to print multi-level menu recursion. I got it working with this code but in function generate_menu I had to use 3 foreach loops for cat_subcategories. When I used recursion like this:
function generate_menu($menu, $level = 1, $tmp_array = array())
{
foreach($menu as $key => $c)
{
$menu_item = get_menu_elements($c, $level);
$tmp_array = array_replace_recursive($tmp_array, $menu_item);
if (!empty($c->cat_subcategories)) generate_menu($c->cat_subcategories, $level + 1, $tmp_array);
}
return $tmp_array;
}
i got only 'level_1' in output
The output which I am getting now is the same I want I just want to simplify the multiple foreach()
etc. Basicaly
level_1 - first loop of $menu
level_2 - loop of cat_subcategories
level_3 - loop of cat_subcategories for cat_subcategories item
You essentially need to perform a breadth-first traversal in your menu tree. For that I would suggest an iterative function instead of a recursive function, as the latter is more suitable for a depth-first traversal.
Here is the function:
function generate_menu($menu) {
$result = [];
$level = 1;
while (count($menu)) {
$queue = [];
$items = [];
foreach($menu as $cat) {
$items[$cat->cat_id] = [
"parent" => $cat->cat_parent,
"html" => $cat->cat_name
];
$queue = array_merge($queue, $cat->cat_subcategories);
}
$result["level_$level"] = $items;
$level++;
$menu = $queue;
}
return $result;
}
See it run on eval.in.
As this function traverses the top level of the tree, it collects all the children into $queue. Once the top level has been translated into the output (in the level_1 key), that queue will have all second level entries. This then is moved to $menu, and the process is repeated, but now with level_2. This continues until a level is reached where no more entries are found.
I've made some time ago this function. It could help you.
I've got an array where $arr[actual item][parental id]
function getChild($id, $result, $count) {
for( $i = 0; $i < sizeof($result); $i ++) {
if( $result[$i][2] == $id) {
getChild($result[$i][0], $result, $count + 1);
}
}
}
EDIT
In my case, where all the items in the one-dimensional array, but with the hierarchy of n-dimensional array depended on the parental id.
CODE TO PRINT MENU
This code will work for N-number of hierarchy
static function printAllCategoriesAsCheckbox($arr, $d=0){
$htmlString = "";
if (is_array($arr)){
foreach($arr as $category){
$htmlString = $htmlString . '<div><input style="margin-left: ' . 30*$d . 'px;" value="'.$category->id.'" class="category-input" type="checkbox" name="category_id[]">';
$htmlString = $htmlString . ' <label for="category_'.$category->id.'">' . $category->name . '</label></div>';
if (is_array($category['childs'])){
$htmlString = $htmlString . ItemCategory::printAllCategoriesAsCheckbox($category['childs'], $d+1);
}
}
}
return $htmlString;
}
This is my code! I'm trying to get from $test array and at end show me like $final array! tnx to help! code should get domain name and show how many time a userid has been repeated!
<?php
$test = array(
array(
'id' => 1,
'domain' => 'google.com',
'userid' => "123"
),
array(
'id' => 2,
'domain' => 'google.com',
'userid' => "456"enter
),
array(
'id' => 3,
'domain' => 'yahoo.com',
'userid' => "456"
),
array(
'id' => 4,
'domain' => 'google.com',
'userid' => "123"
),
array(
'id' => 5,
'domain' => 'bing.com',
'userid' => "128"
)
);
$i=0;
foreach ($test as $items) {
$domains[$i]=$items['domain'];
$userid[$i]=$items['userid'];
$i++;
}
$domain=array_unique($domains);
$domain1 = array_values($domain);
print_r($domain);
echo '<pre>';
print_r($test);
echo '</pre>';
echo '<hr>';
$d=1; $tedad = 1;
while($d<=4){
$b = 1 ; $c=0;
while ($test[$b]['id']<=4) {
if($test[$b]['domain'] == $domain1[$d])
{
$temp = $test[$b]['userid'];
if(/*$test[$b]['userid'] !==*/ !array_key_exists($domain1[$d] ['userid']){
//$domain1[$d] = array($test[$b]['userid'] => $tedad) ;
echo $temp;
$end = array(
$domain1[$d] => array(
$temp => $tedad )
);
}
else{
$end[$d][$test[$b]['userid']]= $end[$d][$test[$b]['userid']] +1;
}
}
else{
$b++;
}
}
$d++; $tedad = 1;
}
print_r($end);
$final = array(
"google.com" => array(
"123" => 2,
"456" => 1
),
"yahoo.com" => array(
"456" => 1
)
);
echo '<pre>';
print_r($final);
echo '</pre>';
echo '<hr>';
?>
Try:
$result = array();
foreach($test as $t)
{
if(isset($result[$t['domain']][$t['userid']]))
$result[$t['domain']][$t['userid']]++;
else $result[$t['domain']][$t['userid']] = 1;
}
If you don't want to include some domains use like:
$result = array();
foreach($test as $t)
{
if($t['domain'] == 'bing.com')
continue;
if(isset($result[$t['domain']][$t['userid']]))
$result[$t['domain']][$t['userid']]++;
else $result[$t['domain']][$t['userid']] = 1;
}
<?php
$test = array(
array(
'id' => 1,
'domain' => 'google.com',
'userid' => "123"
),
array(
'id' => 2,
'domain' => 'google.com',
'userid' => "456"
),
array(
'id' => 3,
'domain' => 'yahoo.com',
'userid' => "456"
),
array(
'id' => 4,
'domain' => 'google.com',
'userid' => "123"
)
);
echo '<pre>';
print_r($test);
echo '</pre>';
echo '<hr>';
$domains = array();
foreach ($test as $value) {
if (!in_array($value['domain'], $domains))
$domains[$value['domain']] = array();
}
foreach ($domains as $key => $domain) {
foreach ($test as $item) {
if ($key == $item['domain']) {
if (isset($domains[$key][$item['userid']])) {
$domains[$key][$item['userid']] = $domains[$key] [$item['userid']]+1;
} else {
$domains[$key][$item['userid']] = 1;
}
}
}
}
echo '<pre>';
print_r($domains);
echo '</pre>';
echo '<hr>';
// $final = array(
// "google.com" => array(
// "123" => 2,
// "456" => 1
// ),
// "yahoo.com" => array(
// "456" => 1
// )
// );
// echo '<pre>';
// print_r($final);
// echo '</pre>';
// echo '<hr>';
?>
Example i have this array:
$ar = array(
"1.00" => array("value0"," very bad"),
"1.49" => array("value1","bad"),
"2.00" => array("value2","not bad"),
"2.49" => array("value3","normal"),
"3.00" => array("value4","good"),
"3.49" => array("value5","very good")
);
I want to check if $val is under 1.00 the $result is array("value0"," very bad"). if between range 1.00 - 1,49 the result is array("value1","bad"), etc.
Anyone can help me?
Here is a hint :
<?php
$ar = array(
"1.00" => array("value0"," very bad"),
"1.49" => array("value1","bad"),
"2.00" => array("value2","not bad"),
"2.49" => array("value3","normal"),
"3.00" => array("value4","good"),
"3.49" => array("value5","very good")
);
$input = 1.2;
foreach($ar as $key=>$text)
{
if($input < floatval($key))
{
echo $text[0].' => '.$text[1];
break;
}
}
?>
$val = '2.15';
$val_data = ['value2','not bad'];
$data = array(
"1.00" => array("value0"," very bad"),
"1.49" => array("value1","bad"),
"2.00" => array("value2","not bad"),
"2.49" => array("value3","normal"),
"3.00" => array("value4","good"),
"3.49" => array("value5","very good")
);
$_fkey = array_keys($data)[0];
foreach($data as $key => $value){
if($key > $_fkey && $key < $val){$_fkey = $key;}
}
echo "$val, $val_data\n";
echo "=> $f_key, " . $data[$f_key] . "\n";