PHP convert multiple strings with special chars as delimiters to array - php

I have a multiple arrays with strings that comes out from a wordpress database.
Its a table from a plugin that stores the data in a very strange way, like this:
print_r($results);
Array
(
[form] => text^name14^Antony~text^secondname14^White~email^email14^antony.white#gmail.com
)
Array
(
[form] => ......
)
I need to get the clean data, so:
foreach ($results as $result) {
$formdata_array = explode('~',$result);
$formdata_array_count = count($formdata_array);
for ( $i=0 ; $i < $formdata_array_count ; $i++) {
if ( empty( $formdata_array[$i] ) ) {
continue;
}
$elemnts = explode('^',$formdata_array[$i]);
$type = $elemnts[0];
$element_name = $elemnts[1];
$value = $elemnts[2];
$value = nl2br($value);
}
And at this point I get:
print_r($value)
Antony
White
antony.white#gmail.com
But I need to have an array to work with
Array
(
[0] => Antony
[1] => White
[2] => antony.white#gmail.com
)
I triede differents methods like array_merge, array_column, array_combine but I can't get the final result

This probably is what you are looking for:
<?php
$results = [
['form' => "text^name14^Antony~text^secondname14^White~email^email14^antony.white#gmail.com"],
['form' => "text^name14^Georgy~text^secondname14^Black~email^email14^georgy.black#gmail.com"],
];
foreach ($results as $result) {
$output = [];
$formdata_array = explode('~',$result['form']);
$formdata_array_count = count($formdata_array);
for ( $i=0 ; $i < $formdata_array_count ; $i++) {
if ( empty( $formdata_array[$i] ) ) {
continue;
}
$elements = explode('^',$formdata_array[$i]);
$output[] = [
'type' => $elements[0],
'name' => $elements[1],
'value' => $elements[2],
];
}
print_r(array_column($output, 'value'));
}
The output is:
Array
(
[0] => Antony
[1] => White
[2] => antony.white#gmail.com
)
Array
(
[0] => Georgy
[1] => Black
[2] => georgy.black#gmail.com
)

Related

PHP Unique MultiDimensional Array Issue

So, I am getting unique values from my MD array utilizing the following function:
function unique_multidim_array($array, $key) {
$temp_array = array();
$i = 0;
$key_array = array();
foreach( $array as $val ) {
if ( ! in_array( $val[$key], $key_array ) ) {
$key_array[$i] = $val[$key];
$temp_array[$i] = $val;
}
$i++;
}
return $temp_array;
}
My array is similar to the following:
Array (
[0] =>
Array (
'name' => 'Nevada'
)
[1] =>
Array (
'name' => 'Colorado'
)
[2] =>
Array (
'name' => 'Nevada'
)
[3] =>
Array (
'name' => 'Colorado'
)
[4] =>
Array (
'name' => 'Oklahoma'
)
[5] =>
Array (
'name' => 'Nevada'
)
[6] =>
Array (
'name' => 'Nevada'
)
)
And using the function (unique_multidim_array ( $term_arr, 'name' )) above I am getting a single Nevada and a single Colorado, however, it is not returning Oklahoma
What can I do to ensure that it will return unique values, even if there are no duplicates?
Your resulting array keeps the original indices, and, depending on how you are iterating over it, you might get unexpected results. Try resetting the indices:
function unique_multidim_array($array, $key) {
$temp_array = array();
$i = 0;
$key_array = array();
foreach( $array as $val ) {
if ( ! in_array( $val[$key], $key_array ) ) {
$key_array[$i] = $val[$key];
$temp_array[] = $val; // <--- remove the $i
}
$i++;
}
return $temp_array;
}
Or, as you say, array_values() will help too:
$term_arr = array_values ( unique_multidim_array ( $term_arr, 'name' ) );
PHP already has a function to remove duplicates from an array
array_unique(array)
should do the trick

Associative index array to associative associative array

Problem
I have an array which is returned from PHPExcel via the following
<?php
require_once 'PHPExcel/Classes/PHPExcel/IOFactory.php';
$excelFile = "excel/1240.xlsx";
$objReader = PHPExcel_IOFactory::createReader('Excel2007');
$objPHPExcel = $objReader->load($excelFile);
foreach ($objPHPExcel->getWorksheetIterator() as $worksheet) {
$arrayData[$worksheet->getTitle()] = $worksheet->toArray();
}
print_r($arrayData);
?>
This returns:
Array
(
[Films] => Array
(
[0] => Array
(
[0] => Name
[1] => Rating
)
[1] => Array
(
[0] => Shawshank Redemption
[1] => 39
)
[2] => Array
(
[0] => A Clockwork Orange
[1] => 39
)
)
[Games] => Array
(
[0] => Array
(
[0] => Name
[1] => Rating
)
[1] => Array
(
[0] => F.E.A.R
[1] => 4
)
[2] => Array
(
[0] => World of Warcraft
[1] => 6
)
)
)
What I would like to have is
Array
(
[Films] => Array
(
[0] => Array
(
[Name] => Shawshank Redemption
[Rating] => 39
)
[1] => Array
(
[Name] => A Clockwork Orange
[Rating] => 39
)
)
[Games] => Array
(
[0] => Array
(
[Name] => F.E.A.R
[Rating] => 4
)
[1] => Array
(
[Name] => World of Warcraft
[Rating] => 6
)
)
)
The arrays names (Films, Games) are taken from the sheet name so the amount can be variable. The first sub-array will always contain the key names e.g. Films[0] and Games[0] and the amount of these can be varible. I (think I) know I will need to do something like below but I'm at a loss.
foreach ($arrayData as $value) {
foreach ($value as $rowKey => $rowValue) {
for ($i=0; $i <count($value) ; $i++) {
# code to add NAME[n] as keys
}
}
}
I have searched extensively here and else where if it is a duplicate I will remove it.
Thanks for any input
Try
$result= array();
foreach($arr as $key=>$value){
$keys = array_slice($value,0,1);
$values = array_slice($value,1);
foreach($values as $val){
$result[$key][] = array_combine($keys[0],$val);
}
}
See demo here
You may use nested array_map calls. Somehow like this:
$result = array_map(
function ($subarr) {
$names = array_shift($subarr);
return array_map(
function ($el) use ($names) {
return array_combine($names, $el);
},
$subarr
);
},
$array
);
Demo
Something like this should work:
$newArray = array();
foreach ($arrayData as $section => $list) {
$newArray[$section] = array();
$count = count($list);
for ($x = 1; $x < $count; $x++) {
$newArray[$section][] = array_combine($list[0], $list[$x]);
}
}
unset($arrayData, $section, $x);
Demo: http://ideone.com/ZmnFMM
Probably a little late answer, but it looks more like your tried solution
//Films,Games // Row Data
foreach ($arrayData as $type => $value)
{
$key1 = $value[0][0]; // Get the Name Key
$key2 = $value[0][1]; // Get the Rating Key
$count = count($value) - 1;
for ($i = 0; $i < $count; $i++)
{
/* Get the values from the i+1 row and put it in the ith row, with a set key */
$arrayData[$type][$i] = array(
$key1 => $value[$i + 1][0],
$key2 => $value[$i + 1][1],
);
}
unset($arrayData[$type][$count]); // Unset the last row since this will be repeated data
}
I think this will do:
foreach($arrayData as $key => $array){
for($i=0; $i<count($array[0]); $i++){
$indexes[$i]=$array[0][$i];
}
for($i=1; $i<count($array); $i++){
for($j=0; $j<count($array[$i]); $j++){
$temp_array[$indexes[$j]]=$array[$i][$j];
}
$new_array[$key][]=$temp_array;
}
}
print_r($new_array);
EDIT: tested and updated the code, works...

convert array into key value pairs

I have following array,
Array
(
[Char100_1] => Array
(
[0] => Array
(
[Char100_1] => Mr S Kumar
)
[1] => Array
(
[Char100_1] => Mr S Kumar2
)
)
[Char100_13] => Array
(
[0] => Array
(
[Char100_13] => 159.9
)
[1] => Array
(
[Char100_13] => 119.9
)
)
[Char100_14] => Array
(
[0] => Array
(
[Char100_14] => 191.88
)
[1] => Array
(
[Char100_14] => 143.88
)
)
)
which is created dynamically from a database query result and some loops.
Now I wanted to convert this array into something like below,
Array
(
[0] => Array
(
[Char100_1] => Mr S Kumar
[Char100_13] => 159.9
[Char100_14] => 191.88
)
[1] => Array
(
[Char100_1] => Mr S Kumar2
[Char100_13] => 119.9
[Char100_14] => 143.88
)
)
I have tried looping through them but its not working.
<?php
/* database process to create array */
$contentArray = array();
foreach($newData['DataField'] as $ndata) :
$responsedata = getAppContent($appid, $ndata);
while($haveresult = mysql_fetch_assoc($responsedata))
{
$contentArray[$ndata][] = $haveresult;
}
endforeach;
/* for getting resulting array start */
$newdataArray = array();
foreach($contentArray as $field => $value):
$newdataArray[$field] = array();
foreach( $value as $val ) :
$newdataArray[$field] = $val;
endforeach;
endforeach;
?>
If you can't change the query (as suggested in the comments), then the following should work:
$output = array();
foreach ($array as $a) {
foreach ($a as $k => $b) {
if (empty($output[$k])) {
$output[$k] = array();
}
$output[$k] += $b;
}
}
I observe that you are transposing the arrays. i.e all the zero subscript values together and all the one subscript values together.
Therefore your outer subscript should be the '0' and '1'. These are available in the inner loop. So, the inner loop index becomes the outer array index. And the inner loop value, which is an array, you need to take the 'current' value of.
/* for getting resulting array start (PHP 5.3.18) */
$newdataArray = array();
foreach($contentArray as $field => $value):
foreach( $value as $idx => $val ): // $idx takes value 0 or 1. $val is an array
$newdataArray[$idx][$field] = current($val);
endforeach;
endforeach;
print_r($newdataArray);
As long as all of your arrays have the same amount of values containing them a for loop will do:
$NewDataArray = array();
for ($a = 0; $a < $Numberofvaluesineacharray; $a++){
$NewDataArray[$a] = $NewDataArray[$array1[$a], $array2[$a],.....arrayn[$a];
}

How to get the value from serialized array by using preg_match in php

I need to get the value from the serialized array by matching the index value.My unserialized array value is like
Array ( [info1] => test service [price_total1] => 10
[info2] => test servicing [price_total2] => 5 )
I need to display array like
Array ( [service_1] => Array ([info]=>test service [price_total] => 10 )
[service_2] => Array ([info]=>test servicing [price_total] => 5 ))
buy i get the result like the below one
Array ( [service_1] => Array ( [price_total] => 10 )
[service_2] => Array ( [price_total] => 5 ) )
my coding is
public function getServices($serviceinfo) {
$n = 1;
$m = 1;
$matches = array();
$services = array();
print_r($serviceinfo);
if ($serviceinfo) {
foreach ($serviceinfo as $key => $value) {
if (preg_match('/info(\d+)$/', $key, $matches)) {
print_r($match);
$artkey = 'service_' . $n;
$services[$artkey] = array();
$services[$artkey]['info'] = $serviceinfo['info' . $matches[1]];
$n++;
}
if ($value > 0 && preg_match('/price_total(\d+)$/', $key, $matches)) {
print_r($matches);
$artkey = 'service_' . $m;
$services[$artkey] = array();
$services[$artkey]['price_total'] = $serviceinfo['price_total' . $matches[1]];
$m++;
}
}
}
if (empty($services)) {
$services['service_1'] = array();
$services['service_1']['info'] = '';
$services['service_1']['price_total'] = '';
return $services;
}
return $services;
}
I try to print the matches it will give the result as
Array ( [0] => info1 [1] => 1 ) Array ( [0] => price_total1 [1] => 1 )
Array ( [0] => info2 [1] => 2 ) Array ( [0] => price_total2 [1] => 2 )
Thanks in advance.
try this. shorted version and don't use preg_match
function getServices($serviceinfo) {
$services = array();
if (is_array($serviceinfo) && !empty($serviceinfo)) {
foreach ($serviceinfo as $key => $value) {
if(strpos($key, 'info') === 0){
$services['service_'.substr($key, 4)]['info']=$value;
}elseif (strpos($key, 'price_total') === 0){
$services['service_'.substr($key, 11)]['price_total']=$value;
}
}
}
return $services;
}
$data = array('info1'=>'test service','price_total1'=>10,'info2'=>'test servicing','price_total2'=>5);
$service = getServices($data);
print_r($service);

How to change index of the multidimentional array below?

How to change this bellow array
Array
(
[0] => Array
(
[0] => Array
(
[0] => 35.2
)
[1] => Array
(
[0] => 49.5
)
)
[1] => Array
(
[0] => Array
(
[0] => 44
)
[1] => Array
(
[0] => 38.5
)
)
)
into
Array
(
[0] => Array
(
[0] => 35.2
[1] =>49.5
)
[1] => Array
(
[0] => 44
[1] => 38.5
)
)
The easy, easy way is a simple nested foreach:
// be sure to include the &. Otherwise your edits will do nothing.
foreach( $input as &$level1 )
{
// level 1 is the array of the arrays which contain your values
// we need the keys and the arrays which hold the desired values
foreach( $level1 as $key => $level2 )
{
// assign the key to be the value at 0 instead of its current value
// (which happens to be level2)
$level1[ $key ] = $level2[ 0 ];
}
}
Sheepy proposed use of array_merge with call_user_func_array below. That is a good idea and I gave it a +1. (I encourage others to do the same). To see which was better, I decided to run a benchmark on both solutions:
The results:
manual 0.074267864227295
call_usr_func_array 0.13694596290588
manual 0.080928087234497
call_usr_func_array 0.13510608673096
I then reversed test order:
call_usr_func_array 0.14956903457642
manual 0.066309928894043
call_usr_func_array 0.14821600914001
manual 0.064701080322266
Here's the benchmark code:
$st = microtime(1);
$input = array();
for( $i = 0; $i < 10000; $i++ )
$input[] = array( array( 1 ),array( 2 ),array( 3 ) );
// be sure to include the &. Otherwise your edits will do nothing.
foreach( $input as &$level1 )
{
// level 1 is the array of the arrays which contain your values
// we need the keys and the arrays which hold the desired values
foreach( $level1 as $key => $level2 )
{
// assign the key to be the value at 0 instead of its current value
// (which happens to be level2)
$level1[ $key ] = $level2[ 0 ];
}
}
print microtime(1) - $st;
print PHP_EOL;
$st = microtime(1);
$input = array();
for( $i = 0; $i < 10000; $i++ )
$input[] = array( array( 1 ),array( 2 ),array( 3 ) );
foreach ( $input as $k => $ary ) {
$input[$k] = call_user_func_array ( 'array_merge' , $ary );
}
print microtime(1) - $st;
Here is a shorter version. It is still a nested loop in reality, though.
foreach ( $input as $k => $ary ) {
$input[$k] = call_user_func_array ( array_merge , $ary );
}

Categories