0

After hours going around in circles I gave up and ask for help.

I have an array which looks like:

[field01] => Array
 (
 [name] => test01
 [prefix] => C01
 )
[field02] => Array
 (
 [url] => http://www.url.com
 [user] => a_user
 [password] => a_password
 )
[filed03] => Array
 (
 [0] => Array
 (
 [id] => 1
 [Type] => standard
 [Name] => name
 )
 [1] => Array
 (
 [id] => 5
 [Type] => standard
 [Name] => name
 )
 )

Now I want to go through that array and get the following output as a return from a recursive function:

Array (
 [0] = "The values of field01: name, prefix - test01, C01"
 [1] = "The values of field02: url, user, password - http://www.url.com, a_user, a_password"
 [2] = "The values of field03: id, Type, Name - 1, standard, name"
 [3] = "The values of field03: id, Type, Name - 5, standard, name"
)

I have tried it with a recursive function but stuck with field03 to save the name.

Any help for that?

UPDTAE:

here is the array, so you don't have to write it:

$data['field01']['name'] = "field01";
$data['field01']['prefix'] = "C01";
$data['field02']['url'] = "http://www.url.com";
$data['field02']['user'] = "a_user";
$data['field02']['password'] = "a_password";
$data['field03'][0]['List_id'] = 1;
$data['field03'][0]['Type'] = "standard";
$data['field03'][0]['Name'] = "name";
$data['field03'][1]['List_id'] = 5;
$data['field03'][1]['Type'] = "standard";
$data['field03'][1]['Name'] = "name";

Thanks in advance!

asked Aug 5, 2011 at 3:24
2
  • Post some of the code you've attempted. Commented Aug 5, 2011 at 3:37
  • Hi Dave. I've tried a lot of ways on paper and as code. I can post you one but I guess it will confuse more as it helps. And Ivan already post a possible code. :) Thanks Commented Aug 5, 2011 at 3:55

2 Answers 2

1
<?php
$data['field01']['name'] = "field01";
$data['field01']['prefix'] = "C01";
$data['field02']['url'] = "http://www.url.com";
$data['field02']['user'] = "a_user";
$data['field02']['password'] = "a_password";
$data['field03'][0]['List_id'] = 1;
$data['field03'][0]['Type'] = "standard";
$data['field03'][0]['Name'] = "name";
$data['field03'][1]['List_id'] = 5;
$data['field03'][1]['Type'] = "standard";
$data['field03'][1]['Name'] = "name";
$text = '';
foreach ($data as $k => $fields) {
 if (isset($fields[0])) {
 foreach ($fields as $field) {
 $text .= 'The values of ' . $k . ': ' . traverse($field) . "\n";
 }
 } else {
 $text .= 'The values of ' . $k . ': ' . traverse($fields) . "\n";
 }
}
echo $text;
function traverse($fields) {
 $keys = array_keys($fields);
 $values = array_values($fields);
 return implode(', ', $keys) . ' - ' . implode(', ', $values);
}
?>
answered Aug 5, 2011 at 3:44
0
1

This assumes if there is an array of arrays, it is all arrays. In other words, you aren't mixing strings with arrays.

$resultArray = array();
function process($inputArray, $fieldName) {
 $result = array();
 $keys = array_keys($inputArray);
 if (!is_array($inputArray[0])) {
 $result[] = "The value of $fieldName: ".implode($keys,', '). ' - '. implode($inputArray, ', ');
 } else {
 foreach($inputArray as $arr) {
 $result = array_merge($result, process($arr, $fieldName));
 }
 }
 return $result;
}
foreach($data as $k=>$v) {
 $processed = process($v, $k);
 $resultArray = array_merge($resultArray, $processed);
}
print_r($resultArray);
answered Aug 5, 2011 at 3:58
0

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.