Conversion of a two-dimensional array to three-dimensional array - php

Please, give me advice. I have a directory of companies. How to group the names of companies by the first letter. On example: A - Adidas,ASUS... B - BMW,Bentley etc
Array of companies:
array() {
[0]=> array(10) { ["id"]=> "1" ["name"]=> "Adidas"}
    [1]=> array(10) { ["id"]=> "2" ["name"]=> "ASUS"}
[2]=> array(10) { ["id"]=> "3" ["name"]=> "BMW"}
    [3]=> array(10) { ["id"]=> "4" ["name"]=> "Bentley"}
}
I want to get:
array() {
  ['A']=> array {[0]=> array(10) { ["id"]=> "1" ["name"]=> "Adidas"} , [1]=> array(10) { ["id"]=> "2" ["name"]=> "ASUS"}}
  ['B']=> array {[2]=> array(10) { ["id"]=> "3" ["name"]=> "BMW"},[3]=> array(10) { ["id"]=> "4" ["name"]=> "Bentley"}}
}

Just loop over the array and add the sub-arrays to a new array with the key the first character of $entry["name"]
$newArray = array_keys(range('A', 'Z')); // ensure a filled array
foreach ($data as $entry) {
// & ~" " is a shorthand for transforming to uppercase
$newArray[$entry["name"][0] & ~" ")]][] = $entry; // add $entry to $newArray
}

Related

Foreach through multidimensional array with dynamic/unknown dimensions

I'm trying to loop through a multidimensional array with foreach but sometimes there's 5 dimensions and sometimes there's 2, but I need to foreach every array. Here is an example:
array(16) {
["id"]=>
string(2) "1"
["name"]=>
string(1) "Bob"
["job"]=>
array(2) {
[0]=>
string(8) "software"
[1]=>
string(7) "plumber"
}
["kids"]=>
array(2) {
[1]=>
array(2) {
[0]=>
string(4) "Jane"
[1]=>
string(4) "girl"
}
[2]=>
array(2) {
[0]=>
string(3) "Sam"
[1]=>
string(4) "boy"
[2] => array(2) {
[0]=>
string(3) "123"
[1]=>
string(11) "Main Street"
}
}
}
}
you get the point.... but imagine if I had a dimension of 10 in the array. How can I dynamically loop through them and do trim() to each value in the whole array?
Here is what I have so far:
foreach ($array as $key => $value) {
$array[$key] = trim($value);
}
but I need it to go deeper into an array if there is an array and perform trim to all values in my $array.

Transform Static Array into a generated by foreach loop

Im a bit lost with this array here, and i need some "light" if you could.
My situation is: change some static data from a array to one generated by an foreach.
The static php code example is the following:
$p->data = array(
array(
array("A",148), //value_1 for letter A
array("B",238), //value_1 for letter B
array("C",151) //value_1 for letter C
),
array(
array("A",238), //value_2 for letter A
array("B",338), //value_2 for letter B
array("C",285) //value_2 for letter C
),
array(
array("A",278), //value_3 for letter A
array("B",138), //value_3 for letter B
array("C",205) //value_3 for letter C
)
);
My problem is: when i try to generate the same result with a foreach, i'm doing it wrong.. i already tried nested foreach's and get nothing (or doing it wrong too), but this is the closest I reach from the result i need:
foreach($result as $reg){
$p->data = array(
array(
array($reg['letters'],$reg['value_1'])
),
array(
array($reg['letters'],$reg['value_2'])
),
array(
array($reg['letters'],$reg['value_3'])
)
);
};
Instead, i get only the first Letter with the right values. If i use "$p->data[]" to show the other letters, it doesn't work.
The var_dump() i get from the code above is the following:
array(3) {
[0]=> array(1) {
[0]=> array(2) {
[0]=> string(1) "A"
[1]=> int(148) }
}
[1]=> array(1) {
[0]=> array(2) {
[0]=> string(1) "A"
[1]=> int(238) }
}
[2]=> array(1) {
[0]=> array(2) {
[0]=> string(1) "A"
[1]=> int(278) }
}
}
As you see, only the first variable 'letter' comes from the array. I need the others, like this:
array(3) {
[0]=> array(3) {
[0]=> array(2) {
[0]=> string(1) "A"
[1]=> int(148)
}
[1]=> array(2) {
[0]=> string(1) "B"
[1]=> int(238)
}
[2]=> array(2) {
[0]=> string(1) "C"
[1]=> int(151)
}
}
[1]=> array(3) {
[0]=> array(2) {
[0]=> string(1) "A"
[1]=> int(238)
}
[1]=> array(2) {
[0]=> string(1) "B"
[1]=> int(338)
}
[2]=> array(2) {
[0]=> string(1) "C"
[1]=> int(285)
}
}
[2]=> array(3) {
[0]=> array(2) {
[0]=> string(1) "A"
[1]=> int(278)
}
[1]=> array(2) {
[0]=> string(1) "B"
[1]=> int(138)
}
[2]=> array(2) {
[0]=> string(1) "C"
[1]=> int(205)
}
}
}
Explaing the code: in the foreach im using.. im getting the values from a database. Each variable "Letter" has 3 variable "Values". What im trying to do is show one same variable Value for each Letter separated on each array.
Can someone point me to the right direction? Thank you all o/
what is $reg['letters']? where does $result come from?
you will need an array of letters, and you will need to iterate that. with a little more info i could provide an example.

