Merging Data of MultiDimensional Array - php

I have this kind of array
Array ( [0] => Array ( [id] => 1
[name] => PeopleOne
[address] => AddressOneOfPeopleOne
[1] => Array ( [id] => 2
[name] => PeopleTwo
[address] => AddressOneOfPeopleTwo
[2] => Array ( [id] => 3
[name] => PeopleThree
[address] => AddressOneOfPeopleThree
[3] => Array ( [id] => 4
[name] => PeopleOne
[address] => AddressTwoOfPeopleOne
and I want this kind of format
Array ( [0] => Array ( [id] => 1
[name] => PeopleOne
[address] => Array(
[0] => AddressOneOfPeopleOne
[1] => AddressTwoOfPeopleOne
)
[1] => Array ( [id] => 2
[name] => PeopleTwo
[address] => AddressOneOfPeopleTwo
[2] => Array ( [id] => 3
[name] => PeopleThree
[address] => AddressOneOfPeopleThree
I don't know how to do?
Could anyone please solve this?

I have a feeling this isn't exactly what you're looking for, but I'm not entirely sure what needs to be inside the final array. How is this, at least for a start?
<?php
$array = array(
array('id' => 1, 'name' => 'PeopleOne', 'address' => 'Address1'),
array('id' => 2, 'name' => 'PeopleTwo', 'address' => 'Address2'),
array('id' => 3, 'name' => 'PeopleOne', 'address' => 'Address3')
);
foreach ($array as $k => $v) {
$newarray[$v['name']][] = $v['address'];
}
echo '<pre>'.print_r($array,1).'</pre>';
echo '<pre>'.print_r($newarray,1).'</pre>';
?>

I'll assume that your array is called $array.
foreach ($array as $subArray) {
if (!is_array($subArray['address']) {
$subArray['address'] = array($subArray['adress']);
}
$subArray['address'][] = 'AddressTwoOfPeopleOne';
}

Related

Group array by array name

I have this array:
Array
(
[0] => Array
(
[id] => 1
[name] => Something
[other_id] => 2
[other_name] => One
)
[1] => Array
(
[id] => 1
[name] => Something
[other_id] => 3
[other_name] => Two
)
[2] => Array
(
[id] => 1
[name] => Something
[other_id] => 3
[other_name] => Three
)
[3] => Array
(
[id] => 1
[name] => Something
[other_id] => 2
[other_name] => Four
)
)
Now I need the array to look like this:
Array
(
[0] => Array
(
[id] => 1
[name] => Something
[0] => Array
(
[other_id] => 2
[other_name] => One
)
[1] => Array
(
[other_id] => 3
[other_name] => Two
)
[2] => Array
(
[other_id] => 4
[other_name] => Three
)
)
)
I tried different ways but no result. I hope someone can help me a little bit with that.
Not sure I understand what you want, but this might do it:
//Somewhere to store the result.
$output = array();
//Loop through the input array.
foreach($input as $element) {
$id = $element['id'];
$other_id = $element['other_id'];
if(!isset($output[$id])) {
//ID is not already in the output array, so add it.
$output[$id] = array(
'id' => $id,
'name' => $element['name'],
);
}
if(!isset($output[$id][$other_id])) {
//Other_ID is not already in the output array, so add it.
$output[$id][$other_id] = array(
'other_id' => $other_id,
'other_name' => $element['other_name'],
);
}
}

Converting a php object into associative array

I am trying to read the following php object result of an api call into an associative array. Has someone done this before or can otherwise help me?
I have tried object var dump, and array casting, but this just doesnt seems to work. I know I am doing something fundamentally wrong. Can someone help?
Many thanks.
Google_Service_Directory_Users Object
(
[etag] => "sfgsfgdsfgsdfgjkdjgfd"
[kind] => admin#directory#users
[nextPageToken] =>
[triggerEvent] =>
[usersType:protected] => Google_Service_Directory_User
[usersDataType:protected] => array
[collection_key:protected] => items
[modelData:protected] => Array
(
[users] => Array
(
[0] => Array
(
[kind] => admin#directory#user
[id] => 7642341239423
[etag] => "jasdfjshwer43537345fsdfs"
[primaryEmail] => info#example.com
[name] => Array
(
[givenName] => Info -
[familyName] => Example
[fullName] => Info - Example
)
[isAdmin] =>
[isDelegatedAdmin] =>
[lastLoginTime] => 2014-07-29T08:46:28.000Z
[creationTime] => 2014-07-29T08:31:56.000Z
[agreedToTerms] => 1
[suspended] =>
[changePasswordAtNextLogin] =>
[ipWhitelisted] =>
[emails] => Array
(
[0] => Array
(
[address] => info#example.com
[primary] => 1
)
)
[nonEditableAliases] => Array
(
[0] => info#example.com.test-google-a.com
)
[customerId] => fsdfdd4
[orgUnitPath] => /
[isMailboxSetup] => 1
[includeInGlobalAddressList] => 1
)
[1] => Array
(
[kind] => admin#directory#user
[id] => 3895729453245
[etag] => "fsajdfd64hkj4534h5k3454"
[primaryEmail] => user#example.com
[name] => Array
(
[givenName] => User
[familyName] => Name
[fullName] => User Name
)
[isAdmin] => 1
[isDelegatedAdmin] =>
[lastLoginTime] => 2014-08-26T09:05:49.000Z
[creationTime] => 2012-09-16T08:55:26.000Z
[agreedToTerms] => 1
[suspended] =>
[changePasswordAtNextLogin] =>
[ipWhitelisted] =>
[emails] => Array
(
[0] => Array
(
[address] => support#example.com
)
[1] => Array
(
[address] => help#example.com
)
)
[customerId] => fsdafwr4
[orgUnitPath] => /
[isMailboxSetup] => 1
[includeInGlobalAddressList] => 1
)
)
)
[processed:protected] => Array
(
)
)
First solution:
$array = json_decode(json_encode($nested_object), true);
Second solution:
function object_to_array($data) {
if (is_array($data) || is_object($data)):
$result = array();
foreach ($data as $key => $value)
$result[$key] = object_to_array($value);
return $result;
endif;
return $data;
}
both searched from the internetz

How can I sort multidimensional array through key

Array
(
[12] => Array
(
[id] => 12
[name] => Car
[children] => Array
(
[0] => Array
(
[id] => 14
[name] => Volvo
)
[1] => Array
(
[id] => 15
[name] => Mercedes-Benz
)
)
)
[13] => Array
(
[id] => 13
[name] => Manga
[children] => Array
(
[0] => Array
(
[id] => 16
[name] => Naruto
)
[1] => Array
(
[id] => 17
[name] => Hunter X Hunter
)
)
)
[18] => Array
(
[id] => 18
[name] => aa
[children] => Array
(
)
)
)
Guys I want to sort the values of this array, i want to sort it by key and the key is 'name'. This should sort the first level and the second level thru key 'name'.
This array i put in print_r() so it looks like this. The array is not fix so i can add in the future.
So after sorting the final value of the array is...
Array
(
[18] => Array
(
[id] => 18
[name] => aa
[children] => Array
(
)
)
[12] => Array
(
[id] => 12
[name] => Car
[children] => Array
(
[0] => Array
(
[id] => 15
[name] => Mercedes-Benz
)
[1] => Array
(
[id] => 14
[name] => Volvo
)
)
)
[13] => Array
(
[id] => 13
[name] => Manga
[children] => Array
(
[0] => Array
(
[id] => 17
[name] => Hunter X Hunter
)
[1] => Array
(
[id] => 16
[name] => Naruto
)
)
)
)
And this is the array in code.
$categories = array(
12 =>
array('id' =>12,
'name' => 'Car',
'children' =>
array(
array('id' => 14,
'name' => 'volvo'
)
),
array(
array('id' => 15,
'name' => 'Mercedez-Benz'
)
)
),
13 =>
array('id' =>13,
'name' => 'Manga',
'children' =>
array(
array('id' => 16,
'name' => 'Naruto'
)
),
array(
array('id' => 17,
'name' => 'Hunter X Hunter'
)
)
),
18=>
array('id' => 18,
'name'=> 'aa',
'children' => array())
);
echo "<pre>";
print_r($categories);
the 'name' is not your real 'Key', just saying, because 'key' in Php normally means the name of your array, like '18' and '12' in your array and so on, like this:
<?php
$fruits = array("d"=>"lemon", "a"=>"orange", "b"=>"banana", "c"=>"apple");
ksort($fruits);
foreach ($fruits as $key => $val) {
echo "$key = $val\n";
}
?>
in this example, the result will be:
a = orange
b = banana
c = apple
d = lemon
anyway to answer your question on how to sort it on 'name', use 'usort':
function cmp($a, $b) {
return $a['name'] - $b['name'];
}
usort($arr,"cmp");
check out the following link for further information:
How do I sort a PHP array by an element nested inside?
if using php 5.3+ you can use closures in your code and sort like this
usort($categories,function($a,$b){
return $a['name'] - $b['name']
}
So,
I'm going to try and explain it better to you
your array is big, yes, but you want to sort only on the key value '[name]'.
In my test I used the array you provided:
$categories = array(
12 =>
array('id' =>12,
'name' => 'Car',
'children' =>
array(
array('id' => 14,
'name' => 'volvo'
)
),
array(
array('id' => 15,
'name' => 'Mercedez-Benz'
)
)
),
13 =>
array('id' =>13,
'name' => 'Manga',
'children' =>
array(
array('id' => 16,
'name' => 'Naruto'
)
),
array(
array('id' => 17,
'name' => 'Hunter X Hunter'
)
)
),
18=>
array('id' => 18,
'name'=> 'aa',
'children' => array())
);
if the array is declared, you can add the usort like this:
usort($categories,"sortByName");
the first element in the above ($categories), is your array, the second element will provide the name of a function, in this case 'sortByName'.
after that, you just add a function into your code (it doesn't really matter where you put it, even if it's at the bottom of the page):
function sortByName($categories, $b) {
return strcasecmp($categories["name"], $b["name"]);
}
Basically what this function does, is compairing your array with ...your array :) so it can sort it alfabetically by name- like you want it.
sthe 'strcasecmp' is to compare your strings against eachother to sort it. not that this is case non sensitive (strcmp will be case sensitive too). I assume you don't want it to check on uppercase and so on too, otherwise the result won't be what you wanted.
when you added that code, you can just print your array again:
echo "<pre>";
print_r($categories);
your result will be:
Array
(
[0] => Array
(
[id] => 18
[name] => aa
[children] => Array
(
)
)
[1] => Array
(
[id] => 12
[name] => Car
[children] => Array
(
[0] => Array
(
[id] => 14
[name] => volvo
)
)
[0] => Array
(
[0] => Array
(
[id] => 15
[name] => Mercedez-Benz
)
)
)
[2] => Array
(
[id] => 13
[name] => Manga
[children] => Array
(
[0] => Array
(
[id] => 16
[name] => Naruto
)
)
[0] => Array
(
[0] => Array
(
[id] => 17
[name] => Hunter X Hunter
)
)
)
I hope this is what you wanted. :-)

convert multidimensional array into two separated

I have a multidimensional Array like this:
Array (
[0] => Array (
[id] => 1
[name] => privilages1
)
[1] => Array (
[id] => 2
[name] => privilages2
)
[2] => Array (
[id] => 3
[name] => privilages3
)
[3] => Array (
[id] => 4
[name] => privilage4 )
[4] => Array (
[id] => 5
[name] => privilages5 )
)
and i want to compare it with another array, which looks like this:
Array (
[0] => Array (
[id] => 1 )
[1] => Array (
[id] => 2)
)
if the value of id matches, then i want to all values from the first example.
How can i do this?
You can use array_filter to filter the array elements you want by supplying a user defined callback function.
Here is the code:
$arr = array( array('id' => 1, 'name' => 'foo'),
array('id' => 2, 'name' => 'bar'),
array('id' => 3, 'name' => 'baz'),
array('id' => 4, 'name' => 'wow'));
$ret = array_filter($arr, create_function('$el',
'static $search=array(array("id" => 1), array("id" => 2));
$n=array("id" => $el["id"]);
return (array_search($n, $search) !== false);'));
print_r($ret);
OUTPUT
Array
(
[0] => Array
(
[id] => 1
[name] => foo
)
[1] => Array
(
[id] => 2
[name] => bar
)
)

Altering multidimensional array

I have a large multidimensional array and I basically want to drop the first level of arrays and build an array the doesn't have the numerical keys:
My current array:
Array
(
[0] => Array
(
[block_header14] => Array
(
[type] => block_header
[caption] => Silver
[collapsable] => 1
[collapsed] =>
)
[section14] => Array
(
[type] => checkbox_set
[name] => purchasable_memberships14
[caption] => Available Memberships
[values] => Array
(
[14] => Silver
[15] => Gold
)
[value] =>
)
)
[1] => Array
(
[block_header15] => Array
(
[type] => block_header
[caption] => Gold
[collapsable] => 1
[collapsed] =>
)
[section15] => Array
(
[type] => checkbox_set
[name] => purchasable_memberships15
[caption] => Available Memberships
[values] => Array
(
[14] => Silver
[15] => Gold
)
[value] =>
)
)
)
This what I want to end up with:
Array
(
[block_header14] => Array
(
[type] => block_header
[caption] => Silver
[collapsable] => 1
[collapsed] =>
)
[section14] => Array
(
[type] => checkbox_set
[name] => purchasable_memberships14
[caption] => Available Memberships
[values] => Array
(
[14] => Silver
[15] => Gold
)
[value] =>
)
[block_header15] => Array
(
[type] => block_header
[caption] => Gold
[collapsable] => 1
[collapsed] =>
)
[section15] => Array
(
[type] => checkbox_set
[name] => purchasable_memberships15
[caption] => Available Memberships
[values] => Array
(
[14] => Silver
[15] => Gold
)
[value] =>
)
)
Edit: I reworked the initial foreach and skipped the extra step: Sorry for the blurry question
foreach ($aMemLevels as $id =>$name) {
$aForm['inputs']['block_header'.$id] = array(
'type' => 'block_header',
'caption' => 'Available to ' . $name . ' Members',
'collapsable' => true,
'collapsed' => false
);
$aForm['inputs']['section'.$id] = array(
'type' => 'checkbox_set',
'name' => 'purchasable_memberships'.$id,
'values' => getMemberships(true),
'value' => $aValue,
);
}
$new_array = array();
foreach($array as $value) {
$new_array = array_merge($new_array, $value);
}
print_r($new_array);
I haven't tested it, but maybe something like this will help?
$new_array = array();
foreach ($multi_array as $inner_arr) {
$new_array = array_merge($new_array, $inner_array);
}
$multi_array being your initial, multidimensional array...
Let me know if that works.

Categories