Split continuous array into subarrays - php

I am trying to convert an array formatted as the following:
object(Categories_store_tree)#519 (1) {
["list_of_sections":"Categories_store_tree":private]=> array(5) {
["id"]=> int(1)
["name"]=> string(11) "Main Store"
["parent_id"]=> NULL
["children"]=> array(2) {
[0]=> array(5) {
["id"]=> int(2)
["name"]=> string(4) "Food"
["parent_id"]=> int(1)
["children"]=> array(0) { }
}
[1]=> array(5) {
["id"]=> int(3)
["name"]=> string(14) "Electronics"
["parent_id"]=> int(1)
["children"]=> array(2) {
[0]=> array(5) {
["id"]=> int(4)
["name"]=> string(8) "Headphones"
["parent_id"]=> int(3)
["children"]=> array(0) { }
}
[1]=> array(5) {
["id"]=> int(5)
["name"]=> string(5) "Smartphones"
["parent_id"]=> int(3)
["children"]=> array(0) { }
}
}
}
}
}
}
To this structure of array:
object(Categories_store_tree)#964 (1) {
["list_of_sections":"Categories_store_tree":private]=> array(5) {
[0]=> array(4) {
["id"]=> int(1)
["name"]=> string(11) "Main Store"
["parent_id"]=> NULL
}
[1]=> array(4) {
["id"]=> int(2)
["name"]=> string(4) "Food"
["parent_id"]=> int(1)
}
[2]=> array(4) {
["id"]=> int(3)
["name"]=> string(14) "Electronics"
["parent_id"]=> int(1)
}
[3]=> array(4) {
["id"]=> int(4)
["name"]=> string(8) "Headphones"
["parent_id"]=> int(3)
}
[4]=> array(4) {
["id"]=> int(5)
["name"]=> string(5) "Smartphones"
["parent_id"]=> int(3)
}
}
}
Currently I am doing in manual but the idea is to make it automatic. I have tried with this code, but it returns an empty array, I have also tried with a function but I have been stuck for a couple of days and don't know what to do.
$clean_array = array();
$cont = 0;
foreach ( $new_tree as $key => $value ) {
if ( is_array( $value ) ) {
$cont++;
foreach ( $value as $key1 => $value1 ) {
if ( is_array( $value1 ) ) {
$cont++;
foreach ( $value1 as $key2 => $value2 ) {
$clean_array[$cont][$key2] = $value2;
}
} else {
$clean_array[$cont][$key1] = $value1;
}
}
} else {
$clean_array[$cont][$key] = $value;
}
}

Maybe you could try something like this.
Convert the object To an array:
function objectToArray($d) {
if (is_object($d))
$d = get_object_vars($d);
return is_array($d) ? array_map(__METHOD__, $d) : $d;
}
Then:
$arr = objectToArray($object);
print_r($arr);
Source

Related

how to loop through array and extract another array data php

