PhpStorm indent multi-line array elements - php

How to configure PhpStorm's PHP coding style settings so this code:
<?php
$array = [
'element1' => 'value1',
'element2' => 'value2',
'element3' => 'value3',
];
Is reformatted like this:
<?php
$array = [
'element1' => 'value1',
'element2' => 'value2',
'element3' => 'value3',
];
Problem is that even if I format arrays like I want, any call to reformat option will "destroy" that formatting and bring me back to unindented elements.
I am interested in short array declaration only.

Based on your current code style: please change Continuation indent from 0 to the desired value (I guess it will be 4, based on your code sample).
This can be done for all languages at once (General node) or just PHP.

Related

Return textfile as multidimensional array

Hope everything is good.
I use php file() and it works very well. I only need to separate my "values" in the txt-file with a new row, and then 'file()' will give me the contents of the txt-file as an array with all the values separately.
I do not know if I can take the same function one step further to achieve 'key/values' and 'multidimensional arrays'. If not, what do I have for other options to be able to save 'text data' in a txt file and then get it back in a multidimensional array?
At the moment, I only get the following:
[0] => 'value1',
[1] => 'value2',
[2] => 'value3',
If you know any solutions that are very straightforward and can put me on the right track here, I am very grateful.
A simple primer for doing this with JSON:
// example array of data
$myarray = array();
$myarray[] = ['name' => 'value 1','age' => '31','city' => 'nowhere'];
$myarray[] = ['name' => 'value 2','age' => '12','city' => 'somewhere'];
$myarray[] = ['name' => 'value 3','age' => '67','city' => 'anywhere'];
print_r($myarray);
// to save your array to a file
file_put_contents('/path/to/file.json',json_encode($myarray));
// now to retrieve:
$myarray = json_decode(file_get_contents('/path/to/file.json'),true);
print_r($myarray);
By doing it this way, you retain those keys you want, and the values. The JSON encoding/decoding handles all the painful bits of storing it as text in a flatfile.

Merge Twitter and Instagram API Results and Order by Date

