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',
]
Related
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]);
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);
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 have an associative array with strings as keys and I want to list all the keys and corresponding values in the order in which it was input. For example:
$arr=array();
$arr['tree']='leaves';
$arr['fruits']='seed';
and output should be like :
keys : tree, fruits
values : leaves,seed
If you're running a version of PHP that supports array dereferencing:
$key = array_keys($arr)[1];
else
$keys = array_keys($arr);
$key = $keys[1];
make it associative array you can get both keys and values
$arr=array('tree'=>'leaves','fruits'=>'seeds');
foreach($arr as $key=>$value)
{
echo $key."====>".$value;
}
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 8 years ago.
Improve this question
I have two arrays inventoryStock and posStock (point of sale stock) they both use product sku numbers as the key and the value is the quantity on hand I need to somehow update the posStock with the values from $inventoryStock where there keys are matching.
Examples of arrays:
inventoryStock{
abs-0098 => 5,
abs-0099 => 23,
abs-0100 => 8,
abs-0101 => 19
}
posStock{
abs-0098 => 5,
abs-0099 => 23,
abs-0101 => 15
}
I need the posStock to be the same as the inventoryStock I cannot just make posStock be inventory stock becasue inventory stock has extra products not listed in the Point of sale.
You can use array union.
The + operator returns the right-hand array appended to the left-hand array; for keys that exist in both arrays, the elements from the left-hand array will be used, and the matching elements from the right-hand array will be ignored.
In your case (if I understand the spec correctly):
$newPOSStock = $inventoryStock + $posStock;
You are looking for the array_key_exists() function of PHP.
foreach ($inventoryStock as $key => $value) {
if (array_key_exists($key, $posStock)) {
$posStock[$key] = $value;
continue; // Continue Loop
}
// Do something if the array key doesn't exist.
}
To expand on why I would do it this way. I now have a logic block to allow me to do something if the array key doesn't exist like add it to the PosStock, or if I want to or change values of other variables to trigger other behaviors.
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]);