Hi i have a multidimensional array and a parameter that may change frequently .
i made some code to extract data just for 3 level loop through array .
but i can't convert it to function that work in array_map or array_walk .
is it possible ?
sample array data :
array(4) {
[0]=>
array(5) {
[0]=>
array(2) {
["loc"]=>
array(1) {
["type"]=>
string(8) "test1"
}
["rel"]=>
array(1) {
["id"]=>
int(1)
}
}
[1]=>
array(2) {
["loc"]=>
array(1) {
["type"]=>
string(8) "test2"
}
["rel"]=>
array(1) {
["id"]=>
int(2)
}
}
[2]=>
array(2) {
["loc"]=>
array(1) {
["type"]=>
string(8) "test2"
}
["rel"]=>
array(1) {
["id"]=>
int(2)
}
}
[3]=>
array(2) {
["loc"]=>
array(1) {
["type"]=>
string(8) "test3"
}
["rel"]=>
array(1) {
["id"]=>
int(3)
}
}
[4]=>
array(2) {
["loc"]=>
array(1) {
["type"]=>
string(8) "test4"
}
["rel"]=>
array(1) {
["id"]=>
int(4)
}
}
}
[1]=>
array(5) {
[0]=>
array(2) {
["loc"]=>
array(1) {
["type"]=>
string(8) "test10"
}
["rel"]=>
array(1) {
["id"]=>
int(10)
}
}
[1]=>
array(2) {
["loc"]=>
array(1) {
["type"]=>
string(8) "test11"
}
["rel"]=>
array(1) {
["id"]=>
int(11)
}
}
[2]=>
array(2) {
["loc"]=>
array(1) {
["type"]=>
string(8) "test12"
}
["rel"]=>
array(1) {
["id"]=>
int(12)
}
}
[3]=>
array(2) {
["loc"]=>
array(1) {
["type"]=>
string(8) "test13"
}
["rel"]=>
array(1) {
["id"]=>
int(13)
}
}
[4]=>
array(2) {
["loc"]=>
array(1) {
["type"]=>
string(8) "test14"
}
["rel"]=>
array(1) {
["id"]=>
int(14)
}
}
}
[2]=>
array(5) {
[0]=>
array(2) {
["loc"]=>
array(1) {
["type"]=>
string(8) "test20"
}
["rel"]=>
array(1) {
["id"]=>
int(20)
}
}
[1]=>
array(2) {
["loc"]=>
array(1) {
["type"]=>
string(8) "test21"
}
["rel"]=>
array(1) {
["id"]=>
int(21)
}
}
[2]=>
array(2) {
["loc"]=>
array(1) {
["type"]=>
string(8) "test22"
}
["rel"]=>
array(1) {
["id"]=>
int(22)
}
}
[3]=>
array(2) {
["loc"]=>
array(1) {
["type"]=>
string(8) "test23"
}
["rel"]=>
array(1) {
["id"]=>
int(23)
}
}
[4]=>
array(2) {
["loc"]=>
array(1) {
["type"]=>
string(8) "test24"
}
["rel"]=>
array(1) {
["id"]=>
int(24)
}
}
}
}
and here is sample function that can loop 3 times
but i want make it dynamic loop through this array
foreach ( $data as $k => $v ) {
$cnt = 0 ;
foreach ( $data as $m => $n ) {
array_push($parents,$k);
if ( !in_array($m,$parents)) {
foreach ( $data as $x => $y ) {
array_push($parents,$m);
if ( !in_array($x,$parents)) {
$hold[$k][$cnt] = array(
$k , $m , $x
);
$cnt++;
}
}
$parents = deleteFromArray($parents , $m);
}
}
$parents = deleteFromArray($parents , $k);
}

How to get part of array excluding one key in multidimensional array?

I'm grouping one multidimensional array by age.
This is my code:
$mEmployees = array (
array("name"=>"Pedro", "age"=>20, "ID"=>1111),
array("name"=>"Carlos", "age"=>15, "ID"=>2222),
array("name"=>"Susana", "age"=>20, "ID"=>3333),
array("name"=>"Carmen", "age"=>19, "ID"=>4444)
);
$byAge=array();
foreach ($mEmployees as $k => $oneItem) {
$byAge[$oneItem['age']][$k] = $oneItem;
}
var_dump($byAge);
That works fine as you can see below:
output:
array(3) {
[20]=>
array(2) {
[0]=>
array(3) {
["name"]=>
string(5) "Pedro"
["age"]=>
int(20)
["ID"]=>
int(1111)
}
[2]=>
array(3) {
["name"]=>
string(6) "Susana"
["age"]=>
int(20)
["ID"]=>
int(3333)
}
}
[15]=>
array(1) {
[1]=>
array(3) {
["name"]=>
string(6) "Carlos"
["age"]=>
int(15)
["ID"]=>
int(2222)
}
}
[19]=>
array(1) {
[3]=>
array(3) {
["name"]=>
string(6) "Carmen"
["age"]=>
int(19)
["ID"]=>
int(4444)
}
}
}
But in the results, the age key is redundant. I want to remove this key in the $byAge array.
I tried with array_slice, but it's not possible to indicate one irregular offset (the key age is in middle).
How I can achieve this easily for this result?
array(3) {
[20]=>
array(2) {
[0]=>
array(3) {
["name"]=>
string(5) "Pedro"
["ID"]=>
int(1111)
}
[2]=>
array(3) {
["name"]=>
string(6) "Susana"
["ID"]=>
int(3333)
}
}
[15]=>
array(1) {
[1]=>
array(3) {
["name"]=>
string(6) "Carlos"
["ID"]=>
int(2222)
}
}
[19]=>
array(1) {
[3]=>
array(3) {
["name"]=>
string(6) "Carmen"
["ID"]=>
int(4444)
}
}
}
Cache the age value in a variable and unset from $oneItem.
foreach ($mEmployees as $k => $oneItem) {
$age = $oneItem['age'];
unset($oneItem['age']);
$byAge[$age][$k] = $oneItem;
}
Demo: https://3v4l.org/pDDn5

