PHP Vertical String to Horizontal String [closed] - php

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 5 years ago.
Improve this question
i got the result my for loop :
FFFF
AAAA
TTTT
EEEE
while mod=4 my loop echo br
but i want result like this :
FATE
FATE
FATE
FATE
how i do this in php ?
thanks for answers and sorry my english:D

This is almost a duplicate of:
How to restructure multi-dimensional array with columns as rows? and
Combining array inside multidimensional array with same key
This can be done with foreach loops, but I like the condensed variadic method (PHP 5.6+).
You can research the aforementioned links to see the other techniques if your version isn't high enough or you want something different.
The strings just need to be converted to arrays before rotating, then imploded() after rotating.
Code: (Demo)
$input=['FFFF','AAAA','TTTT','EEEE'];
$rotated=array_map(function(){return implode(func_get_args());},...array_map('str_split',$input));
var_export($rotated);
Output:
array (
0 => 'FATE',
1 => 'FATE',
2 => 'FATE',
3 => 'FATE',
)
Here is a less fancy method to achieve the same result:
$input=['FFFF','AAAA','TTTT','EEEE'];
$length=strlen($input[0]);
foreach($input as $string){
for($offset=0; $offset<$length; ++$offset){
if(!isset($rotated[$offset])){$rotated[$offset]='';}
$rotated[$offset].=$string[$offset];
}
}
var_export($rotated);

Related

Passing Numbers in array format - Laravel [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 2 years ago.
Improve this question
I am trying to convert string to an array format in my code below. I tried to use explode but it returns my results as
In my code i have
File.php
$dial_deustche_no = $payloadArray[1];
dial_deustche_no = 49744990,49010101 //result
$numbers = json_encode([0 => $dial_deustche_no]);
$numbers =. ["49744990,49010101"] //result
When i use explode results looks like
explode(',', $numbers);
//results
array (
0 => '["49744990',
1 => '49010101"]',
)
This is how i want my results to look like
$numbers = ['49744990','49010101']
PS: Beginner in laravel PHP
Explode it before doing the json_encode
$numbers = explode(',', $payloadArray[1]);

CSV to key=>value array in PHP [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 7 years ago.
Improve this question
I have a simple CSV in the form:
Joe,2
Peter,321
Jane,34
I need to convert this to a key=>value array as follows:
$result = array(Joe=>2,Peter=>321,Jane=>34)
I can read the CSV file but am unsure how to loop through each line of the CSV to assign the keys and values.
Many thanks.
Here's a cheap one-liner to do it:
$result = array_column(array_map('str_getcsv', file('data.csv')), 1, 0);
It makes use of:
file() to read the file into an array, giving (newlines omitted for brevity, they disappear in the next step anyway):
["Joe,2", "Peter,321", "Jane,34"]
array_map() to apply str_getcsv() to each element of that array, parsing the comma separated values, giving:
[['Joe', '2',], ['Peter', '321',], ['Jane', '34',]]
array_column() to take that array, grab each element at index 1 from the sub-arrays to use as values, and those at index 0 to use as keys, giving the final result:
[
'Joe' => '2',
'Peter' => '321',
'Jane' => '34',
]

PHP string assign to array variable [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 9 years ago.
Improve this question
I have written some php code, trying what is to assign a string (tag_of_organization) to a variable and then latter to assign it to array newvar. As i am totally new to php and search a lot for it but cannot find anything, so i am asking it here how to do this. here it is
$organ='tag_of_organization';
$newvar = array();
$newvar["tag_of_organization"] =$organ;
Try with:
$newvar[$organ] = $organ;
You are already correct. put print_r to test print your array
$organ='tag_of_organization';
$newvar = array();
$newvar["tag_of_organization"] =$organ;
print_r($newvar);
Output
Array ( [tag_of_organization] => tag_of_organization )
Update
you want dynamic result
$organ='tag_of_organization';
$newvar = array();
$newvar[$organ] =$organ;
print_r($newvar);
Output
Array ( [tag_of_organization] => tag_of_organization )

How to remove some part of text in the first index of php array [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 9 years ago.
Improve this question
This is my array in php
$a is my array
Array
(
[0] => class="date-display-single">24-Feb-2013
[2] => 11:35
[3] => AM
)
How do I remove class="date-display-single"> from array[0]?
Several ways... But the simplest one is to do:
$a[0] = str_replace('class="data-display-single">', '', $a[0]);
This simple statement should do exactly that:
$a[0] = substr($a[0], strpos($a[0], '>') + 1);
That said, it all depends on how you ended up with that array in the first place; it seems things can be fixed higher up in the code.
there you are:
$a[0] = str_replace('class="date-display-single">','',$a[0]);
but i would do it in the string, before you explode your date string. no in the array after
Check out unset()
You could try something like:
unset($a[0]);
Try this
str_replace('class="date-display-single">', '', $a[0]);

Make a 1 item array be seen as an array [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 8 years ago.
Improve this question
I am currently working on a php project where I am trying to loop through an array using a foreach. However, sometimes the array may only contain 1 item so when I try and do a foreach it fails as 1 item is just seen as a normal variable.
Is there a way that I can trick php into thinking that a 1 item array is actually an array and not just a variable so that I don't get this error.
Thanks for your help.
foreach will work fine with arrays of size 0,1 or bigger. I suspect your problem is, that the variable doesn't really contain an array, but some scalar value - in this case use something like
if (!is_array($var)) $var=array($var);
foreach ($var as $item) {
//...
}
if(is_array($arr))
$arr2=$arr
else
$arr2=array($arr)
and then you iterate over $arr2
I would recommend just using a standard for loop. It should work no matter what the length of the array is
for($i = 0, $l = count($myArray); $i < $l; $i+=1){
//code in here
}
But chances are you have an issue with your array to begin. Posting the structure would be helpful, or you should var_dump it to make sure it is indeed an array.

Categories