Foreach through multidimensional array with dynamic/unknown dimensions - php

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.

Related

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"
}

Extract value from an array of array in PHP

I have a form and I serialised it to send it to PHP (AJAX) :
var dataString = $('#form_filtre').serializeArray();
I would like to extract in PHP value where names is "ou" :
array(1) {
["form_serialize"]=>
array(6) {
[0]=>
array(2) {
["name"]=>
string(3) "ctr"
["value"]=>
string(6) "maison"
}
[1]=>
array(2) {
["name"]=>
string(6) "action"
["value"]=>
string(17) "readHomesLocation"
}
[2]=>
array(2) {
["name"]=>
string(2) "ou"
["value"]=>
string(1) "8"
}
[3]=>
array(2) {
["name"]=>
string(2) "ou"
["value"]=>
string(1) "6"
}
[4]=>
array(2) {
["name"]=>
string(5) "quand"
["value"]=>
string(0) ""
}
[5]=>
array(2) {
["name"]=>
string(3) "max"
["value"]=>
string(3) "500"
}
}
}
I would like to extrat 6 and 8.
The problem, is that I don't know in advance how many "ou" I will have.
It can be from 0 to n
Seems like an easy approach would be to have PHP unserialize the array, then iterate over it with a foreach loop like this:
foreach($array as $index => $subArray) {
foreach($subArray as $key => $val) {
if ($key == "ou") {
$ouArray[$index] = $val;
}
}
}
(where $array, obviously, is your unserialized array)
You could also just use $ouArray[] = $val, if you don't care which element the ou belonged to.

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

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
}

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 convert this array?

i have the following array:
["addToCart"]=>
array(3) {
[1]=>
array(5) {
["aantal"]=>
int(1)
["film_id"]=>
string(1) "1"
["zaal_id"]=>
string(1) "1"
["dag"]=>
string(7) "maandag"
["seats"]=>
array(4) {
[0]=>
string(2) "67"
[1]=>
string(2) "68"
[2]=>
string(2) "69"
[3]=>
string(2) "70"
}
}
You can see that i have an array called "seats" inside the "addToCart" array.There are 4 items in the "seats" array.
what i would like to have is 4 separate arrays, they should all have the same content but each of them needs to have 1 value of "seats".
I'm not sure I got exactly what you're looking to do, but this would result in an array of arrays where each has only one seat:
$seatArrays = array();
foreach ($addToCart as $arr)
{
foreach ($arr["seats"] as $seat)
{
$seatArr = $arr; // Copy the original array
$seatArr["seats"] = $seat; // Replace the "seats" subarray with the current seat
$seatArrays[] = $seatArr;
}
}

Categories