PHP Nested Array convert into flat array

I have a problem to transform my hierarchical array like this:
array(
[0]=>
array(3) {
["id"]=>
int(2353011010)
["name"]=>
string(17) "LEDER ACCESSOIRES"
["order"]=>
int(15)
}
[1]=>
array(3) {
["id"]=>
int(2371475010)
["name"]=>
string(15) "SPORT AUFKLEBER"
["order"]=>
int(25)
}
[2]=>
array(4) {
["id"]=>
int(2563635010)
["name"]=>
string(17) "KENNZEICHENHALTER"
["order"]=>
int(10)
["children"]=>
array(6) {
[0]=>
array(4) {
["id"]=>
int(3854259010)
["name"]=>
string(9) "EDELSTAHL"
["order"]=>
int(92)
["children"]=>
array(2) {
[0]=>
array(3) {
["id"]=>
int(20878056010)
["name"]=>
string(5) "test1"
["order"]=>
int(1)
}
}
}
[1]=>
array(3) {
["id"]=>
int(3854260010)
["name"]=>
string(5) "CHROM"
["order"]=>
int(91)
}
}
[3]=>
array(4) {
["id"]=>
int(19754330010)
["name"]=>
string(30) "SCHALTMANSCHETTEN CARBON OPTIK"
["order"]=>
int(3)
}
}
)
Into a flat ones like this:
array(
[0]=>
array(3) {
["id"]=>
int(2353011010)
["name"]=>
string(17) "LEDER ACCESSOIRES"
["order"]=>
int(15)
}
[1]=>
array(3) {
["id"]=>
int(2371475010)
["name"]=>
string(15) "SPORT AUFKLEBER"
["order"]=>
int(25)
}
[2]=>
array(3) {
["id"]=>
int(2563635010)
["name"]=>
string(17) "KENNZEICHENHALTER"
["order"]=>
int(10)
}
[3]=>
array(4) {
["id"]=>
int(3854259010)
["name"]=>
string(9) "EDELSTAHL"
["order"]=>
int(92),
["parentId"]=> 2563635010
}
[4]=>
array(4) {
["id"]=>
int(20878056010)
["name"]=>
string(5) "test1"
["order"]=>
int(1),
["parentId"]=> 2563635010
}
[5]=>
array(4) {
["id"]=>
int(3854260010)
["name"]=>
string(5) "CHROM"
["order"]=>
int(91),
["parentId"]=> 2563635010
}
[6]=>
array(4) {
["id"]=>
int(19754330010)
["name"]=>
string(30) "SCHALTMANSCHETTEN CARBON OPTIK"
["order"]=>
int(3)
}
)
The children antities should be removed and every child element should get a parentId entity of the higher level id. I need this solution for transfering into DB.
thx
Now, I have create a "temporary" method that works for me, but is noch flexible to use:
function recursive($categories) {
foreach ($categories as $value) {
$result[$value->id]['id'] = $value->id;
$result[$value->id]['name'] = $value->name;
$result[$value->id]['order'] = $value->order;
$result[$value->id]['parentId'] = 0;
if(isset($value->children)) {
$parentId = $value->id;
foreach($value->children as $value2) {
$result[$value2->id]['id'] = $value2->id;
$result[$value2->id]['name'] = $value2->name;
$result[$value2->id]['parentId'] = $parentId;
if(isset($value2->children)) {
$parentId = $value2->id;
foreach($value2->children as $value3) {
$result[$value3->id]['id'] = $value3->id;
$result[$value3->id]['name'] = $value3->name;
$result[$value3->id]['parentId'] = $parentId;
}
}
}
}
}
return $result;
}
Do anybody know a recursive solution for this method?

