How to access a specific key from an associative array in PHP? - php

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.

Related

Sequence issue with nested foreach loop php

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],
];
}

assign value to last multidimensional array

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);

Get the difference between one sub-Array or multiple sub-Arrays in a multidimensional Array

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']);
}
}
}

php multidemensional array to simple array

I need this multidimensional array converted to a simple array.
Array
(
[0] => Array
(
[id_zub] => 1
[name] => Backen
)
[1] => Array
(
[id_zub] => 2
[name] => Kochen
)
)
A simple array:
array(
[id_zub] => 1
[name] => Backen
[id_zub] => 2
[name] => Kochen
)
function array_flattern($array){
foreach($array as $key=> $value){
if(is_array($value)){
$this->array_flattern($value);
}
else{
$this->result[$key] = $value;
}
}
}
The function gives me this result:
Array
(
[id_zub] => 2
[name] => Kochen
)
your function works as intended, Your getting a "key clash" and the latter key's value is the one used. You would have yo have a suffix on the key if you wanted it in one dimension
eg
Array ( [id_zub_2] => Kochen )

how to extract the exact data from an array in php

i got this array from an database...how to get the exact value attribute......
Array
(
[0] => TRowResult Object
(
[row] => tests
[columns] => Array
(
[a21ha:link_0] => TCell Object
(
[value] =>testsample
[timestamp] => 1265010584060
)
[a21ha:link_1] => TCell Object
(
[value] => example
[timestamp] => 1265092697040
)
)
)
how to get the [value] alone in php
print $myArray[0]->columns['a21ha:link_0']->value;
This would give you the first. To get all, you'd have to loop through the contents.
foreach ($myArray[0]->columns as $column) {
print $column->value;
}
Supposed your array called $array:
foreach($array as $arridx => $rowobj){
foreach($rowobj->columns as $cellid => $rowcell){
echo $rowcell->value;
}
}

Categories