I am using the https://github.com/J7mbo/twitter-api-php Twitter API PHP wrapper and the https://github.com/cosenary/Instagram-PHP-API#authenticate-user-oauth2 Instagram API PHP wrapper, both are functioning and displaying the results as I intended. However, what I was hoping to accomplish was merging both results and ordering them by date, newest to oldest, and displaying them in a masonry fashion. My PHP isn't the strongest so I am really just looking for guidance to get me on the right path. The code now is pretty basic, basically a couple of foreach loops, one for Instagram and one for Twitter a majority of the work is handled by the wrappers by passing in the appropriate keys/tokens.
For Twitter each tweet has 'created_at' and that is the very first piece of data returned:
[ { "created_at":"Wed Mar 18 19:33:49 +0000 2015"
Then based on the wrapper I am using I can access it within my foreach loop by doing:
$tweetDate = date('F j, Y', strtotime($tweets->created_at));
Instagram uses created_time and based on the wrapper I can access it using:
echo date('F j, Y', $entry->created_time);
Hopefully that helps but please let me know if you'd like to see more.
You didn't paste in samples of your data, but since you mentioned foreach loops I'll assume you've got two arrays of results that you want to merge and sort...
For merging nested arrays, you'll want either array_merge_recursive($array1, $array2) or array_replace_recursive($array1, $array2). The difference between the two is how they handle duplicate keys in the two arrays: array_merge_recursive() will preserve both values for a duplicate key in a nested array, wheres array_replace_recursive() will overwrite the values of $array1 with the values in $array2 for duplicate keys.
For example:
$array1 = ['key1' => 'original value', 'key2' => 'value2'];
$array2 = ['key1' => 'overlapped value', 'key3' => 'value3'];
$mergeResult = array_merge_recursive($array1, $array2);
$replaceResult = array_replace_recursive($array1, $array2);
$mergeResult will look like this:
[
'key1' => [
'original value',
'overlapped value'
],
'key2' => 'value2',
'key3' => 'value3'
]
$replaceResult will look like this:
[
'key1' => 'overlapped value',
'key2' => 'value2',
'key3' => 'value3'
]
It's up to you which is more appropriate for your application.
If you're working with nested arrays, then to sort the array resulting from your merge, you'll want to use usort(). Here's a simplified example that assumes that the value you want to sort by is 1 level deep in a nested array:
function sortByDate($a, $b) {
return $a['date'] - $b['date'];
}
usort($array1, 'sortByDate');
Here are docs for array_merge_recursive, array_replace_recursive, and usort.
I assume that that the results of both API calls contain some date key/value that can be used to sort the data. You can merge the results in data variable (array) and then use:
function dateSort($a, $b)
{
return strtotime($a) >= strtotime($b);
}
usort($data, "dateSort");
The format of data should be something like this
Array
(
[0] => '2015-04-09',
[1] => '2015-03-15',
[2] => '2015-04-06'
)
The output would be
Array
(
[0] => '2015-03-15',
[1] => '2015-04-06',
[2] => '2015-04-09'
)

Odd array_merge_recursive behavior with string keys

I'm trying to use array_merge_recursive to merge two data structures.
<?php
$testSite = array(
'name' => 'test site',
'modules' => array(
'foo' => 'true',
'bar' => 'true'
)
);
$testData = array(
'modules' => array(
'bar' => 'false'
)
);
$testSite = array_merge_recursive($testSite, $testData);
Note that I'm using strings instead of booleans for debug printing purposes
I would expect $testSite to be the exact same after this code has ran, except for the modules.bar property, which I'd expect to see being changed to false. What happens instead, as seen in this live example, is that bar is turned into an array containing it's old value and the value false is appended to that.
The documentation page reads that this is what will happen for numeric keys, but these are all strings keys. Can anyone shed some light on this?
I think you want array_replace_recursive.
array_merge_recursive() vs. array_replace_recursive()

Recursively iterate over an array to change specific key's value

I have an array like this:
$a = array(
'key1' => 'value1',
'key2' => 'value2',
'key3' => array(
'key4' => 'value4',
'key5' => array(
'key6' => 'value6'
)
)
);
as you can see there are inner arrays inside $a
Now, I have a list of keys, example:
key1
key4
key6
I need a script that search if those key exists, and if exists change their values.
I Need to change their values with base64_encode($value_of_the_key)
so Maybe a callback that get the current value and convert it using base64_encode() function.
COuld someone help me?
I'm tring to see the current php functions but it seems there is not ones that do this thing.
THanks
EDIT:
Using the follow code i can get the keys in the callback....but the problem is:
How can i modify the values directly in the array? I Mean.... ok ... i get key and value, but how to change the value in the original array? ($a)
$a = array(
'key1' => 'value1',
'key2' => 'value2',
'key3' => array(
'key4' => 'value4',
'key5' => array(
'key6' => 'value6'
)
)
);
function test($item, $key)
{
echo "$key. $item<br />\n";
}
array_walk_recursive($a, 'test');
array_walk_recursive() with callback supplied should help. More info here.

take php string which contains square brackets as string not array

I have a weird problem here. I'm using an associative array in php (using cakePHP) which has the following form:
$my_array = array(
'data['a']['b'] => 'value1',
'data['b']['c'] => 'value2',
'data['b']['d'] => 'value3',
'data['e'] => array(
'data['e1']['e2']' => 'value3',
'data['e1']['e3']' => 'value4'));
The problem I'm having is that
'data['e1']['e2']' => 'value3' and 'data['e1']['e3']' => 'value4'
are taken like an array like this:
'data['e1']' => array(
['e2'] => 'value3',
['e3'] => 'value4');
I don't want these to be taken as arrays, I want them to be taken as key and value of the array 'data['e']'. As a matter of fact, I want all the elements of the arrays $my_array and 'data['e']' to be taken as keys and values of the corresponding array (not as arrays).
Any help please?
P.S This seems to happen only when I do a debug on cakePHP, if I don't use cakePHP everything seems to be fine and "data" comes from a cURL posted data to cakePHP
Your code is invalid PHP. My best guess is that it should look like this:
$my_array = array(
$data['a']['b'] => 'value1',
$data['b']['c'] => 'value2',
$data['b']['d'] => 'value3',
$data['e'] => array(
$data['e1']['e2'] => 'value3',
$data['e1']['e3'] => 'value4'));
Please show us the contents (for instance, using print_r) of $data.
POSTed data in a certain syntax is automatically parsed into $_POST as array. If you want to get the raw input, use file_get_contents('php://input'). See http://php.net/manual/en/wrappers.php.php.

Categories