Comma separated list into a non associated array in PHP - php

I have a string of emails:
$emails = 'email1#example.com, email2#example.com,email3#example.com';
and I need a basic array like [one,two,three] and not [0 => 'one', 1 => 'two', 2 => 'three']. I guess is a non-associative array?
How to do that? I have tried the inbuild 'explode()' PHP function but it creates 0 => one, ... instad of a a simple array email1,email2,..
Besides, explode has problem when there is a space after the comma, so I have to use ', ' or ',' but can't use both.
What PHP function is the best to convert a list of email addresses into a simple non-associative array? Thanks in advance.

First you can remove spaces by str_replace
$emails = str_replace(' ', '', $emails);
and I need a basic array like [one,two,three] and not [0 => 'one', 1 => 'two', 2 => 'three']. I guess is a non-associative arr
In php simple arrays has numeric keys, so it's correct

Related

Given array keys that correspond to another array, how can I merge them?

I have the following array that is supposed to only be keys:
$keys = ['mod_4_key'];
and the bigger array which contains a lot of information:
$big_array = [ 'mod_4_key' => ['old' => '', 'info' => ''], 'mod_5_key' => ..]
I would like to, based on what is inside $keys generate a new array with the information from $big_array, as such, if we are to compute the "non-difference" between the arrays, the output should be:
$final_array = [ 'mod_4_key' => ['old' => '', 'info' => '']]
I achieved this using a classic foreach but I was wondering if there was no in-built way to achieve this.
You may be better off with a simple foreach() loop, but there are probably several ways of achieving this.
This uses array_flip() on the $keys, so that you end up with another associative array, then use array_intersect_key() with the big array first.
$final_array = array_intersect_key($big_array, array_flip($keys));

is there a standard PHP function for transfering an associative array elements to another associative array

Is there a standard PHP function that changes an associative array's index names?
$a1 = array('one'=>1,'two'=>2,'three'=>3);
$new_index_names = array('one'=>'ono','two'=>'dos','three'=>'tres');
$a2 = change_index_names($a1,$new_index_names);
print_r($a2);
// $a2 should have the index names changed accordingly and look like this:
// array('ono'=>1,'dos'=>2,'tres'=>3)
EDIT
Please note that the function needs to know the mappings to the new index names. Meaning, in $new_index_names array provides the mappings. So again, it needs to know that 'ono' is the new index name for 'one'.
EDIT
I know you guys can come up with your own solution, i was wondering there is a standard PHP function that already does this.
EDIT
There are several situations where changing index names would help:
1) separates post value names to generic/internal names so you can separate
your backend code from front-end code.
2) say for instance you have two arrays from post that need to go through the
same exact process, except both arrays although mean/contain same exact type
of values/order/structure, they're index names are different. So when
passing to a function/method that goes by only a set of index names, you'll
need to convert the index names before passing them to that function/method.
You don't need array_values To get your desired output you can just use
array_combine($new_index_names,$a1);
not in just 1 function, but you could use array_values to get the values of the first array, and array_combine to set the new keys
Assuming both arrays has equal count, one way is with array_combine() and array_values()
$a2 = array_combine(array_values($new_index_names), $a1);
The previous answers does not consider the order of $a1 and $new_index_names, so I put my solution following:
$a1 = array('one' => 1, 'two' => 2, 'three' => 3, 'zero' => 0);
$new_index_names = array('zero' => 'zero', 'one' => 'ono', 'two' => 'dos', 'three' => 'tres');
array_combine(
$new_index_names,
str_replace(array_keys($a1), array_values($a1), array_keys($new_index_names))
);
Array
(
[zero] => 0
[ono] => 1
[dos] => 2
[tres] => 3
)
The best answer I've seen thus far:
foreach ($a1 as $k => $v) $a2[$new_index_names[$k]] = $v;
Credits go to jh1711

Replace numbers with array

how to put integer values, taken by another array, instead of 1,2,3 like this?
$arr_id = array(1,2,3); //this arr_id go to line 2 instead of 1,2,3
'id' => array('$in' => array(1,2,3))
well, putting $arr_id instead of "1,2,3"... something like this
'id' => array('$in' => array($arr_id))
the problem is that 1,2,3 are number, but in my $arr_id i've strings. If i try to convert string to int i've a problem with "," too
here
array($arr_id))
i need to have integers (not string) taken by $arr_id, separated by ","...
You can convert a string array to an int array in PHP using:
$arr_id_str = array("1","2","3");
var_dump(array_map('intval',$arr_id_str));

Explode string with two different separators to create multidimensional array

I have a string:
01;Tommy;32;Coder&&02;Annie;20;Seller
I want it like this:
array (size=2)
0 =>
array (size=4)
0 => string '01' (length=2)
1 => string 'Tommy' (length=5)
2 => int 42
3 => string 'Coder' (length=5)
1 =>
array (size=4)
0 => string '02' (length=2)
1 => string 'Annie' (length=5)
2 => int 20
3 => string 'Seller' (length=6)
Hope you can help me, thank you!
Not sure if the datatypes will be matching (as I believe it's all in a string) but here's the code
$myarray = array();
foreach(explode("&&",$mystring) as $key=>$val)
{
$myarray[] = explode(";",$val);
}
The explode command takes a string and turns it into an array based on a certain 'split key' which is && in your case
but since this is a dual array, I had to pass it through a foreach and another explode to solve.
It's very simple. First you need to explode the string by && and then traverse through array exploded by &&. And explode each element of an array by ;.
Like this,
<?php
$str="01;Tommy;32;Coder&&02;Annie;20;Seller";
$array=explode("&&",$str);
foreach($array as $key=>$val){
$array[$key]=explode(";",$val);
}
print_r($array);
Demo: https://eval.in/629507
you should just have to split on '&&', then split the results by ';' to create your new two dimensional array:
// $string = '01;Tommy;32;Coder&&02;Annie;20;Seller';
// declare output
$output = [];
// create array of item strings
$itemarray = explode('&&',$string);
// loop through item strings
foreach($itemarray as $itemstring) {
// create array of item values
$subarray = explode(';',$itemstring);
// cast age to int
$subarray[2] = (int) $subarray[2]; // only useful for validation
// push subarray onto output array
$output[] = $subarray;
}
// $output = [['01','Tommy',32,'Coder'],['02','Annie',20,'Seller']];
keep in mind that since php variables are not typed, casting of strings to ints or keeping ints as strings will only last depending on how the values are used, however variable type casting can help validate data and keep the wrong kind of values out of your objects.
good luck!
There is another appropach of solving this problem. Here I used array_map() with anonymous function:
$string = '01;Tommy;32;Coder&&02;Annie;20;Seller';
$result = array_map(function($value){return explode(';',$value);}, explode('&&', $string));

multidimensional array with only one value, how to get properly the value?

I work with a CMS (Drupal 8). It automatically generates some multidimensional array with an unique value, like this :
//var_dump of my $array
array (size=1)
0 =>
array (size=1)
'value' => string '50' (length=2)
To date, I use this ugly way for automatically get the value (in example : "50") of these arrays :
array_shift(array_values(array_shift(array_values($array))))
My question is, is there a better way in php for get that ?
So you know it's an array in an array?
$value = reset(reset($array));
You don't know how many turtles arrays are nested?
$value = $array;
while(is_array($value))
$value = reset($array);
Docs on reset
Just use via index:
$array[0]['value']

Categories