I have this array and I want to convert it into string.
I try to get string from php implode() function but could not get the desired result.
The output I want is arraykey-arrayvalue,arraykey-arrayvalue,arraykey-arrayvalue and so on as long as array limit end.
Array ( [1] => 1 [2] => 1 [3] => 1 )
$data = implode(",", $pData);
//it is creating string like
$data=1,1,1;
// but i want like below
$string=1-1,2-1,3-1;
You could just gather the key pair values inside an array then implode it:
foreach($array as $k => $v) { $data[] = "$k-$v"; }
echo implode(',', $data);
You can also use array_map function as
$arar = Array ( '1' => 1 ,'2' => 1, '3' => 1 );
$result = implode(',',array_map('out',array_keys($arar),$arar));
function out($a,$b){
return $a.'-'.$b;
}
echo $result;//1-1,2-1,3-1;
This can be done using the below code:
$temp = '';
$val = '';
$i=0;
foreach ($array as $key => $value)
{
$temp = $key.'-'.$val;
if($i == 0)
{
$val = $temp; // so that comma does not append before the string starts
$i = 1;
}
else
{
$val = $val.','.$temp;
}
}
Related
This question already has answers here:
Convert backslash-delimited string into an associative array
(4 answers)
Closed 7 months ago.
I have a colon-delimited string like this:
"Part1:Part2:Part3:Part4"
With explode I can split the string into an array:
echo '<pre>';
print_r(explode(":", "Part1:Part2:Part3:Part4"));
echo '</pre>';
Output:
Array
(
[0] => Part1
[1] => Part2
[2] => Part3
[3] => Part4
)
But I need associative array elements like this:
Array
(
[Part1] => Part2
[Part3] => Part4
)
UPDATE
echo '<pre>';
list($key, $val) = explode(':', 'Part1:Part2:Part3:Part4');
$arr= array($key => $val);
print_r($arr);
echo '</pre>';
Result:
Array
(
[Part1] => Part2
)
Alternative solution using only array_* functions.
Collect all values that are going to be keys for the resultant array.
Collect all values that are going to be actual values for the resultant array.
Combine both using array_combine().
Snippet:
<?php
$str = "Part1:Part2:Part3:Part4";
$res = array_combine(
array_map(fn($v) => $v[0], array_chunk(explode(':',$str),2)),
array_map(fn($v) => $v[1], array_chunk(explode(':',$str),2))
);
Using a for loop, you could also use the modulo operator % and create the key in the if part.
The if clause will be true for the first item having the key first and then the value in the else part, toggeling the keys and the values.
$str = "Part1:Part2:Part3:Part4";
$parts = explode(':', $str);
$result = [];
for ($i = 0; $i < count($parts); $i++) {
$i % 2 === 0 ? $k = $parts[$i] : $result[$k] = $parts[$i];
}
print_r($result);
Output
Array
(
[Part1] => Part2
[Part3] => Part4
)
See a php demo
Try this
$string = "Part1:Part2:Part3:Part4";
$arr = explode(":", $string);
$key = "";
$i = 0;
$newArray = [];
foreach($arr as $row){
if($i == 0){
$key = $row;
$i++;
} else {
$newArray[$key] = $row;
$i = 0;
}
}
echo "<pre>";
print_r($newArray);
echo "</pre>";
I have an array.
Array
(
[0] => 1_4
[1] => 1_1
[2] => 1_2
[3] => 2_3
[4] => 2_5
)
I want to convert it to
Array
(
[1] => Array (4,1,2)
[2] => Array (3,5)
)
Can any1 help me in this? The new keys (1 and 2) are the distinct values from 1st part of array before _.
If the underscore will always be the unique split you could explode on underscore and append results to a new array.
$before = ['1_4','1_1','1_2','2_3','2_5'];
$after = [];
foreach ($before as $entry) {
$index = explode('_',$entry);
$after[$index[0]][] = $index[1];
}
return $after;
Quick and dirty...
foreach($array as $value){ // Loop your current array
$arr = substr($value,0); // Get the character before _, 1 or 2
$val = substr($value,2); // Get character after _, 1,2,3,4 or 5
if($arr == 1){ // if 1_, put into first new array
$newArray[0][] = $val;
}else{
$newArray[1][] = $val; // Put into second array else
}
}
print_r($newArray);
Maybe try something like this?
foreach ($array as $element( {
$kv_pair = explode("_", $element, 2);
if (array_key_exists($kv_pair[0])) {
array_push($new_array[$kv_pair[0]], $kv_pair[1]);
} else {
$new_array[$kv_pair[0]] = [$kv_pair[1]];
}
}
I have an array that looks something like this:
Array (
[0] => Array ( [country_percentage] => 5 %North America )
[1] => Array ( [country_percentage] => 0 %Latin America )
)
I want only numeric values from above array. I want my final array like this
Array (
[0] => Array ( [country_percentage] => 5)
[1] => Array ( [country_percentage] => 0)
)
How I achieve this using PHP?? Thanks in advance...
When the number is in first position you can int cast it like so:
$newArray = [];
foreach($array => $value) {
$newArray[] = (int)$value;
}
I guess you can loop the 2 dimensional array and use a preg_replace, i.e.:
for($i=0; $i < count($arrays); $i++){
$arrays[$i]['country_percentage'] = preg_replace( '/[^\d]/', '', $arrays[$i]['country_percentage'] );
}
Ideone Demo
Update Based on your comment:
for($i=0; $i < count($arrays); $i++){
if( preg_match( '/North America/', $arrays[$i]['country_percentage'] )){
echo preg_replace( '/[^\d]/', '', $arrays[$i]['country_percentage'] );
}
}
Try this:
$arr = array(array('country_percentage' => '5 %North America'),array("country_percentage"=>"0 %Latin America"));
$result = array();
foreach($arr as $array) {
$int = filter_var($array['country_percentage'], FILTER_SANITIZE_NUMBER_INT);
$result[] = array('country_percentage' => $int);
}
Try this one:-
$arr =[['country_percentage' => '5 %North America'],
['country_percentage' => '0 %Latin America']];
$res = [];
foreach ($arr as $key => $val) {
$res[]['country_percentage'] = (int)$val['country_percentage'];
}
echo '<pre>'; print_r($res);
output:-
Array
(
[0] => Array
(
[country_percentage] => 5
)
[1] => Array
(
[country_percentage] => 0
)
)
You can use array_walk_recursive to do away with the loop,
passing the first parameter of the callback as a reference to modify the initial array value.
Then just apply either filter_var or intval as already mentioned the other answers.
$array = [
["country_percentage" => "5 %North America"],
["country_percentage" => "0 %Latin America"]
];
array_walk_recursive($array, function(&$value,$key){
$value = filter_var($value,FILTER_SANITIZE_NUMBER_INT);
// or
$value = intval($value);
});
print_r($array);
Will output
Array
(
[0] => Array
(
[country_percentage] => 5
)
[1] => Array
(
[country_percentage] => 0
)
)
You could get all nemeric values by looping through the array. However I don't think this is the most efficient and good looking answer, I'll post it anyways.
// Array to hold just the numbers
$newArray = array();
// Loop through array
foreach ($array as $key => $value) {
// Check if the value is numeric
if (is_numeric($value)) {
$newArray[$key] = $value;
}
}
I missunderstood your question.
$newArray = array();
foreach ($array as $key => $value) {
foreach ($value as $subkey => $subvalue) {
$subvalue = trim(current(explode('%', $subvalue)));
$newArray[$key] = array($subkey => $subvalue);
}
}
If you want all but numeric values :
$array[] = array("country_percentage"=>"5 %North America");
$array[] = array("country_percentage"=>"3 %Latin America");
$newArray = [];
foreach ($array as $arr){
foreach($arr as $key1=>$arr1) {
$newArray[][$key1] = intval($arr1);
}
}
echo "<pre>";
print_R($newArray);
This is kind of a ghetto method to doing it cause I love using not as many pre made functions as possible. But this should work for you :D
$array = array('jack', 2, 5, 'gday!');
$new = array();
foreach ($array as $item) {
// IF Is numeric (each item from the array) will insert into new array called $new.
if (is_numeric($item)) { array_push($new, $item); }
}
I want to concat values of array with same key
Example:
[0] => Array
(
[0] => A
[1] => XYZ
)
[1] => Array
(
[0] => B
[1] => ABC
)
[2] => Array
(
[0] => A
[1] => LMN
)
[3] => Array
(
[0] => B
[1] => PQR
)
)
Expected output:
[0] => Array
(
[0] => A
[1] => XYZ,LMN
)
[1] => Array
(
[0] => B
[1] => ABC,PQR
)
)
A simple solution uses the PHP function array_reduce():
// The input array you posted in the question
$input = array(
array('A', 'XYZ'),
array('B', 'ABC'),
array('A', 'LMN'),
array('B', 'PQR'),
);
// Reduce the array to a new array that contains the data aggregated as you need
$output = array_reduce(
// Process each $item from $input using a callback function
$input,
// The callback function processes $item; the partial result is $carry
function (array $carry, array $item) {
// Extract the key into a variable
$key = $item[0];
// If the key was encountered before
// then a partial entry already exists in $carry
if (isset($carry[$key])) {
// Append the desired value to the existing entry
$carry[$key][1] .= ','.$item[1];
} else {
// Create a new entry in $carry (copy $item to key $key for quick retrieval)
$carry[$key] = $item;
}
// Return the updated $carry
return $carry;
},
// Start with an empty array (it is known as $carry in the callback function)
array()
);
// $output contains the array you need
Try this:
$final = array();
foreach ($array_items as $item)
{
$key = $item[0];
$found_index = -1;
for ($i=0; $i<count($final); $i++)
{
if ($key == $final[$i][0])
{
$found_index = $i;
break;
}
}
if ($found_index == -1)
{
$final_item = array();
$final_item[0] = $key;
$final_item[1] = $item[1];
$final[] = $final_item;
}
else
{
$final[$found_index][1] .= ",".$item[1];
}
}
We create a new array $final, and loop through your old array $array_items. For each item, we see if there is already an item in $final that has the same [0] index. If it doesn't exist, we create it and add the initial string to the [1] index. If it does exist, we just have to add the string onto the end of the [1] index.
Try it, substituting $array_items for whatever your array is called, let me know if it works.
Check my solution. It should work fine. I hope it will help you much.
$result = $passed_keys = $extended_arr = [];
foreach ($arr as $k => $value) {
for($i = $k + 1; $i < count($arr); $i++){
if ( $value[0] == $arr[$i][0] ){ // compare each array with rest subsequent arrays
$key_name = $value[0];
if (!array_key_exists($key_name, $result)){
$result[$key_name] = $value[1] .",". $arr[$i][1];
} else {
if (!in_array($i, $passed_keys[$key_name])) {
$result[$key_name] .= ",". $arr[$i][1];
}
}
$passed_keys[$key_name][] = $i; // memorizing keys that were passed
}
}
}
array_walk($result, function($v, $k) use(&$extended_arr){
$extended_arr[] = [$k, $v];
});
The result is in $extended_arr
My solution, creates a custom key which makes identifying the letter much easier. This removes the need to continuously iterate through each array, which can become a major resources hog.
<?php
$inital_array = array(
array('A','XYZ'),
array('B','ABC'),
array('A','LMN'),
array('B','PQR')
);
$concat_array = array();
foreach($inital_array as $a){
$key = $a[0];
if( !isset($concat_array[$key]) ){
$concat_array[$key] = array($key,'');
}
$concat_array[$key][1] .= (empty($concat_array[$key][1]) ? '' : ',').$a[1];
}
$concat_array = array_values($concat_array);
echo '<pre>',print_r($concat_array),'</pre>';
I want to group an associative array by fields. The array itself is originally from a mysql database query.
Below is an example of how I do it by hard coding it:
<?php
$fields = array("ID,subID");
$fieldCounts = count($fields);
$data = array(); //there is sql querieed data
$parsedData = array();
foreach ($data as $val)
{
if ($fieldCounts == 1)
{
$f0 = $fields[0];
$fv0 = $val[$f0];
$parsedData[$fv0][] = $val;
}
else if ($fieldCounts == 2)
{
$f0 = $fields[0];
$fv0 = $val[$f0];
$f1 = $fields[10];
$fv1 = $val[$f1];
$parsedData[$fv0][$f1][] = $val;
}
else
{
exit("Third field not implemented");
}
}
?>
But how can I do it dynamically with an arbitrary number of fields?
Am not sure how this code has worked for you but some things are that wrong and might not allow the code to function properly
Fields has only as one valued with ,
$fields = array("ID,subID");
^----------- between string
Instead of
$fields = array("ID","subID");
Notice: Undefined offset:
$f1 = $fields[10];
^----- your array is not up to 10
Since you did not put your generate data and desired output. I would assume your final output and generate some temporary data
$fields = array("ID","subID"); //You can Increase or decrease this Fields
$fieldCounts = count($fields);
$data = array(); // there is sql querieed data
for($i = 0; $i < 3; $i ++) {
$data[] = array("ID" => mt_rand(1, 1000),"subID" => "sub" . mt_rand(100, 900));
}
Ruining your code with the 2 corrections above
foreach ( $data as $val ) {
if ($fieldCounts == 1) {
$f0 = $fields[0];
$fv0 = $val[$f0];
$parsedData[$fv0][] = $val;
} else if ($fieldCounts == 2) {
$f0 = $fields[0];
$fv0 = $val[$f0];
$f1 = $fields[1];
$fv1 = $val[$f1];
$parsedData[$fv0][$f1][] = $val;
} else {
exit("Third field not implemented");
}
}
Output
Array
(
[159] => Array
(
[subID] => Array <----------- SubID is fixed in your can cause confict
(
[0] => Array
(
[ID] => 159
[subID] => sub589
)
)
)
[334] => Array
(
[subID] => Array
(
[0] => Array
(
[ID] => 334
[subID] => sub703
)
)
)
)
A better Alternative to yours
$parsedData = array();
foreach ( $data as $val ) {
$temp = &$parsedData;
foreach ( array_slice($val, 0, $fieldCounts) as $key ) {
$temp = &$temp[$key];
}
$temp[] = $val;
}
print_r($parsedData);
Output
Array
(
[159] => Array
(
[sub589] => Array <---------- Make Sub ID Dynamic
(
[0] => Array
(
[ID] => 159
[subID] => sub589
)
)
)
[334] => Array
(
[sub703] => Array
(
[0] => Array
(
[ID] => 334
[subID] => sub703
)
)
)
)
Recommended Version For easy array path
$parsedData = array();
foreach ( $data as $val ) {
$temp = &$parsedData;
foreach ( array_slice($val, 0, $fieldCounts) as $key ) {
$temp = &$temp[$key];
}
$temp = $val;
}
print_r($parsedData);
Output
Array
(
[159] => Array
(
[sub589] => Array <---- Easy to asses as $parsedData['159']['sub589']
(
[ID] => 159
[subID] => sub589
)
)
[334] => Array
(
[sub703] => Array
(
[ID] => 334
[subID] => sub703
)
)
)
Instead of doing if/elseif/else inside your $data foreach-loop (which is always limited to the number you "write" in there with that structure and a lot of code-duplicateion) you need to turn that if/elseif/else into a loop of it's own.
But first of all transform the existing code, I start in the first if body, it contains already all code necessary:
$f0 = $fields[0];
$fv0 = $val[$f0];
$parsedData[$fv0][] = $val;
The $val should be assigned to the array $parsedData which is keyed by $fields name $value. Let's compress this here, the number 0 in names is superfluous as we don't want it any longer (but maybe the first):
$field = $fields[0];
$value = $values[$field];
$parsedData[$value][] = $values;
(I changed $val into $values to improve naming). This is now more easy to read and understand. Also we spot the magic number 0 here more easily.
Now to the magic. We want to add to an array here (push):
$parsedData[$value][] = $values;
To make this more easy, let's turn it this way:
$array = &$parsedData[$value];
$array[] = $values;
This right now seems superfluous, but when this turns into a loop, it will become more clear:
$array = &$parsedData;
...
$array = &array[$value];
...
$array[] = $values;
Let's review the code in with the outer loop at this moment:
foreach ($data as $values)
{
$array = &$parsedData;
$field = $fields[0];
$value = $values[$field];
$array = &$array[$value];
$array[] = $values;
}
Obviously this code is yet not complete. The inner-loop is missing but it starts to get some kind of body. And actually the inner loop is pretty simple to achieve:
$array = &$parsedData;
foreach ($fields as $field)
{
$value = $values[$field];
$array = &$array[$value];
}
$array[] = $values;
And that's already it. The single field has been turned into an iteration over all fields. The aliasing/referencing of the sub-array per each step in the iteration allows to push the value to the appropriate array entry after the inner loop has finished.
The whole outer and inner loop:
foreach ($data as $values)
{
$array = &$parsedData; # set reference
foreach ($fields as $field)
{
$value = $values[$field];
$array = &$array[$value];
}
$array[] = $values;
unset($array); # remove reference
}