i just want to assign array value to my last array inside multidimensional array.
Here is my array
Array (
[college] => Array (
[student] => Array (
[contact] => Array ()
)
[parents] => Array (
[contact] => Array ()
)
)
[school] => Array (
[parents] => Array (
[contact] => Array ()
)
[student] => Array (
[contact] => Array ()
)
[data] => Array (
[contact] => Array()
)
))
Things are that every things is dynamic some time it could be simple array or might be multidimensional array.
every key and value are dynamic
i want to assign array value to last array eg.
i want something like that.
$assignArray = array('primary_number'=>123,'main_number'=>123);
Array (
[college] => Array (
[student] => Array (
[contact] => Array ('primary_number'=>123,'main_number'=>123)
)
[parents] => Array (
[contact] => Array ('primary_number'=>123,'main_number'=>123)
)
)
[school] => Array (
[parents] => Array (
[contact] => Array ('primary_number'=>123,'main_number'=>123)
)
[student] => Array (
[contact] => Array ('primary_number'=>123,'main_number'=>123)
)
[data] => Array (
[contact] => Array('primary_number'=>123,'main_number'=>123)
)
))
can any one solve it?
any help will appreciate..
Use a recursive procedure:
function replace_leaf_arrays(&$array, $replacement) {
foreach ($array as &$val) {
if (empty($val)) {
$val = $replacement;
} else {
replace_leaf_arrays($val, $replacement);
}
}
}
replace_leaf_arrays($main_array, $assignArray);
Related
I have an array that has some keys that are repeating multiple times. Would like to combine them so that I can have them in one array, within the same array.
I have an array of this type;
Array
(
[Shoes] => Array
(
[POLO] => Array
(
[0] => Size5
)
)
)
Array
(
[Shoes] => Array
(
[FUBU] => Array
(
[0] => size6
)
)
)
Array
(
[Bag] => Array
(
[HPBAG] => Array
(
[0] => Black
)
)
)
Array
(
[Bag] => Array
(
[HPBAG] => Array
(
[0] => White
)
)
)
I would like an output of the following kind;
Array
(
[Shoes] => Array
(
[POLO] => Array
(
[0] => size5
)
[FUBU] => Array
(
[0] => size6
)
)
[Bag] => Array
(
[HPBAG] => Array
(
[0] => Black
[1] => White
)
)
)
I have tried array_merge, array_merge_recursive but all are not working.
foreach ($county as $value) { print_r(array_merge($value)); } //$value contains the array
foreach ($county as $value) { print_r(array_merge_recursive($value)); } //$value contains the array
Kindly assist if you have an idea on how to solve this in PHP.
You could do it with a few nested array_walk() calls:
$arr=array(array("Shoes" => array("POLO" => array("Size5"))),
array("Shoes" => array("FUBU" => array("size6"))),
array("Bag" => array("HPBAG" => array("Black"))),
array("Bag" => array("HPBAG" => array("White"))));
$res=[];
array_walk($arr,function($a) use (&$res){
array_walk($a, function($ar,$type) use (&$res){
array_walk($ar, function ($arr,$brand) use (&$res,$type){
$res[$type][$brand]=array_merge($res[$type][$brand]??[],$arr);
});
});
});
print_r($res);
See the demo here: https://rextester.com/RFLQ18142
It produces this result:
Array
(
[Shoes] => Array
(
[POLO] => Array
(
[0] => Size5
)
[FUBU] => Array
(
[0] => size6
)
)
[Bag] => Array
(
[HPBAG] => Array
(
[0] => Black
[1] => White
)
)
)
I have two array. array one and array two. I want to merge these array into single array with key. My output result is valid but sequence is not correct.
Array one
Array
(
[0] => test-685f1e7bc357187e449479d627100102
[1] => test-685f1e7bc357187e449479d627d29390
)
Array two
Array
(
[0] => DF955298-A664-4FA7-9586-FCD4CF977777
[1] => DF955298-A664-4FA7-9586-FCD4CF988888
)
Expected Result
Array
(
[0] => Array
(
[key] => test-685f1e7bc357187e449479d627100102
[uuid] => DF955298-A664-4FA7-9586-FCD4CF977777
)
[1] => Array
(
[key] => test-685f1e7bc357187e449479d627d29390
[uuid] => DF955298-A664-4FA7-9586-FCD4CF988888
)
)
My code is for that result is mentioned below:
$record = array();
foreach ($keys_array as $key => $all_key) {
foreach ($uuid_array as $uuid_key => $all_uuid) {
$record[$key]['key'] = $all_key;
$record[$uuid_key]['uuid'] = $all_uuid;
}
}
My output sequence is not valid. Where is the problem
Array
(
[0] => Array
(
[key] => test-685f1e7bc357187e449479d627100102
[uuid] => DF955298-A664-4FA7-9586-FCD4CF977777
)
[1] => Array
(
[uuid] => test-685f1e7bc357187e449479d627d29390
[key] => DF955298-A664-4FA7-9586-FCD4CF988888
)
)
Simple solution:
$record = array();
foreach ($keys_array as $key => $all_key) {
$record[] = [
'key' => $all_key,
// get value under the same key from `$uuid_array`
'uuid' => $uuid_array[$key],
];
}
I have following multidimensional Array and I want to get the difference, if there is just one sub Array or multiple in that array.
For Example:
In Array [1] there is just one sub Array [example]
In Array [2] there are two sub Arrays [example]
[content] => Array
(
[...]
[1] => Array
(
[example] => Array
(
[value] => GET THIS
[attr] => Array
(
[...]
)
)
)
[2] => Array
(
[example] => Array
(
[0] => Array
(
[value] => GET THIS
[attr] => Array
(
[...]
)
)
[1] => Array
(
[value] => GET THIS
[attr] => Array
(
[...]
)
)
)
)
Now to get the [value] from the first Array I would try:
foreach ($content as $example) {
echo($content['example']['value']);
}
And to get each [value] from the second Array I would try:
foreach ($content as $example) {
foreach ($example as $values) {
echo($value['value']);
}
}
So far so good but how do I decide which function to run? Am I missing something?
Is there an if-statement which can help me there?
Something like:
if(multiple sub-arrays){
// do first code example
} else {
// do second code example
}
I simply want a method to get all values called [value] out of the array.
Thank you in advance!
The most obvious solution is to change function which generates your content array so as it always generates sub arrays in a format like:
[content] => Array
(
[...]
[1] => Array
(
[example] => Array
(
[0] => Array
(
[value] => GET THIS
[attr] => Array
(
[...]
)
)
)
)
[2] => Array
(
[example] => Array
(
[0] => Array
(
[value] => GET THIS
[attr] => Array
(
[...]
)
)
[1] => Array
(
[value] => GET THIS
[attr] => Array
(
[...]
)
)
)
)
But if you don't have such option - then use a simple check:
foreach ($content as $item) {
// here check if your `$item` has an `value` subkey under `example` key
if (array_key_exists('value', $item['example'])) {
echo($item['example']['value']);
} else {
foreach ($item['example'] as $values) {
echo ($values['value']);
}
}
}
Assuming that your final dimension allways as a 'value' node:
function arrayIterate($array){
foreach ($content as $example) {
if(!isset($example['value'])){
arrayIterate($example);
}else{
echo($example['value']);
}
}
}
I'm a newbie to this associative array concept in PHP. Now I'm having an array named $sample as follows:
Array
(
[name] => definitions
[text] =>
[attributes] => Array
(
[name] => Mediation_Soap_Server_Reporting
[targetnamespace] => https://mediation.moceanmobile.net/soap/reporting
)
[children] => Array
(
[types] => Array
(
[0] => Array
(
[name] => types
[text] =>
[attributes] => Array
(
)
[children] => Array
(
[xsd:schema] => Array
(
[0] => Array
(
[name] => schema
[text] =>
[attributes] => Array
(
[targetnamespace] => https://mediation.moceanmobile.net/soap/reporting
)
[children] => Array
(
[xsd:complextype] => Array
(
[0] => Array
(
[name] => complextype
[text] =>
[attributes] => Array
(
[name] => Mediation_Soap_FaultMessage
)
[children] => Array
(
[xsd:sequence] => Array
(
[0] => Array
(
[name] => sequence
[text] =>
[attributes] => Array
(
)
)
)
)
)
)
)
)
)
)
)
)
)
)
From the above array I want to refer(or access) to the key xsd:schema. But I'm not able to do it. Can you please tell me how should I access or refer this key from the associative array names $sample? Thanks in advance.
To access this value you would use:-
$sample['children']['types'][0]['children']['xsd:schema'];
If you have multiple of these elements in your types array you will need to loop through them:-
foreach($sample['children']['types'] as $type) {
if(isset($type['children']) && isset($type['children']['xsd:schema'])) {
// Perform action on element
$type['children']['xsd:schema'];
}
}
If you do not know your structure (as in xsd:schema can occur outside of types) then you will need to write a recursive function or loop for finding it.
I guess your goal is to seek for the key/value pair where the key is "xsd" ?
If so, in PHP, you can do so by using the follwing logic:
while (list($key, $value) = each($arr)) {
echo "Key: $key; Value: $value<br />\n";
}
// OR
foreach ($arr as $key => $value) {
echo "Key: $key; Value: $value<br />\n";
}
Just add a set of recursing or nested loops to traverse the structure until you find the proper key.
I need to sort a multi-dimensional array which represents filesystem structure:
Array
(
[dir1] => Array
(
[dir2] => Array
(
[dir3] => Array
(
[dir4] => Array
(
[0] => file1.php
[1] => abc.php
)
)
[0] => file2.php
[1] => abc.php
)
)
[abc] => Array
(
[abc] => Array
(
[abc] => Array
(
[0] => file5.php
)
)
)
)
I have no idea what is the algorithm.
http://php.net/sort#51088
replace sort($a) at the beginning of the mulsort function by ksort($a)
EDIT: sorry, just change the mulsort code to :
function mulsort(&$a)
{
ksort($a);
foreach($a as &$value)
if (is_array($value))
mulsort($value);
}