move array keys to another array - php

i have an array like this :
$post = array(
"name" => "John",
"user" => "1" ,
"title" => "hello" ,
"uploader_0_name" => "pic.jpg",
"uploader_0_status" => "done",
"uploader_1_name" => "aaaa.jpg",
"uploader_1_status" => "done",
"uploader_2_name" => "Tulips.jpg",
"uploader_2_status" => "failed",
"uploader_count" => "3"
);
i want to have uploader_[/d]_name and uploader_[/d]_name in another array like example :
[0] => Array
(
[name] => pic.jpg
[status] => done
)
[1] => Array
(
[name] => aaaa.jpg
[status] => done
)
[2] => Array
(
[name] => Tulips.jpg
[status] => failed
)
in this case array with index 0 should have uploader_0_name,uploader_0_status
i tried a lot to do this with preg_match in foreach loop , but i could not be successful
foreach ( $post as $key => $value ) {
$pattern = "/^uploader_[\d]_(name|status)$/";
preg_match( $pattern , $key ,$matches[]);
}
P.S : Unfortunately today i seen the best answer and the best way was deleted ,so i added it , if any one have problem like this , can use :
foreach ($post as $key => $value) {
if (preg_match('/^uploader_(\d)_(name|status)$/', $key, $matches)) {
$result[$matches[1]][$matches[2]] = $value;
}
}

try this if you dont want to use regular expressions:
$newArr = array();
foreach($post as $key => $val) {
$newKey = explode("_", $key);
if (count($newKey) > 2) {
//this is the status
$innerValue = array_pop($newKey);
//this is the numeric ID _2_ for example
$innerKey = array_pop($newKey);
$newArr[$innerKey][$innerValue] = $val;
}
}

There is a simple way to do this do not use hard structures:
$post = array(
"name" => "John",
"user" => "1" ,
"title" => "hello" ,
"uploader_0_name" => "pic.jpg",
"uploader_0_status" => "done",
"uploader_1_name" => "aaaa.jpg",
"uploader_1_status" => "done",
"uploader_2_name" => "Tulips.jpg",
"uploader_2_status" => "failed",
"uploader_count" => "3"
);
//result array;
$arr = array();
//counter
$n = 0;
foreach ($post as $key => $value) {
if(strpos($key, '_name') != false){
$arr[$n]['name'] = $value;
}elseif(strpos($key, '_status') != false){
$arr[$n]['status'] = $value;
$n++;
}
}
print_r($arr);

Related

Grouping PHP Array on repeated events date value with array_flip modification

I am finding difficulty to group events on basis of repeated date values I am getting in my array.
I have applied array_flip function to my array named as $element but it is not able to fulfill the requirements .
My input $element array is :
$element = array(
"2/ 5/2" => "",
"2/ 6/22" => "",
"2/ 7/22" => "AAHPM",
"2/ 8/22" => "AAHPM",
"2/ 9/22" => "AAHPM",
"2/10/22" => "AAHPM",
"2/11/22" => "AAHPM",
"2/12/22" => "AAHPM",
"2/13/22" => "",
"2/14/22" => "School Administrator",
"2/15/22" => "School Administrator",
"2/16/22" => "School Administrator",
"2/17/22" => "School Administrator",
"2/18/22" => "School Administrator",
"2/19/22" => "School Administrator",
"2/20/22" => "",
"2/21/22" => "",
"2/22/22" => "AAHPM",
"2/23/22" => "AAHPM",
"2/24/22" => "AAHPM",
"2/25/22" => "AAHPM",
"2/26/22" => "AAHPM",
"2/27/22" => "AAHPM",
"2/28/22" => "AAHPM",
);
I applied this function :
$reverse = array_flip($element);
The output I am getting now is :
Array
(
[ AAHPM ] => [ 2/28/22],
[ School Administrator ] => [ 2/19/22 ],
)
My desired output is as AAHPN is repeating two times in elements array so I need them separately in an array :
Array
(
[ AAHPM ] => [ 2/12/22, 2/28/22],
[ School Administrator ] => [ 2/19/22 ],
)
I tried to acheive by doing the following instead of array_flip on $elements:
$result = array();
foreach($element as $k => $v) {
if (array_key_exists($v, $result)) {
$result[$v][] = $k;
} else {
$result[$v] = array($k);
}
}
But the output I got is following :
Array
(
[AAHPM] => Array
(
[0] => 2/ 7/22
[1] => 2/ 8/22
[2] => 2/ 9/22
[3] => 2/10/22
[4] => 2/11/22
[5] => 2/12/22
[6] => 2/22/22
[7] => 2/23/22
[8] => 2/24/22
[9] => 2/25/22
[10] => 2/26/22
[11] => 2/27/22
[12] => 2/28/22
)
[School Administrator] => Array
(
[0] => 2/14/22
[1] => 2/15/22
[2] => 2/16/22
[3] => 2/17/22
[4] => 2/18/22
[5] => 2/19/22
)
)
Still I am not able to get the desired output.
You can try using a temp variable and storing the last key value in it. On each iteration you can check the key value with temp variable. If it doesn't match then create a new array otherwise push in last array.
Here is how you can do it.
$result = array();
$temp = "";
foreach($element as $k => $v) {
$modified_val = trim($v) == "" ? "null" : preg_replace('/\s+/', '_', $v);
if($temp == "" || $modified_val != $temp){
$result[$modified_val][] = array($k);
}
elseif($modified_val == $temp){
if (array_key_exists($modified_val, $result)) {
$lastindex = count($result[$modified_val]) - 1;
$result[$modified_val][$lastindex][] = $k;
} else {
$result[$modified_val][] = array($k);
}
}
$temp = $modified_val == "" ? "null" : $modified_val;
}
Here I have written a function
<?php
function reverse($array){
$output = array();
foreach($array as $key => $value){
if(!empty($value)){ //ignoring empty events
$added = false;
foreach($output as $k => $val){ //checking whether event added or not
if($value == $k){
$output[$k][] = $key;
$added = true;
break; //no need to checking further
}
}
if(!$added){
$output[$value] = array($key);
} //creating new event
}
}
return $output;
}
$output = reverse($element);
?>

