Php - How to convert multiple key values array to | (Pipe) Separated string - php

I am working on one project with multiple array operations.
I have one variable called $product_attributes and it contains below array as value.
Array
(
[0] => Array
(
[0] => Applications
[1] => Steel; PVC; Std. Wall
)
[1] => Array
(
[0] => Blade Exp.
[1] => 0.29
)
[2] => Array
(
[0] => Fits Model
[1] => 153
)
)
Now i want to convert it into | (Pipe) Separated String like below:
Applications=Steel; PVC; Std. Wall|Blade Exp.=0.29|Fits Model=153
Below is what i have tried:
$tags = implode('|',$product_attributes);
echo "Output".$tags;
But it returns output as below:
OutputArray|Array|Array|Array|Array|Array

The solution using array_map and implode functions:
$result = implode("|", array_map(function ($v) {
return $v[0] . "=" .$v[1];
}, $product_attributes));
print_r($result);
The output:
Applications=Steel; PVC; Std. Wall|Blade Exp.=0.29|Fits Model=153

Related

Array values to single array using foreach loop in PHP

I am working with php and arrays, I have multiple arrays like following
Array
(
[0] => Array
(
[wallet_address] => 0x127e61982701axxxxxxxxxxxxxxxxxxxxxxxxxxx
)
[1] => Array
(
[wallet_address] => 0xf80a41eE97e3xxxxxxxxxxxxxxxxxxxxxxxxxxxx
)
[2] => Array
(
[wallet_address] => 0x24361F1602bxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
)
and so on....
And i want to make them in single array with comma like following way
$set = array("0x127e61982701axxxxxxxxxxxxxxxxxxxxxxxxxxx","0xf80a41eE97e3xxxxxxxxxxxxxxxxxxxxxxxxxxxx","0x24361F1602bxxxxxxxxxxxxxxxxxxxxxxxxxxxxx");
How can i do this ?Here is my current code but not working,showing me same result(0,1,2 keys),Where i am wrong ?
$GetUserFollower; //contaning multiple array value
$set=array();
foreach($GetUserFollower as $arr)
{
$set[]=$arr;
}
echo "<pre>";print_R($set);
The original array is an Assoc array and therefore the wallet_address needs to be addressed specifically in a loop. Or you could use the array_column() builtin function to achieve the same thing.
$GetUserFollower; //contaning multiple array value
$set=array();
foreach($GetUserFollower as $arr)
{
$set[] = $arr['wallet_address'];
}
echo "<pre>";print_r($set);
Or
$new = array_column($GetUserFollower, 'wallet_address');
print_r($new);
RESULT
Array
(
[0] => 0x127e61982701axxxxxxxxxxxxxxxxxxxxxxxxxxx
[1] => 0xf80a41eE97e3xxxxxxxxxxxxxxxxxxxxxxxxxxxx
[2] => 0x24361F1602bxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
)
Your comments are making me think you want an array without a key, which is impossible. If you do this with the example you show in your comments
$set = array("0x127e61982701axxxxxxxxxxxxxxxxxxxxxxxxxxx","0xf80a41eE97e3xxxxxxxxxxxxxxxxxxxxxxxxxxxx","0x24361F1602bxxxxxxxxxxxxxxxxxxxxxxxxxxxxx");
print_r($set);
You will see
Array
(
[0] => 0x127e61982701axxxxxxxxxxxxxxxxxxxxxxxxxxx
[1] => 0xf80a41eE97e3xxxxxxxxxxxxxxxxxxxxxxxxxxxx
[2] => 0x24361F1602bxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
)

How to make array unique by one value and others in comma separated? [duplicate]

This question already has answers here:
Group subarrays by one column, make comma-separated values from other column within groups
(2 answers)
Closed 3 years ago.
Here is the array which I have,
Array
(
[0] => Array
(
[number] => 2
[name] => ABC
)
[1] => Array
(
[number] => 3
[name] => ABC
)
[2] => Array
(
[number] => 1
[name] => XYZ
)
[3] => Array
(
[number] => 2
[name] => XYZ
)
)
what I want is...
Array
(
[0] => Array
(
[name] => XYZ
[number] => 1,2
)
[1] => Array
(
[name] => ABC
[number] => 2,3
)
)
it should be unique by name.
And numbers of particular name will be in comma separated.
please help me guys
Thanks in advance
You can do this in one loop but I prefer to do it in two loops as it will be easier to get the correct output with implode than adding commas and then removing them again.
Loop the array and build an associative array and make an array to collect the numbers.
Then loop again and implode the numbers to a string.
foreach($arr as $sub){
$res[$sub['name']]['name'] = $sub['name'];
$res[$sub['name']]['number'][] = $sub['number'];
}
foreach($res as &$sub){
$sub['number'] = implode(",", $sub['number']);
}
$res = array_values($res);
var_dump($res);
https://3v4l.org/PbGsu
You can use array_map, array_key_exists, array_values to get the desired results
$res = [];
array_map(function($v) use (&$res){
array_key_exists($v['name'], $res) ?
($res[$v['name']]['number'] = $res[$v['name']]['number'].','.$v['number'])
:
($res[$v['name']] = ['number' => $v['number'],'name' => $v['name']])
;
}, $arr);
$res = array_values($res);
Live Demo
To use
array_merge()
function.
If suppose your key values are same, your key and values are overwrites after merge your array.

PHP Array only get those elements/values whose keys match a particular pattern

I have a multi-dimensional & nested array like below:
Array
(
[_edit_lock] => Array
(
[0] => 1504299434:6
)
[_edit_last] => Array
(
[0] => 6
)
[additional_photos_0_gallery_image_1] => Array
(
[0] => 77556
)
[additional_photos_0_gallery_image_2] => Array
(
[0] => 77567
)
[additional_photos_0_gallery_image_3] => Array
(
[0] => 73768
)
....
)
Now I need to get only those elements of the given array(in a separate array without changing current array), whose keys match a particular pattern like below:
additional_photos_[any_number]_gallery_image_[any_number]
How can I get using one of the array functions and avoiding foreach loops ?
Just use array_filter and preg_match.
return array_filter($data, function($key) {
return preg_match('~^additional_photos_[0-9]+_gallery_image_[0-9]+$~', $key);
}, ARRAY_FILTER_USE_KEY);

Getting array values into a string

I output a PHP object via a loop but withing this look I have a few nested arrays.
[categories] => Array
(
[0] => Array
(
[0] => Chinese
[1] => chinese
)
[1] => Array
(
[0] => Vietnamese
[1] => vietnamese
)
)
[phone] => 5123355555
I can get the phone like this:
$response->businesses[$x]->phone
How do I get categories (first value) into a string like this:
Chinese, Vietnamese
You can achieve with array_column() :
$newArray = array_column($response->businesses[$x]->categories, 0);
It returns an array with the column 0. So the response will be:
print_r($newArray);
//Array ( [0] => Chinese [1] => Vietnamese )
Then you can join it safely:
$newString = implode(",", $newArray)
echo $newString; // "Chinese, Vietnamese"
implode(', ', array_map(function($item) {
return $item[0];
}, $response->businesses[$x]->categories));

Regex for match and retrieve values from separated number list

Got a number list with separator(s) like these (note: quotes not included):
"1"
"1$^20"
"23$^100$^250"
I watch to write a regex to match both syntax of numbers and separators and also return all numbers in list, the best try I can get in PHP is this code segment:
preg_match_all("/(\d+)(?:\\$\\^){0,1}/", $s2, $n);
print_r($n);
but it returns:
Array
(
[0] => Array
(
[0] => 1
[1] => 20
)
[1] => Array
(
[0] => 1
[1] => 20
)
)
What I need is:
Array
(
[0] => 1
[1] => 20
)
or at least:
Array
(
[0] => Array
(
[0] => 1
[1] => 20
)
)
You can just get the first entry in your match array like this:
$s2 = "1$^20";
preg_match_all("/(\d+)(?:\$\^){0,1}/", $s2, $n);
print_r($n[0]);
// Array ( [0] => 1 [1] => 20 )
Or drop the group and just extract the numbers like this:
$s2 = "1$^20";
preg_match_all("/\d+/", $s2, $n);
print_r($n);
// Array ( [0] => Array ( [0] => 1 [1] => 20 ) )
Another alternative might be to use preg_split:
$s2 = "1$^20";
$n = preg_split('/\$\^/', $s2);
print_r($n);
// Array ( [0] => 1 [1] => 20 )
I thought about this quesiton again. I know I need not only split them but also check the value syntax. And what if it's a text seprated list? ... Hmm... then a smart way comes into my mind as follows in PHP codes:
// Split and also check value validity of number separated list
$pattern1 = "/(\d+?)\\$\\^/";
$1 = "1^$23";
$s1 .= "$^"; // Always append one separator set
preg_match_all($pattern1, $s1, $matches);
Change \d to . will work for text separated list or number-text-mixed separated list, too.

Categories