Loop through PHP multidimensional array

I am trying to loop through a multidimensional array but in the foreach loop it just outputs error
index 'name' not found. index 'calories' not founder
foreach($responsex['foods'] as $fx5)
{
echo($fx5['name']);
echo($fx5['calories']);
}
Response: i.e. $responsex
array ( 'encodedId' => '4H8xxx', 'displayName' => 'sam', )array(3) {
["foods"]=> array(3) { [0]=> array(5) { ["isFavorite"]=> bool(false)
["logDate"]=> string(10) "2016-04-15" ["logId"]=> int(7139364449)
["loggedFood"]=> array(10) { ["accessLevel"]=> string(6) "PUBLIC"
["amount"]=> int(2) ["brand"]=> string(0) "" ["calories"]=> int(574)
["foodId"]=> int(536497687) ["locale"]=> string(5) "en_AU"
["mealTypeId"]=> int(7) ["name"]=> string(14) "Potato Pudding"
["unit"]=> array(3) { ["id"]=> int(91) ["name"]=> string(3) "cup"
["plural"]=> string(4) "cups" } ["units"]=> array(8) { [0]=> int(6754)
[1]=> int(91) [2]=> int(256) [3]=> int(279) [4]=> int(226) [5]=>
int(180) [6]=> int(147) [7]=> int(389) } } ["nutritionalValues"]=>
array(6) { ["calories"]=> int(574) ["carbs"]=> float(49.16) ["fat"]=>
float(34.98) ["fiber"]=> float(3.6) ["protein"]=> float(16.1)
["sodium"]=> int(1524) } } [1]=> array(5) { ["isFavorite"]=>
bool(false) ["logDate"]=> string(10) "2016-04-15" ["logId"]=>
int(7138517833) ["loggedFood"]=> array(10) { ["accessLevel"]=>
string(6) "PUBLIC" ["amount"]=> int(1) ["brand"]=> string(0) ""
["calories"]=> int(359) ["foodId"]=> int(535239347) ["locale"]=>
string(5) "en_AU" ["mealTypeId"]=> int(7) ["name"]=> string(54) "Fish,
Noodles and Vegetables in Cheese Sauce (Mixture)" ["unit"]=> array(3)
{ ["id"]=> int(91) ["name"]=> string(3) "cup" ["plural"]=> string(4)
"cups" } ["units"]=> array(8) { [0]=> int(6837) [1]=> int(91) [2]=>
int(256) [3]=> int(279) [4]=> int(226) [5]=> int(180) [6]=> int(147)
[7]=> int(389) } } ["nutritionalValues"]=> array(6) { ["calories"]=>
int(359) ["carbs"]=> float(28.01) ["fat"]=> float(14.05) ["fiber"]=>
float(2.9) ["protein"]=> float(29.08) ["sodium"]=> int(534) } } [2]=>
array(5) { ["isFavorite"]=> bool(false) ["logDate"]=> string(10)
"2016-04-15" ["logId"]=> int(7138326866) ["loggedFood"]=> array(10) {
["accessLevel"]=> string(6) "PUBLIC" ["amount"]=> int(1) ["brand"]=>
string(0) "" ["calories"]=> int(157) ["foodId"]=> int(536493638)
["locale"]=> string(5) "en_AU" ["mealTypeId"]=> int(7) ["name"]=>
string(11) "Cashew Nuts" ["unit"]=> array(3) { ["id"]=> int(226)
["name"]=> string(2) "oz" ["plural"]=> string(2) "oz" } ["units"]=>
array(4) { [0]=> int(226) [1]=> int(180) [2]=> int(147) [3]=> int(389)
} } ["nutritionalValues"]=> array(6) { ["calories"]=> int(157)
["carbs"]=> float(8.56) ["fat"]=> float(12.43) ["fiber"]=> float(0.9)
["protein"]=> float(5.17) ["sodium"]=> int(3) } } } ["goals"]=>
array(2) { ["calories"]=> int(1161) ["estimatedCaloriesOut"]=>
int(1411) } ["summary"]=> array(7) { ["calories"]=> int(1090)
["carbs"]=> float(85.73) ["fat"]=> float(61.46) ["fiber"]=> float(7.4)
["protein"]=> float(50.35) ["sodium"]=> int(2061) ["water"]=> int(0) }
}
you can recursively iterate through the arrays and print them as follows as key value pairs.
<?php
//initially call the function
print_array($responsex);
function print_array($array){
foreach($array as $key=>$value){
//recursively print the array
if(is_array($value)){
echo("Array : ".$key."\n");
print_array($value);
}
else{
echo($key." => ".$value);
}
}
}
?>
You can define additional tasks other than printing them with the above code.
Edit:
if you are sure that the array is two dimensional, no need to go recursively.
<?php
//initially call the function
print_array($responsex);
//if you are sure that the array is two dimensional, no need to go recursively.
function print_array($array){
foreach($array as $key=>$value){
if(is_array($value)){
if($key==="foods"){
var_dump($array[$key]);
}
}
else{
echo($key." => ".$value);
}
}
}
Use this way..
<?php
$keys = array_keys($data);// put your array name as a place of $data
$iterations = count($array[$keys[0]]);
for($i = 0; $i < $iterations; $i++) {
$data = array();
foreach($array as $key => $value) {
$data[$key] = $value[$i];
}
print_r($data);
}
?>