How to extract specific key string from array in php?

This is my array
Array
(
[question_set] => Computer Basics
[question] => Who are You ?
[options_1] => RK
[options_2] => KAMAL
[options_3] => DPK
[options_4] => NARENDRA
[marks] => 5
[negative_marks] => 1
[type] => 1
)
options_ are dynamic means it can be 4, 6 or 8.
I want to get value "options" from key of options_1 or so on. How can I do this.
strpos is way faster than preg_match, for reference: strpos() vs preg_match()
Using foreach and strpos() :
$arr = array(
"question_set" => "Computer Basics",
"question" => "Who are You ?",
"options_1" => "RK",
"options_2" => "KAMAL",
"options_3" => "DPK",
"options_4" => "NARENDRA",
"marks" => 5,
"negative_marks" => 1,
"type" => 1
);
$newArr = array();
foreach($arr as $key => $value) {
if(strpos($key, "options") !== false) {
$newArr[$key] = $value;
}
}
echo '<pre>';
var_dump($newArr);
echo '</pre>';
<?php
$array = array("options_1" => "RK",
"options_213" => "21313",
"options_4" => "NARENDRA",
"foo" => "bar", 5 , 5 => 89009,
);
$pattern = "/\boptions/";
foreach($array as $key => $value) {
if (preg_match($pattern,$key)){
echo $key."\t=>\t".$value."\n";
}
}

Php string comparision in multidimensional array

I have two multidimensional array
Array
(
[0] => Array
(
[code] => 2079
[label] => Nike
)
[1] => Array
(
[code] => 1080
[label] => Adidas
)
)
Array
(
[0] => Array
(
[manufacturers_id] => 2753
[manufacturers_name] => Reebok
)
[1] => Array
(
[manufacturers_id] => 2526
[manufacturers_name] => Adidas
)
[2] => Array
(
[manufacturers_id] => 34
[manufacturers_name] => Nike
)
)
I want to do string matching of a key label of array 1 with key manufacturer_name of array 2. What is the best approach in multidimensional arrays ?
You could loop trough the arrays.
foreach ($multi_array1 as $value) {
foreach ($multi_array2 as $value2) {
If ($value[label] === $value2 [manufacturer_name])
{}
}
}
If you want to match by label and manufacturers_name, then I suggest you reindex your arrays by those fields:
$by_label = [];
foreach($first_array as $element) {
$by_label[$element['label']] = $element['code'];
}
$by_name = [];
foreach($second_array as $element) {
$by_label[$element['manufacturers_name']] = $element['manufacturers_id'];
}
foreach($by_label as $label => $code) {
print "Label is $label, code is $code, id is {$by_name[$label]}";
}
foreach($by_name as $name => $id) {
print "Name is $name, id is $id, label is {$by_label[$name]}";
}
Just use foreach to accomplish your desire result.
Array
$firstArr = array(
array("code" => 2079, "label" => 'Nike'),
array("code" => 1080, "label" => 'Adidas')
);
$secArr = array(
array("manufacturers_id" => 2753, "manufacturers_name" => 'Reebok'),
array("manufacturers_id" => 2526, "manufacturers_name" => 'Adidas'),
array("manufacturers_id" => 34, "manufacturers_name" => 'Nike')
);
Foreach Technique:
foreach($firstArr as $value){
if(in_array_sec($value['label'])){
echo $value['label'].' found in second array.';
}else{
echo $value['label'].' not found in second array.';
}
}
function in_array_sec($val_one){
global $secArr;
$flag = false;
foreach($secArr as $value){
if($value['manufacturers_name'] == $val_one){
$flag = true;
break;
}
else
$flag = false;
}
return $flag;
}
Result:
Nike found in second array.
Adidas found in second array.

Parse a php array that has float index into new array