PHP - Multidimensional array epanded from variable

I have the following array stored in $members
array(3) {
[0]=> array(2) {
["index"]=> string(1) "1"
["routePartitionName"]=> string(20) "US-555-foop-GWRoutes" }
[1]=> array(2) {
["index"]=> string(1) "2"
["routePartitionName"]=> string(27) "Cluster DN Presence Allowed" }
[2]=> array(2) {
["index"]=> string(1) "3"
["routePartitionName"]=> string(26) "Cluster DN Presence Denied" }
}
I'm trying to embed this into another array during a foreach loop. However it seems to evaluate the $members variable as text.
$programTags[] = array(
"name"=>"$cssname",
"description"=>"$cssdescription",
"members"=>"$members");
How can I expand the variable thus creating a multidimensional array?
Remove the quotes from $members variable, it convert it into string.
$programTags[] = array(
"name"=>"$cssname",
"description"=>"$cssdescription",
"members"=>$members);
using nested foreach
foreach($members as $array)
{
//$array is array variable .
foreach($array as $values)
{
//your code here
}
}
exapmple:
$member = array(3) {
[0]=> array(2) {
["index"]=> string(1) "1"
["routePartitionName"]=> string(20) "US-555-foop-GWRoutes" }
[1]=> array(2) {
["index"]=> string(1) "2"
["routePartitionName"]=> string(27) "Cluster DN Presence Allowed" }
[2]=> array(2) {
["index"]=> string(1) "3"
["routePartitionName"]=> string(26) "Cluster DN Presence Denied" }
}
applaying foreach then,
$array = array(2) {
["index"]=> string(1) "1"
["routePartitionName"]=> string(20) "US-555-foop-GWRoutes"
}

How to get a count each unique element in the general array

Good day.
Code:
array(4) {
[0]=> array(1) {
[0]=> array(3) {
[0]=> string(11) "art_7880" [1]=> string(1) "1" [2]=> int(2950)
}
[1]=> array(3) {
[0]=> string(8) "art_7880" [1]=> string(1) "1" [2]=> int(2955)
}
[2]=> array(3) {
[0]=> string(8) "art_7880" [1]=> string(1) "1" [2]=> int(1335)
}
[3]=> array(3) {
[0]=> string(8) "art_7883" [1]=> string(1) "1" [2]=> int(4335)
}
}
I get array unique elements:
$arr_uniq = array();
foreach ($all_array as $keys => $elms ) {
if(!in_array($elms[0], $arr_uniq)) {
$arr_uniq[] = $elms[0];
}
}
Tell me pleasse how to get a count each unique element in the general array?
result should been next:
art_7880 - 3
art_7883 - 1
Assuming $all_array is subarray of your main array in your var_dump snipett, the general idea is
$result = array();
foreach ($all_array as $elms)
$result[$elms[0]]++;
array_count_values()
http://php.net/array_count_values
You should be able to easily apply this function.

How do I sort this array

I grouped an array using the following script
$grouped_array = array();
foreach($ungrouped_array as $item) {
//group them by id
$grouped_array[$item['id']][] = $item;
}
Now this grouped array looks like this
array(3) {
[1]=>
array(2) {
[0]=>
array(1) {
["id"]=>
string(1) "1"
}
[1]=>
array(1) {
["id"]=>
string(1) "1"
}
}
[6]=>
array(1) {
[0]=>
array(1) {
["id"]=>
string(1) "6"
}
}
[2]=>
array(4) {
[0]=>
array(1) {
["id"]=>
string(1) "2"
}
[1]=>
array(2) {
["id"]=>
string(1) "2"
["sub"]=>
string(1) "1"
}
[2]=>
array(2) {
["id"]=>
string(1) "2"
["sub"]=>
string(1) "2"
}
[3]=>
array(1) {
["id"]=>
string(1) "2"
}
}
}
I have deleted the most part of the array to make it shorter but there is no [0] field in this grouped array
All array fields are given the name of [id]'s value. I have no problem with that, I just have to short it again by [ID]
any suggestion will be great.
This should work to get 1, 2, 6:
<?php
$grouped_array = array();
foreach($ungrouped_array as $item) {
$grouped_array[$item['id']][] = $item;
}
// sort by key.
ksort( $grouped_array, SORT_NUMERIC );
print_r( $grouped_array );

Categories