PHP: Count values in Arrays

I have this Array:
array(66) {
[0]=> array(2) {
["location"]=> string(10) "Breakwater" ["bsid"]=> string(4) "105a"
}
[1]=> array(2) {
["location"]=> string(10) "Breakwater" ["bsid"]=> string(4) "105b"
}
[2]=> array(2) {
["location"]=> string(10) "Breakwater" ["bsid"]=> string(4) "105c"
}
[3]=> array(2) {
["location"]=> string(10) "Breakwater" ["bsid"]=> string(4) "105d"
}
[4]=> array(2) {
["location"]=> string(10) "Breakwater" ["bsid"]=> string(4) "117b"
}
[5]=> array(2) {
["location"]=> string(10) "Breakwater" ["bsid"]=> string(4) "117c"
}
[6]=> array(2) {
["location"]=> string(10) "Breakwater" ["bsid"]=> string(4) "123a"
}
[7]=> array(2) {
["location"]=> string(10) "Whateverelse" ["bsid"]=> string(4) "123b"
}
}
How can I count how many Breakwater's I have and how many Whateverelse's and get something like this:
array(2) {
[0]=> array(2) {
["Breakwater"]=> string(2) "20"
} [1]=> array(2) {
["Whateverelse"]=> string(1) "1"
}
}
Just loop around the original array, and each time a location is hit, increment a counter in the locations array where the index is the location.
$loc = array();
foreach($arr as $value) {
$location = $value['location'];
if(isset($loc[$location])) {
$loc[$location]++;
} else {
$loc[$location] = 1;
}
}
print_r($loc);
Will output
array
(
["Breakwater"] => 7,
["Whateverelse"] => 1
}
I got this finally :
$output = array("Breakwater" => 0, "Whateverelse" => 0);
foreach ($array as $val) {
$output["Breakwater"] += ($val["location"] == "Breakwater") ? 1 : 0;
$output["Whateverelse"] += ($val["location"] == "Whateverelse") ? 1 : 0;
}
var_dump($output);

Categories