For a client, I'm building a link between the client's application and a third party's application. The third party's application expects fields like below:
<General>
<Signing>
<Signing>
<FieldA></FieldA>
<FieldB></FieldB>
<FieldC></FieldC>
</Signing>
<Signing>
<FieldA></FieldA>
<FieldB></FieldB>
<FieldC></FieldC>
</Signing>
</Signing>
</General>
We're building the input for the link in PHP, by means of a multi-dimensional associative array. So the above XML would compute to the following PHP:
'General' => array(
'Signing' => array(
'Signing' => array(
'FieldA' => '',
'FieldB' => '',
'FieldC' => ''
),
'Signing' => array(
'FieldA' => '',
'FieldB' => '',
'FieldC' => ''
)
)
)
There's a few problems.
There's no such thing as duplicate keys in an associative array. The output would be messed up.
The third party NEEDS it like this, this is just how they set up their application.
Even if there was such a thing as duplicate kets in an associative array, there is no way that would outputto what the third party expects. The Signing would just be overwritten with the last Signing item.
My question is, how do I make a dynamic associative array (so the number of Signings can vary) with duplicate Signing keys that still outputs every instance of Signing, instead of just one Signingfield with just the last instance.
I hope this makes sense. I'm foreign and my brain is chaos so I'm terrible at explaining things.
Thanks in advance!
You cannot express the same data structure literally in PHP, period. You'll have to express it differently and your XML-serialiser will have to translate appropriately between the PHP array structure and the expected XML representation. A sensible PHP array structure would be this:
'General' => array(
'Signing' => array(
array('FieldA' => '', 'FieldB' => '', 'FieldC' => ''),
array('FieldA' => '', 'FieldB' => '', 'FieldC' => ''),
)
)
The XML serialiser would do something akin to:
foreach ($array['General']['Signing'] as $signing) {
$xml->General->Signing->appendChild('Signing')
..
}
How exactly to do this depends on your XML serialisation process. Suffice it to say that the data structure doesn't need to be/can't be literally identical and will require a translation layer.
Related
This is driving me mad.. I have a PHP script that returns an array in the form $key => $value and I want to rename the key so that I can display it in a table header. I saw there are several ways of doing this but I'm not sure they are what I need... Either that or I haven't understood the examples correctly which is the likely problem.
Basically my array keys differ each time I iterate over a foreach loop and also some can be blank. How can I get round this?
The first output might look like this:
'_can_chaccess' => false,
'_can_chown' => false,
'_can_delete' => false,
'_can_modify' => false,
'_can_read' => true,
'assigned_to_name_879' => 'Unassigned',
'id' => 1,
'type' => 'Private::Reporting::DataViewModel::DataView_223_42858',
'type_877' => 'Email',
The next run through, I might get this:
'_can_chaccess' => false,
'_can_chown' => false,
'_can_delete' => false,
'_can_modify' => false,
'_can_read' => true,
'assigned_to_name_793' => 'Consultants',
'id' => 1,
'object_reference_794' => 'CASE-1004',
'summary_795' => 'Deployment of New System for HQ (Project)',
'type' => 'Private::Reporting::DataViewModel::DataView_200_42858',
),
As you can see, some keys rename the same e.g. id, type. But the most important ones that I am interested in change each time e.g. Assigned To Name.
Any ideas?
Where do you receive your data from?
You can either somehow modify the source of your data, so if it were a query (what I do not assume here), you have the SELECT ... AS ... statement.
First you do need to know how to interpret the changing keys. If e.g. "assigned_to_name_879" and "assigned_to_name_793" is the same field, you can define a canonical function, which mapps both inputs to a unique output.
The output of the cannonical function and as well the other array keys can serve as keys for an additional array, which contains the table headers of your output.
So your current array is the value's array, and by hand you define a header's array:
array(
'assigned_to_name_879' => 'Name assignment'
);
This dynamic way of storing the table headers in an array only makes sense if you are using the array twice. Otherwise you could simply write the header in the html-code which you do output.
I've managed to figure it out using the below:
$mappings_array = array();
foreach ($report['data'][0] as $key => $value) {
$workbooks->log('Old Key', $key);
preg_match_all('([^_\d]+)', $key, $new_key);
$workbooks->log('New Key', $new_key);
$str = implode(" ", $new_key[0]);
$capitalised = ucwords($str);
array_push($mappings_array,$capitalised);
}
Maybe it's not the best solution but it works :) I get the following output:
> New array: «array (
0 => 'Can Chaccess',
1 => 'Can Chown',
2 => 'Can Delete',
3 => 'Can Modify',
4 => 'Can Read',
5 => 'Id',
6 => 'Total Type',
7 => 'Type',
8 => 'Type',
)
Im looking to use a PHP array obtained from a RESTful service.
They array is just a simple array outputted as follows,
array (
0 =>
stdClass::__set_state(array(
'id' => '375',
'primary_name' => 'Beaufort 3',
'price' => '',
'sqft' => '2435',
'bdrm' => '3',
'bthm' => '2.5',
'display_title' => 'Traditional A1',
'full_img_path' => '',
'thumb_path' => '',
'available_in' => 'a:2:{i:0;s:1:"2";i:1;s:1:"5";}',
'display_first' => '',
)),
)
I'm obtaining the data using file_get_contents but of course its no longer an array at this point. What is the best way to convert this back to a usable array?
I have never seen a service that provides this, probably because it is something people would avoid using, but it does look like it could be something they intend for you to be able to use with eval(). I suppose this could be intended as a convenience. I think they have made an error, though. If you use var_export, (which I assume is how this was generated) on an array like this:
$example = array (
array(
'id' => '375',
'primary_name' => 'Beaufort 3',
'price' => '',
'sqft' => '2435',
'bdrm' => '3',
'bthm' => '2.5',
'display_title' => 'Traditional A1',
'full_img_path' => '',
'thumb_path' => '',
'available_in' => 'a:2:{i:0;s:1:"2";i:1;s:1:"5";}',
'display_first' => '',
)
);
then you would get something you could eval into a variable and use in your code. However, if you var_export an array of anonymous objects instead of an array of associative arrays, you will get the type of response you are getting, which I don't know of any way to use. I would guess they are var_exporting the results of a query and they are using FETCH_OBJ instead of FETCH_ASSOC for the fetch style.
EDIT:
I was looking through the comments on var_export after writing this and came across this way of doing it that should work.
$str = str_replace("stdClass::__set_state", "(object)", $str);
eval('$array=' . $str . ';');
But just because something is possible doesn't mean we should do it.
You can convert it back into an array with eval(). http://php.net/manual/en/function.eval.php
eval('$array='.$response.';');
However, this can be dangerous, because eval will take any PHP code given it - if the service is compromised, your code would execute anything passed to it. JSON, if the service supports it, is much safer and natively supported in PHP since 5.2 via json_decode(). http://php.net/manual/en/function.json-decode.php
I have a multidimensional array or arrays which I also use in my configuration file. The file is also manually edited so I want some of the keys to have fixed position. The code reads configuration file with this array, modifies, insert keys etc and then saves it back. On save I sort the keys but now I found that it is not good enough.
Is there any way to have
the key 'full_name' always as the first key
the key 'version' as second one
and the rest of the keys to be sorted alphabetically?
Sample of the array....
array (
'skroob' =>
array (
'ssh' => 'skroob',
'codebase_path' => '/srv/www/htdocs/imacs/radek/4.0.1',
'ssh_status' => 'ok',
'version' => '4.0.1',
'ssh_last_access' => '2012-Feb-17 10:07:26',
'edu_url' => 'https://testing/imacs/radek/4.0.1',
'full_name' => 'skroob 4.0.1',
),
'testing' =>
array (
'full_name' => 'My beautiful testing server (trunk)',
'version' => 'trunk',
'ssh' => 'testing',
'codebase_path' => '/srv/www/htdocs/imacs/radek/trunk',
'ssh_last_access' => '2012-Feb-17 10:07:26',
'ssh_status' => 'ok',
),
)
Here's one way. It sorts the array alphabetically, by key. Then it reverses the array and unsets the full_name and version keys. Then it adds those keys again, so they get placed at the end of the array. Lastly, it reverses the array again. Kinda hacky but I'm not sure there's a much better way to do what you're asking.
ksort($arr['skroob']);
$arr['skroob'] = array_reverse($arr['skroob']);
$version = $arr['skroob']['version'];
$full_name = $arr['skroob']['full_name'];
unset($arr['skroob']['full_name'], $arr['skroob']['version']);
$arr['skroob']['version'] = $version;
$arr['skroob']['full_name'] = $full_name;
$arr['skroob'] = array_reverse($arr['skroob']);
Another solution is to use uksort and write a small callback function. That would probably look a bit more professional.
I have an array that I would like to sort, that should be easy, except that I build my array in a strange manners (for different reasons) that make it hard to sort. Here is my array :
$arrayCEO =array(
'companyName' => array(0=>'name1', 1=>'name2'),
'link' => array (0=>'link1', 1=>'link2'),
'isin' => array (0=>'isin1', 1=>'isin2'),
'mktCap' => array (0=>'mktCap1', 1=>'mktCap2'),
'nbShares' => array (0=>'nbShares1', 1=>'nbShares2'),
'amount' => array (0=>'10', 1=>'20'));
Is that possible to sort by "amount" without breaking the order in the others arrays?
Do you recommend me to rewrite my code to build an array like this one :
$arrayCEO =array(
0 => array ('name' => 'name1', 'link' => 'link1', 'isin' => 'isin1', …),
1 => array ('name' => 'name2', 'link' => 'link2', 'isin' => 'isin2', …));
2 => ...
I know this one would be simple to sort but it's a lot of work to rewrite my piece of code.
Thanks,
Dorian
It makes a lot more sense to rewrite your arrays to store in the more conventional format you described - (It's always better to organise things in decreasing magnitude 'Root > rows > fields' instead of 'Root > fields > rows')
If you didn't want to change you could probably use the usort function and roll your own sorting method, but it might be a bit of work to make it play nicely.
I'm probably missing something simple here but I can't seem to find a way to build a query that will allow me to update a match in a group of nested values.
I have a document like this for a blog app I've been working on (currently uses MySQL):
array (
'_id' => new MongoId("4bc8dcee8ba936a8101a0000"),
'created' => '20100418-201312 +0000',
'post-title' => 'Some Post Title',
'post-body' => 'Blah Blah Blah Blah.',
'post-blog-name' => 'default',
'post-comments' =>
array (
0 =>
array (
'comment-title' => 'Test1',
'comment-body' => 'asdf1',
'created' => '20100418-214512 +0000',
'owner' => 'User1',
),
1 =>
array (
'comment-title' => 'Test2',
'comment-body' => 'asdf2',
'created' => '20100418-214512 +0000',
'owner' => 'User2',
),
),
'owner' => 'zach',
'updated' => '20100418-201312 +0000',
)
I'd like to be able to build a query that can search 'comment-title' for a match and then allow me to update/change/delete data as needed.
Obviously I can perform an update using a query which includes the key value. Something like this works:
$collection->update(
array("post-comments.0.comment-title" => $_POST['comment-title']),
array('$set' => array('entries.0' => array('comment-title' => $_POST['comment-title'], 'comment-body' => $_POST['comment-body'], 'owner' => $_SESSION['username'], 'updated' => gmdate('Ymd\-His O')))));
But I expect I'm missing something that would allow me to leave out the key and still be able to match one of the nested arrays based on a value (in this example the 'comment-title').
Anyway, sorry, this probably isn't the best example and I probably will end up using the keys in comments to identify them (comment #) but since nesting and creating rather complex objects seem to be a few of Mongodbs strong points I'm just hoping someone can point out what it is I might be missing.
A query to remove or update all comments by a specific user (say a user the blog author just black-listed) might be a better example. I'm not sure how I'd do this short of pulling out the entire document and then iterating through the nested arrays using PHP.
try ... notice I removed the "key"
$collection->update(array("post-comments.comment-title" ...
Cheers!