I have the following array of strings with delimiters ':' and ';' (actual data will be 1 or more strings with 5 or more of the same Fieldname:value pairs in each string)
0 => string 'Fieldname1:value;Filedname2:value;Fieldname3:value;...'
1 => string 'Fieldname1:value;Filedname2:value;Fieldname3:value;...'
2 => string 'Fieldname1:value;Filedname2:value;Fieldname3:value;...'
When I turn the above into key => value, the Filednames overwrite. So I am trying to get it into a multidimensional array. I have looked around quite a bit and have not found a solution and need some help.
I used explode() and got it this far:
foreach ($array as $line) {
$value[] = explode(';', $line);
}
var_dump($value);
array
0 =>
array =>
string 'Fieldname1:value'
string 'Filedname2:value'
string 'Fieldname3:value'
1 =>
array =>
string 'Fieldname1:value'
string 'Filedname2:value'
string 'Fieldname3:value'
2 =>
array =>
string 'Fieldname1:value'
string 'Filedname2:value'
string 'Fieldname3:value'
How do I get it into key => value like:
array
0 =>
array =>
'Fieldname1' => 'value'
'Fieldname2' => 'value'
'Fieldname3' => 'value'
1 =>
'Fieldname1' => 'value'
'Fieldname2' => 'value'
'Fieldname3' => 'value'
2=>
'Fieldname1' => 'value'
'Fieldname2' => 'value'
'Fieldname3' => 'value'
Tested and works,
<?php
$a = [
'Fieldname1:value;Filedname2:value;Fieldname3:value',
'Fieldname1:value;Filedname2:value;Fieldname3:value',
'Fieldname1:value;Filedname2:value;Fieldname3:value',
];
$array = array();
foreach($a as $b){
$temp_array = array();
foreach(explode(';',$b) as $c){
list($key,$val) = explode(':',$c);
$temp_array[$key] = $val;
}
$array[] = $temp_array;
}
print_r($array);
returns
Array
(
[0] => Array
(
[Fieldname1] => value
[Filedname2] => value
[Fieldname3] => value
)
[1] => Array
(
[Fieldname1] => value
[Filedname2] => value
[Fieldname3] => value
)
[2] => Array
(
[Fieldname1] => value
[Filedname2] => value
[Fieldname3] => value
)
)
http://ideone.com/Q69vVK
Related
I want to remove key 0 from parent array and set child array as parent.
Here I will get single value so one array is ok for me.
My current array looks like this
Array
(
[0] => Array
(
[id] => 3
[api_key] => acount266
[auth_domain] => Tester26
[database_url] => vcc.test.acc+27#gmail.com
[project_id] => 12345
[storage_bucket] =>
[secret_key_path] =>
[fcm_server_key] => 1
[messaging_sender_id] => 0
[key_phrase] =>
[disable] => 0
[created] =>
[updated] =>
)
)
I want it like below. expected result
Array
(
[id] => 3
[api_key] => acount266
[auth_domain] => Tester26
[database_url] => vcc.test.acc+27#gmail.com
[project_id] => 12345
[storage_bucket] =>
[secret_key_path] =>
[fcm_server_key] => 1
[messaging_sender_id] => 0
[key_phrase] =>
[disable] => 0
[created] =>
[updated] =>
)
For this I tried like below but no success.
$new = array();
foreach ($data as $v){
$new = array_merge($new , array_values($v)) ;
}
but in my code it's removed key e.g id,api_key, etc....
I need key name also in my new array. please suggest
Remove the array_values
Solution
<?php
$test = array(
array
(
'id' => 3,
'api_key' => 'acount266'
)
);
$new = array();
foreach($test as $v){
$new = array_merge($new, $v);
}
var_dump($new);
Result
array(2) {
["id"]=>
int(3)
["api_key"]=>
string(9) "acount266"
}
According to documentation of PHP as mentioned
reset() function returns the value of the first array element, or
FALSE if the array is empty.
$array = array(
array(
'id' => 3,
'api_key' => 'acount266',
'auth_domain' => 'Tester26',
'database_url' => 'vcc.test.acc+27#gmail.com',
'project_id' => '12345',
'storage_bucket' => '',
'secret_key_path' => '',
'fcm_server_key' => 1,
'messaging_sender_id' => 0,
'key_phrase' => '',
'disable' => 0,
'created' => '',
'updated' => ''
)
);
print_r(reset($test));
I tried this:
$arr = array();
foreach ($examples as $example) {
foreach ($example as $e) {
array_push($arr, $e);
}
}
Don't overcomplicate this, reassigning the first element to the parent array is quick and easy:
<?php
$array =
array (
0 =>
array (
'first' => 'Michael',
'last' => 'Thompson'
)
);
$array = $array[0];
var_export($array);
Output:
array (
'first' => 'Michael',
'last' => 'Thompson',
)
Or:
$array = array_shift($array);
I am getting this array when form is submitted
array
'item' =>
array
0 => string 'Salt'
1 => string 'Pepper'
'quantity' =>
array (size=2)
0 => string '2 spoon'
1 => string '5'
and now want to rearrange above array, so it should look like
array
'0' =>
array
'item' => string 'Salt'
'quantity' => string '2 spoon'
'1' =>
array
'item' => string 'Pepper'
'quantity' => string '5'
I tried so many combinations but failed, will somebody help me how to rearrange this array. Any help will be more than appreciated.
Try this
$array = array(
'item' =>
array(
0 => 'Salt',
1 => 'Pepper'),
'quantity' =>
array(
0 => '2 spoon',
1 => '5')
);
$new_array = array();
foreach ($array['item'] as $key => $value) {
$new_array[$key]["item"] = $value;
$new_array[$key]["quantity"] = $array['quantity'][$key];
}
echo "<pre>";
var_dump($new_array);
Do it as below -
<?php
$arr=array(
'item' =>
array(
0 => 'Salt' ,
1 => 'Pepper'
),
'quantity' =>
array (
0 =>'spoon',
1 =>'5'
)
);
$result=array();
$com=array_combine($arr['item'],$arr['quantity']);
foreach($com as $k=>$v)
{
$result[]=array("item"=>$k,"quantity"=>$v);
}
print_r($result);
?>
OUTPUT
Array (
[0] => Array ( [item] => Salt [quantity] => spoon )
[1] => Array ( [item] => Pepper [quantity] => 5 )
)
try this, I think this would help you,
$a = array(
'item' =>
array(
0 => 'Salt',
1 => 'Pepper'),
'quantity' =>
array(
0 => '2 spoon',
1 => '5')
);
$i = 0;
foreach($a['item'] as $row){
$b[$i]["item"] = $row;
$b[$i]["quantity"] = $a['quantity'][$i];
$i++;
}
print_r($b);
Output from print_r() will be
Array
(
[0] => Array
(
[item] => Salt
[quantity] => 2 spoon
)
[1] => Array
(
[item] => Pepper
[quantity] => 5
)
)
You can simply use array_walk like as
$result = [];
array_walk($arr['item'], function($v, $k)use(&$result, $arr) {
$result[$k]['item'] = $arr['item'][$k];
$result[$k]['quantity'] = $arr['quantity'][$k];
});
print_r($result);
Demo
Hoping that your array is stored in the $array variable and that item is leading. Meaning that there is always an item in the array and not always a quantity
<?php
foreach($array['item'] as $key => $item) {
$newArray[$key]['item'] = $item;
if (isset($array['quantity'][$key]) {
$newArray[$key]['quantity'] = $array['quantity'][$key];
} else {
$newArray[$key]['quantity'] = 0;
}
?>
here is my array
$array = Array
(
[0] => Array
(
[name] => crud_inputs[id]
[value] => id_Value
)
[1] => Array
(
[name] => crud_inputs[user_id][]
[value] => userid_Value
)
[2] => Array
(
[name] => crud_inputs[details]
[value] => details_value
)
)
i want to remove parent array ( $array ) and pair the
[name]=[value]
in each inner array
i want to end up with a
crud_inputs array
i.e
$crud_inputs[id] = id_Value ;
$crud_inputs[user_id][] = userid_Value ;
$crud_inputs[details] = details_value ;
-
I WANT TO BE ABLE TO DO SOMETHING LIKE THIS AT THE END
$MY_ORM->UPDATE( $table , $crud_inputs );
this is what i wrote so far but it doesn't work , i get a empty array at the end
$crud_inputs = array();
foreach($array as $ar )
{
$$ar['name'] = $ar['value'];
}
var_dump($crud_inputs);
#Wrikken , =========================================================
this is exactly what i get as my raw array
array (size=6)
0 =>
array (size=2)
'name' => string 'crud_inputs[id]' (length=15)
'value' => string 'id_Value' (length=8)
1 =>
array (size=2)
'name' => string 'crud_inputs[user_id][]' (length=22)
'value' => string 'userid_Value' (length=3)
2 =>
array (size=2)
'name' => string 'crud_inputs[user_id][]' (length=22)
'value' => string 'userid_Value2' (length=3)
3 =>
array (size=2)
'name' => string 'implode[user_id]' (length=16)
'value' => string ',' (length=1)
4 =>
array (size=2)
'name' => string 'crud_inputs[date]' (length=18)
'value' => string 'date_value' (length=13)
5 =>
array (size=2)
'name' => string 'crud_inputs[ip]' (length=15)
'value' => string 'ip_value' (length=13)
-
and this is how i handle it
$new = array();
foreach($array as $value){
$new[$value['name']] = $value['value'];
}
$item = $new;
var_dump($item);
This what was meant as a GET or POST array? parse_str helps:
$array = ...your array...
foreach($array as &$item){
$item = array_map('urlencode',$item);
$item = implode('=',$item);
}
$string = implode('&',$array);
parse_str($string,$result);
var_dump($result);
//and if I read the rest right:
foreach($result['implode'] as $key =>$value){
$result['crudinputs'][$key] = implode($value,$result['crudinputs'][$key]);
}
Earlier on:
Bad oneliner (your colleagues will hate you):
foreach($array as &$item){
$item = call_user_func_array('array_combine',call_user_func_array('array_merge_recursive',$item));
}
Readable multiliner (everybody happy):
foreach($array as &$item){
$new = array();
foreach($item as $value){
$new[$value['name']] = $value['value'];
}
$item = $new;
}
This is assuming an array with multiple items if I read your code correctly, so like:
Array
(
[0] => Array
(
[0] => Array
(
[name] => key0
[value] => value0
)
[1] => Array
(
[name] => key1
[value] => value1
)
[2] => Array
(
[name] => key2
[value] => value2
)
[3] => Array
(
[name] => key3
[value] => value3
)
)
[1] => Array
.... more items here
If not (you only have 1 item), you can omit the outer foreach loop in both examples.
I want to change the index of an array but I don't know how to do it..
The following array
array(
0 => array ( 'id' => 33, 'name' => 'test' )
1 => array ( 'id' => 37, 'name' => 'test2' )
)
should become - if i want the index
array(
33 => array ( 'id' => 33, 'name' => 'test' )
37 => array ( 'id' => 37, 'name' => 'test2' )
)
or if i want the name
array(
test => array ( 'id' => 33, 'name' => 'test' )
test2 => array ( 'id' => 37, 'name' => 'test2' )
)
also for a multi-dimensional array
array(
0 => array ( 'id' => 33, 'details' => array (name => 'test' , age ='50' ) )
1 => array ( 'id' => 37, 'details' => array (name => 'test2' , age ='60' ) )
)
to index replace 0 and 1 with the name - test or test2
right now I made a function but is not working with multi dimensional arrays
function index_array( $array, $index ){
$new_array = array();
foreach($array as $key => $value){
$new_array[$index] = $array[$key];
}
return $new_array;
}
$array = array(33 => $oldarray[0], 37 => $oldarray[1]);
$array = array('test' => $oldarray[0], 'test2' => $oldarray[1]);
Since the right-hand-side of the expression is evaluated before the assignment you could also use $array on both sides instead of a different variable name.
I'd do it with a mapping table for the first part, or a simple foreach for the second part:
<?php
$arr = array(
0 => array( 'id' => 33, 'name' => 'test' ),
1 => array( 'id' => 37, 'name' => 'test2' )
);
/* Convert to other indexes */
$mapping = array(
0 => 33,
1 => 37
);
foreach($arr as $k => $v){
unset($arr[$k]);
$arr[$mapping[$k]] = $v;
}
print_r($arr);
/* Convert key to name field */
foreach($arr as $k => $v){
unset($arr[$k]);
$arr[$v['name']] = $v;
}
print_r($arr);
EDIT: Now that I read your question again, the first part is actually the same as the second, but then just with the id field instead of the name field.
EDIT2: Note that you'd have to use another array to write to, to avoid overrides, which will occur when the id field is used as a replacement.
I have two arrays:
Array
(
[0] => Array
(
[id] => 1
[type] => field
[remote_name] => Title
[my_name] => title
[default_value] => http%3A%2F%2Ftest.com
)
[1] => Array
(
[id] => 2
[type] => field
[remote_name] => BookType
[my_name] => book-type
[default_value] =>
)
[2] => Array
(
[id] => 3
[type] => value
[remote_name] => dvd-disc
[my_name] => dvd
[default_value] =>
)
)
Array
(
[title] => Test
[book-type] => dvd
)
I need to take each key in the second array, match it with the my_name value in the first array and replace it with the corresponding remote_name value of the first array while preserving the value of the second array.
There's got to be some carrayzy function to help!
EDIT: There will also be a few cases that the value of the second array will need to be replaced by the value of the first array's remote_name where the value of the second array matches the value of the first array's my_name. How can I achieve this?
EG: book-type => dvd should turn into BookType => dvd-disc
Like so?:
$first = array(
array(
'id' => 1,
'type' => 'field',
'remote_name' => 'Title',
'my_name' => 'title',
'default_value' => 'http%3A%2F%2Ftest.com',
),
array(
'id' => 2,
'type' => 'field',
'remote_name' => 'BookType',
'my_name' => 'book-type',
'default_value' => '',
),
array(
'id' => 3,
'type' => 'value',
'remote_name' => 'dvd-disc',
'my_name' => 'dvd',
'default_value' => '',
),
);
$second = array(
'title' => 'Test',
'book-type' => 'dvd',
);
$map = array('fields' => array(), 'values' => array());
foreach ($first as $entry) {
switch ($entry['type']) {
case 'field':
$map['fields'][$entry['my_name']] = $entry['remote_name'];
break;
case 'value':
$map['values'][$entry['my_name']] = $entry['remote_name'];
break;
}
}
$new = array();
foreach ($second as $key => $val) {
$new[isset($map['fields'][$key]) ? $map['fields'][$key] : $key] = isset($map['values'][$val]) ? $map['values'][$val] : $val;
}
print_r($new);
Output:
Array
(
[Title] => Test
[BookType] => dvd-disc
)
Explanation:
The first loop collects the my_name/remote_name pairs for fields and values and makes them more accessible.
Like so:
Array
(
[fields] => Array
(
[title] => Title
[book-type] => BookType
)
[values] => Array
(
[dvd] => dvd-disc
)
)
The second loop will traverse $second and use the key/value pairs therein to populate $new. But while doing so will check for key/value duplicates in $map.
Keys or values not found in the map will be used as is.
foreach($arr1 as &$el) {
$el['remote_name'] = $arr2[$el['my_name']];
}
unset($el);
I am not aware of such a carrayzy function, but I know how you could do it:
//$array1 is first array, $array2 is second array
foreach($array1 as $key => $value){
if (isset($value['remote_name'], $value['my_name']) && $value['remote_name'] && $value['my_name']){
$my_name = $value['my_name'];
if (isset($array2[$my_name])) {
$remote_name = $value['remote_name'];
$array2[$remote_name] = $array2[$my_name];
//cleanup
unset($array2[$my_name]);
}
}
}