i have a array and how can i cover it to string fastest ?
Array
(
[1] => 1,
[2] => 8,
[3] => 10,
[4] => 16,
)
I need cover it to this string $var = (1,8,10,16)
Just implode it:
$var = '(' . implode(',', $data) . ');
Live example: https://3v4l.org/9bsZF
you can use simple implode function for that .
$arr = array(
"1" => 1,
"2" => 8,
"3" => 10,
"4" => 16
);
echo implode($arr, ",");
output : 1,8,10,16
OR
foreach ($arr as $value) {
echo $value .", ";
}
Output : 1, 8, 10, 16,
Related
i have this array like this :
array(2)(
['id'] => "59898441545",
['total'] =>
array(
[0] => 2,
[1] => 5
[2] => 10
[3] => 35
)
);
I would like the returned array to be the same but, the key "total" to not be a aarray but the sum of all the precedemtn value like this :
array(2)(
['id] => "59898441545",
['total'] => 52 // the sum of the precedent elements
);
P.S : the number of elements in the "total array" may change
Any help ? thx in advance
$youarray['total'] = array_sum($youarray['total']);
You can try this:
$sum = 0;
foreach($mainArr['total'] as $arr) {
$sum += $arr;
}
$mainArr['total'] = $sum;
You can try this code.
<?php
$data = array('id' => "59898441545",
'total' => array(
0 => 2,
1 => 5,
2 => 10,
3 => 35
)
);
$data['total'] = array_sum($data['total']);
echo "<pre>";print_r($data);
$b = array("a" => 1.2, "b" => 2.3, "c" => 3.4);
echo "sum(b) = " . array_sum($b);
*Output* : sum(b) = 6.9
I try to create a multimentional array , I have a list of title
$array_title = array(
"fey palin" => 3,
"big theory" => 3,
"nyc through" => 3,
"jonas storm" => 3,
"bang theory" => 3,
"bang big" => 3,
"storm test" => 3,
"plain sahra" => 3,
"mind woolf" => 3,
"mind virginia" => 3
);
and I want to create a subarray if I find a link between two title for example big theory and bang theory are link by the word "theory"
Here my full code :
$array_title = array(
"fey palin" => 3,
"big theory" => 3,
"nyc through" => 3,
"jonas storm" => 3,
"bang theory" => 3,
"bang big" => 3,
"storm test" => 3 ,
"plain sahra" => 3,
"mind woolf" => 3,
"mind virginia" => 3
);
$new_array = array();
$i = 0;
foreach($array_title as $title => $value){
if(count($new_array) > 0){
$words = explode(' ',$title);
if(preg_grep ('/\b('.$words[0].'|'.$words[1].')\b/i', $new_array)){
$result = preg_grep ('/\b('.$words[0].'|'.$words[1].')\b/i', $new_array);
reset($result);
$first_key = key($result);
$new_array[$first_key][] = $title;
}else{
$new_array[$i] = $title;
}
}else{
$new_array[$i] = $title;
}
$i++;
}
it s seems i cant do that
$new_array[$first_key][] = $title;
I have an error which is:
[] operator not supported for strings
Edit : I changed my code a bit but I have some duplicate value
Here is my updated code
$array_title = array("fey palin" => 3 , "big theory" => 3 , "nyc through" => 3 ,
"bang theory" => 3 , "jonas storm" => 3 , "bang big" => 3 ,
"storm test" => 3 , "plain sahra" => 3 ,"mind woolf" => 3, "mind virginia" => 3);
$new_array = array();
foreach($array_title as $title => $value){
$words = explode(' ',$title);
if(count($new_array) > 0){
foreach($new_array as $key => $value){
if(preg_match('/\b('.$words[0].'|'.$words[1].')\b/', $key)){
$new_array[$key][$title] = $title;
}else{
if(count($value) > 0){
foreach($value as $val){
if(preg_match('/\b('.$words[0].'|'.$words[1].')\b/', $val)){
$new_array[$key][$title] = $title;
}
}
}else{
$new_array[$title] = array();
}
}
}
}else{
$new_array[$title] = array();
}
}
and the output:
Array
(
[fey palin] => Array
(
)
[big theory] => Array
(
[bang theory] => bang theory
[bang big] => bang big
)
[nyc through] => Array
(
)
[bang theory] => Array << To remove
(
[bang big] => bang big
)
[jonas storm] => Array
(
[storm test] => storm test
)
[bang big] => Array
(
)
[storm test] => Array << To remove
(
)
[plain sahra] => Array
(
)
[mind woolf] => Array
(
[mind virginia] => mind virginia
)
[mind virginia] => Array
(
)
)
The problem is this line:
$new_array[$i] = $title;
This is setting the initial elements of $new_array to strings, not an array. So when you later do
$new_array[$first_key][] = $title;
it's trying to do array_push on a string.
That line should be:
$new_array[$i] = array($title);
Add this statement before the one that triggers the error:
if (!isset($new_array[$first_key])) $new_array[$first_key] = array();
This way you ensure that $new_array[$first_key] is initialised as an array and you can safely add elements to that array.
But your algorithm will still fail, as you use that same array $new_array in your preg_grep call. That will not work as you want when you have added sub-arrays to $new_array.
You need to two variables: one for doing the preg_grep, which you do not change, and one where you add to in your loop. Currently you are mixing these two...
I have array like below:
Array
(
[22] => Array
(
[0] => 60
[29] => Array
(
[0] => 6
)
[30] => Array
(
[0] => 5
[1] => 8
)
[31] => Array
(
[0] => 7
[1] => 9
[2] => 14
[3] => 26
)
)
[23] => 12
[35] =>10
[42] =>22
)
now i want to implode array like
60[6][5||8][7||9||14||26]|12|10|22
I have tried below code:
$arr = array_map(function($el){ return $el['tag_id']; }, $arr);
$str = implode(',', $arr);
But it is not implode with required glue
How can i do it?
you can use this code
<?php
$a= Array(
22 => Array(
0 => 60,
29 => Array(
0 => 6
),
30 => Array
(
0 => 5,
1 => 8
),
31 => Array
(
0 => 7,
1 => 9,
2 => 14,
3 => 26
),
),
23 => 12,
35 =>10,
42 =>22,
);
$string='';
foreach($a as $arr){
if(is_array($arr)){
foreach($arr as $array){
if(is_array($array)){
$string .= '['.implode("||",$array).']';
}else{
if($string!==''){ $string .= '|';}
$string .= $array;
}
}
}else{
if($string!==''){ $string .= '|';}
$string .= $arr;
}
}
echo $string;die;
?>
Out put wil be
60[6][5||8][7||9||14||26]|12|10|22
Desired result without foreach.
$array = [
22 => [
0 => 60,
29 => [
0 => 6
],
30 => [
0 => 5,
1 => 8
],
31 => [
0 => 7,
1 => 9,
2 => 14,
3 => 26
]
],
23 => 12,
35 => 10,
42 => 22
];
$result = implode('|', array_map(function($item)
{
return is_array($item) // convert sub array into string
? implode('', array_map(function($inner_item)
{
return is_array($inner_item) // convert inner array into string
? '[' . implode('||', $inner_item) . ']'
: $inner_item;
}, $item))
: $item;
}, $array));
var_dump($result);
So, we have 3 types of delimiters: '|' - first level, ''(empty) - second level, '||' - third level.
You can use foreach and implode to achieve your pattern:
<?php
header('Content-Type: text/plain');
$arr = array();
$arr22 = array();
$arr22[0] = 60;
$arr22[29] = array(6);
$arr22[30] = array(5,8);
$arr22[31] = array(7,9,14,26);
$arr[22] = $arr22;
$arr[23] = 12;
$arr[35] = 10;
$arr[42] = 22;
$output = '';
foreach($arr as $entry) {
if(!is_array($entry)) {
$output .= $entry;
} else {
// array
foreach($entry as $inner) {
if(is_array($inner)) {
$output .= '[' . implode('||', $inner) . ']';
} else {
$output .= $inner;
}
}
}
$output .= '|';
}
echo substr($output, 0, strlen($output) - 1);
?>
which outputs:
60[6][5||8][7||9||14||26]|12|10|22
This should work for you:
Here the glue is configurable as you desired and this logic is built on the recursive call hence this will work for any level of multidimensional array:
<?php
$arrVal = array (
'22' => array(
'0' => 60,
'29' => array(
'0' => 6
),
'30' => array (
'0' => 5,
'1' => 8
),
'31' => array (
'0' => 7,
'1' => 9,
'2' => 14,
'3' => 26
)
),
'23' => 12,
'35' => 10,
'42' => 22
);
//60[6][5||8][7||9||14||26]|12|10|22
$constructedValue = "";
$glue = "||";
echo $constructedValue = implodeMultiArr($arrVal,$glue);
function implodeMultiArr($arrVal,$glue) {
$i = 0;
$constructedValue = "";
foreach ( $arrVal as $k=>$v) {
if ( is_array($v) ) {
$constructedValue .= !empty($constructedValue) ? "[".implodeMultiArr($v,$glue)."]" : implodeMultiArr($v,$glue)."]" ;
} else {
$constructedValue .= !empty($constructedValue) ? $glue.$v : $v ;
}
$i++;
}
return $constructedValue;
}
here is my $bob array :
Array
(
[n] => Array
(
[0] => 1
)
[m] => Array
(
[0] => 1
[1] => 2
)
[l] => Array
(
[0] => 1
[1] => 4
[2] => 64
)
[o] => Array
(
[0] => 1
[1] => 4
)
)
And i need to output in:
n-1
m-1 , m-2
l-1 , l-4 , l-64
o-1 , o-4
I tryed some
foreach ($bob as $value) {
foreach ($value as &res)
$value = $bob . "-" . $res;
}
}
I guess its pity but i'm php newbe..
All help will welcome,
Jess
You're miss curly bracket after foreach, and missprint with &res -> $res try use foreach with $key
Try this
<?php
$bob = [
'n' => [0 => 1],
'm' => [0 => 1, 1 => 2],
'l' => [0 => 1, 1 => 4, 2 => 64],
'o' => [1 => 1, 1 => 4],
];
foreach ($bob as $key => $value) {
foreach ($value as $res) {
echo $key . "-" . $res . PHP_EOL;
}
}
This output for me
php test.php
n-1
m-1
m-2
l-1
l-4
l-64
o-4
foreach ($bob as $key => $value) {
foreach ($value as $res){
echo $key . "-" . $res ." ";
}
}
The foreach ($bob as $key => $value) syntax gives you each key for each value. You can then loop over the $value array to get the numbers you need.
Try this:
it will give you exact output with , and new line
Live demo : https://eval.in/87738
$arr =array
(
'n' => array
(
0 => 1
),
'm' => array
(
0 => 1,
1 => 2
),
'l' => array
(
0 => 1
),
'0' => array
(
0 => 1,
1 => 2,
2 => 64,
3 => 120,
)
);
$output = '';
foreach($arr as $k1 =>$v1){
$out = ' ';
foreach($arr[$k1] as $k => $v){
$out .= $k1.'-'.$v.',';
}
$output .= rtrim($out,',').'<br/>';
}
echo $output;
Output:
n-1 m-1,m-2 l-1 0-1,0-2,0-64,0-120
try $res in place of &res in following line:
foreach ($value as &res)
Can you try this,
foreach ($bob as $key=>$value) {
foreach ($value as $res){
echo $value = $key . "-" . $res."<br/>";
}
}
My array looks like this:
Array
(
[0] => Array
(
[id] => 613
[kleur] => royalblauw/wit
[maat] => maat XL
[voorraad] => 100
[sort] => 0
[prijs] => 4.1320
)
[1] => Array
(
[id] => 614
[kleur] => royalblauw/wit
[maat] => maat XXL
[voorraad] => 100
[sort] => 1
[prijs] => 4.1320
)
[2] => Array
(
[id] => 620
[kleur] => zwart/wit
[maat] => maat XL
[voorraad] => 100
[sort] => 2
[prijs] => 4.1320
)
etc.etc.
What I want is to sort the array by 'maat' (size). As you can see there are multiple entries with same 'maat'.
I've been trying for hours now, but still not a good result. Ofcourse I've been looking at Stackoverflow, too. This is what I have until now.
Functions:
function cmp($a, $b)
{
$sizes = array(
"116" => 0,
"128" => 1,
"140" => 2,
"152" => 3,
"164" => 4,
"XXS" => 5,
"XS" => 6,
"S" => 7,
"M" => 8,
"L" => 9,
"XL" => 10,
"XXL" => 11,
"XXXL" => 12,
"XXXXL" => 13,
"XXXXXL" => 14
);
$asize = $sizes[$a];
$bsize = $sizes[$b];
if ($asize == $bsize) {
return 0;
}
return ($asize > $bsize) ? 1 : -1;
}
function aasort (&$array, $key) {
$sorter=array();
$ret=array();
reset($array);
foreach ($array as $ii => $va) {
$sorter[$ii]=$va[$key];
}
uasort($sorter, "cmp");
foreach ($sorter as $ii => $va) {
$ret[$ii]=$array[$ii];
}
$array=$ret;
}
And calling the function aasort:
aasort($maatkleur_array,"maat");
Please can you help me. I hope it's clear what I want to achieve.
You want to sort an array of arrays. The parent array don't have a (named) key. uasort perserves the key wich you don't want in your case so use usort in stead:
error_reporting(E_ALL);
ini_set('display_errors','on');
function cmp($a, $b)
{
$sizes = array('s'=>0,'m'=>'1','l'=>2,'xl'=>3);
if ($sizes[$a['maat']] == $sizes[$b['maat']]) {
return 0;
}
return ($sizes[$a['maat']] < $sizes[$b['maat']]) ? -1 : 1;
}
$input = array(
array('id'=>1,'kleur'=>'blauw','maat'=>'xl'),
array('id'=>2,'kleur'=>'blauw','maat'=>'m'),
array('id'=>1,'kleur'=>'blauw','maat'=>'s'),
array('id'=>2,'kleur'=>'blauw','maat'=>'m'),
array('id'=>3,'kleur'=>'blauw','maat'=>'l')
);
usort($input, "cmp");
print_r($input);
I have changed your cmp function little to achieve what you excepting..
$sort = array();
// $arrayData is your main array
foreach($arrayData as $key => $value)
{
$sort[$key] = cmp(strtoupper(str_replace('maat ', '', $value['maat'])));
}
array_multisort($sort, SORT_ASC, $arrayData);
function cmp($a)
{
$sizes = array(
"116" => 0,
"128" => 1,
"140" => 2,
"152" => 3,
"164" => 4,
"XXS" => 5,
"XS" => 6,
"S" => 7,
"M" => 8,
"L" => 9,
"XL" => 10,
"XXL" => 11,
"XXXL" => 12,
"XXXXL" => 13,
"XXXXXL" => 14
);
return $sizes[$a];
}
sort is based cmp function return value.
$arrayData will have sorted array based on maat size.
EDIT:
Use this callback instead. This will do the trick:
<?php
function usort_callback($a, $b) {
$sizes = array(
"116" => 0,
"128" => 1,
"140" => 2,
"152" => 3,
"164" => 4,
"XXS" => 5,
"XS" => 6,
"S" => 7,
"M" => 8,
"L" => 9,
"XL" => 10,
"XXL" => 11,
"XXXL" => 12,
"XXXXL" => 13,
"XXXXXL" => 14
);
$value1 = $sizes[trim(str_replace('maat', '', $a['maat']))];
$value2 = $sizes[trim(str_replace('maat', '', $b['maat']))];
return ($value1 < $value2) ? -1 : 1;
}
?>
ORIGINAL POST:
Try it with usort():
<?php
function usort_callback($a, $b) {
return strcmp($a['maat'], $b['maat']);
}
usort($array, "usort_callback");
?>
Now your array should be sorted by the key "maat"