I have an array with float indexes that I'm getting from a plugin. I want to parse this array and take the ones that are float and put them in as a new array. Meaning what I have is
[1] => gsurvey128f54af2
[2] => gsurvey282bd4253
[5.1] => gsurvey5649a964f
[5.2] => gsurvey5fddb5e9f
[5.3] => gsurvey533c5b311
[5.4] => gsurvey5c8933efb
[5.5] => gsurvey5da48f59b
What I want is:
[1] => gsurvey128f54af2
[2] => gsurvey282bd4253
[5] => array (
[0] => gsurvey5649a964f
[1] => gsurvey5fddb5e9f
[2] => gsurvey533c5b311
[3] => gsurvey5c8933efb
[4] => gsurvey5da48f59b
)
I'm not clear on what's the best way to approach this.
I made the comment above that this problem is very prone to error because it is possible to have input that would set a single value to be both a string and an array at the same time. That will obviously fail. Assuming that the input is clean...
$new_array = array();
foreach($old_array as $key=>$val)
{
$a = explode('.', $key);
if(sizeof($a)==2)
{
if(!isset($new_array[$a[0]])) $new_array[$a[0]] = array();
$new_array[$a[0]][] = $val;
}
else $new_array[$key]=$val;
}
Example with array_walk:
$array = array (
'1' => 'gsurvey128f54af2',
'2' => 'gsurvey282bd4253',
'5.1' => 'gsurvey5649a964f',
'5.2' => 'gsurvey5fddb5e9f',
'5.3' => 'gsurvey533c5b311',
'5.4' => 'gsurvey5c8933efb',
'5.5' => 'gsurvey5da48f59b'
);
$output = array();
array_walk( $array, function( $item, $key ) use ( &$output ) {
$keys = explode( '.', $key );
isset( $keys[1] ) ? $output[ $keys[0] ][ $keys[1] ] = $item : $output[ $keys[0] ] = $item;
});
print_r( $output );
/*
Array
(
[1] => gsurvey128f54af2
[2] => gsurvey282bd4253
[5] => Array
(
[1] => gsurvey5649a964f
[2] => gsurvey5fddb5e9f
[3] => gsurvey533c5b311
[4] => gsurvey5c8933efb
[5] => gsurvey5da48f59b
)
)
*/
For this specific case, this should work:
foreach($array as $k => $v) {
if(count($i = explode('.', $k)) > 1) {
$result[$i[0]][] = $v;
} else {
$result[$k] = $v;
}
}

Select arrays with multiple occurence from a array of arrys - PHP

My array is given below. Which contains more than one arrays.
Array
(
[0] => Array
(
[user_id] => 1
[name] => name1
)
[1] => Array
(
[user_id] => 2
[name] => name2
)
[2] => Array
(
[user_id] => 2
[name] => name2
)
[3] => Array
(
[user_id] => 3
[name] => name3
)
)<br/>
I need arrays which has more than one occurence.In this case
Array
(
[user_id] => 2
[name] => name2
)
Try this.
function get_multi_occur($my_array)
{
foreach ($my_array as $id => $a) {
$key = $a[user_id] . $a[name];
if ($s[$key]['cnt'] == 0) {
$s[$key]['cnt'] = 1;
$s[$key]['id'] = $id;
} else {
$s[$key]['cnt']++;
}
}
foreach ($s as $r) {
if ($r['cnt'] >= 2) {
$ret[] = $my_array[$r[id]];
}
}
return $ret;
}
foreach ($array as $key_1=>$sub_array_1)
{
foreach ($array as $key_2=>$sub_array_2)
{
if ($sub_array_1 == $sub_array_2 &&
$key_1 != $key_2)//prevent to compare same sub arrays
print_r($sub_array_1);
}
}
something like this
You can try
$array = Array(
"0" => Array("user_id" => 1,"name" => "name1"),
"1" => Array("user_id" => 2,"name" => "name2"),
"2" => Array("user_id" => 2,"name" => "name2"),
"3" => Array("user_id" => 3,"name" => "name3"));
$d = $s = array();
array_map(function ($v) use(&$d, &$s) {
array_key_exists($v['user_id'], $s) && ! array_key_exists($v['user_id'], $d) ? $d[$v['user_id']] = $v : $s[$v['user_id']] = $v;
}, $array);
var_dump($d);
Output
array
2 =>
array
'user_id' => int 2
'name' => string 'name2' (length=5)
Create a new array that will store data that have same values.
Try this code :
$array_existing = array();
$ctr = 0;
foreach ($first_array as $key => $first) :
foreach ($second_array as $keytwo => $second) :
if ($first['user_id'] == $second['user_id'] && $first['name'] == $second['name']) {
$array_existing[$ctr]['user_id'] = $first['user_id'];
$array_existing[$ctr]['name'] = $first['name'];
$array_existing[$ctr]['first_index'] = $key;
$array_existing[$ctr]['second_index'] = $keytwo;
$ctr++;
}
endforeach;
endforeach;
var_dump($array_existing);

Categories