How can I turn the following array below so it looks like example 2 using PHP.
Example 1 array.
Array ( [0] => &sub1=a1 [1] => &sub2=aa [2] => &sub3=glass-and-mosaics [3] => &sub4=guides-and-reviews [4] => &sub5=silent-movies [5] => &sub6=even-more-eastern-religions-and-philosophies )
Example 2.
&sub1=a1&sub2=aa&sub3=glass-and-mosaics&sub4=guides-and-reviews&sub5=silent-movies&sub6=even-more-eastern-religions-and-philosophies
can use implode
You can use the implode function.
$arr = array(0 => '&sub1=a1',1 => '&sub2=aa');
$str = implode('',$arr);
just do a $myVar = implode('', $array);
If this is a query fragment of a URL, use http_build_query instead:
echo http_build_query(
'sub1'=>'a1',
'sub2'=>'aa',
'sub3'=>'glass-and-mosaics',
'sub4'=>'guides-and-reviews',
'sub5'=>'silent-movies',
'sub6'=>'even-more-eastern-religions-and-philosophies'
);
Related
I have the following variable:
$checkbox = implode(';', $_POST['product']);
$checkbox is equal to "Product Name;Price;Unit", how can I add a break after every line?
At the moment $checkbox is equal to:
ASFP4040;18.95;1;ASFP4048;21;1;ASGS100100;25.45;1
I need it to be like:
ASFP4040;18.95;1;
ASFP4048;21;1;
ASGS100100;25.45;1;
EDIT:
I am writing this to a .TXT file, \n shows as text and doesn't actually create a new line.
As I'm not sure, how your $_POST['products'] var looks like, you might like one of these options:
If you have everything in a single array element like this
Array
(
[0] => ASFP4040
[1] => 18.95
[2] => 1
[3] => ASFP4048
[4] => 21
[5] => 1
[6] => ASGS100100
[7] => 25.45
[8] => 1
)
you could split the array into chunks and join them together
$data = implode("\n", array_map(function($chunk) {
return implode(';', $chunk);
}, array_chunk($_POST['product'], 3)));
Alternatively, if you have an array of strings like below:
Array
(
[0] => ASFP4040;18.95;1
[1] => ASFP4048;21;1
[2] => ASGS100100;25.45;1
)
a simple implode would be enough
$data = implode("\n", $_POST['product']);
Try this:
echo "'".implode("','",$checkbox)."'<br>";
You can use regular expressions to do this. Just replace my $str with your $checkbox.
$str = 'ASFP4040;18.95;1;ASFP4048;21;1;ASGS100100;25.45;1';
$str2 = preg_replace('/((?:(?:[^;]+);){3})/',"$1\n",$str);
echo $str2;
As explained in Magnus Eriksson's comment and mine, you just have to use "\n" as first parameter of your implode:
$checkbox = implode("\n", $_POST['product']);
Please notice the use of double quotes (") in order for \n to be used as a linebreak.
I have an array that looks like this:
Array
(
[0] => Dog:110
[1] => Cat:111
[2] => Mouse:101
)
Based on that array, I want to create another array to look like this
Array
(
[Dog] => 110
[Cat] => 111
[Mouse] => 101
)
I know this is easy by creating my own function. But is there any way to do this with built in php function. Basically, I know I need to explode(), but is there any way to use this function in conjuction with one php's array functions or will I need to create my own function?
For fun one-liner:
parse_str(str_replace(':', '=', implode('&', $array)), $result);
print_r($result);
Use explode
$new_arr=array();
foreach($yourarr as $v)
{
$v = explode(':',$v);
$new_arr[$v[0]]=$v[1];
}
Demo
I've following array called $user_id_arr
Array
(
[0] => 92ecd33db4ddcdc28e025cae80f00208
[1] => 9def02e6337b888d6dbe5617a172c18d
[2] => a6d22e4cc3f65778a60b359842bcec82
)
Now I want a string containing above array values but each array value should be enclosed in single quotes. For it I tried following code but not succeed.
$user_ids = implode(",", $user_id_arr);
I'm getting following array:
92ecd33db4ddcdc28e025cae80f00208,9def02e6337b888d6dbe5617a172c18d,a6d22e4cc3f65778a60b359842bcec82
Can anyone please help me in this regard please? I want the desired array in following format:
'92ecd33db4ddcdc28e025cae80f00208','9def02e6337b888d6dbe5617a172c18d','a6d22e4cc3f65778a60b359842bcec82'
Thanks in advance.
You can use the implode as the above answers suggested. If you directly want to modify the array then you could make use of array_walk().
<?php
$arr=Array
(
0 => '92ecd33db4ddcdc28e025cae80f00208',
1 => '9def02e6337b888d6dbe5617a172c18d',
2 => 'a6d22e4cc3f65778a60b359842bcec82',
);
array_walk($arr,function (&$val){ $val="'".$val."'";});
echo implode(',',$arr); //<----- This is the one you wanted by the way...
print_r($arr);
OUTPUT :
'92ecd33db4ddcdc28e025cae80f00208','9def02e6337b888d6dbe5617a172c18d','a6d22e4cc3f65778a60b359842bcec82'
Array
(
[0] => '92ecd33db4ddcdc28e025cae80f00208'
[1] => '9def02e6337b888d6dbe5617a172c18d'
[2] => 'a6d22e4cc3f65778a60b359842bcec82'
)
$user_id_arr = array
(
'0' => '92ecd33db4ddcdc28e025cae80f00208',
'1' => '9def02e6337b888d6dbe5617a172c18d',
'2' => 'a6d22e4cc3f65778a60b359842bcec82'
);
$user_ids = "'". implode("','",$user_id_arr)."'";
echo $user_ids;
Try
$user_ids = "'".implode("','", $arr)."'";
See demo
foreach($user_id_arr as $r){
$user_ids = $user_ids."'".$r."',";
}
I want to clear same values in array.
For example so be my array:
array(0=>"1",1=>"1",2=>"3",3=>"1",4=>"6");
I want ot get as:
array(0=>"1",1=>"3",2=>"6");
How?
<?php
$input = array(0=>"1",1=>"1",2=>"3",3=>"1",4=>"6");
$result = array_values(array_unique($input));
print_r($result);
?>
array_unique
Array
(
[0] => 1
[2] => 3
[4] => 6
)
array_values with array_unique
Array
(
[0] => 1
[1] => 3
[2] => 6
)
With a combination of array_unique [docs] and array_values [docs]:
$array = array_values(array_unique($array));
I believe you want the array_unique() function ( http://php.net/manual/en/function.array-unique.php ):
$arr = array_unique(array(0 => '1', 1 => '1', 2 => '2'));
will return:
array(0=> '1', 2 => '2')
I believe you can use array_slice for this purpose. Then edit the keys' values manually
Edit: To remove a key/value pair, call the unset() function on it. from here under Creating/modifying with square bracket syntax section.
I have an array like this:
$categories_array = array(
[0] => 'category_1',
[1] => 'category_2',
[2] => 'category_3',
[3] => 'category_4'
)
I'd like to "filter" the array to get a new one. For example, I'd like to have a new array with only 'category_2' and 'category_3' like this:
$new_categories_array = array(
[1] => 'category_2',
[2] => 'category_3',
)
How can I accomplish this result?
unset($new_categories_array[0]);
unset($new_categories_array[3]);
..might do the trick
See
array_diff — Computes the difference of arrays
array_intersect — Computes the intersection of arrays
Example:
$original = array('category_1','category_2','category_3','category_4');
$new = array_diff($original, array('category_1', 'category_4'));
print_r($new);
Output:
Array
(
[1] => category_2
[2] => category_3
)
When using array_intersect the returned array would contain cat 1 and 4 obviously.
Use preg_grep:
$new_categories_array = preg_grep('/category_[23]/', $categories_array);
While I agree preg_grep is a good solution in your example, if you want a more general case function, look at array_filter - http://ca.php.net/manual/en/function.array-filter.php