I have array based POST like this :
Array
(
[condition] => Array
(
[0] => 1
)
[container] =>
[cleaning] => Y
[owner] => Eagletainer
[last_cargo] => 1
[vessel] =>
[insulation] => 1
[tare] =>
[gross] =>
[capacity] =>
[unit_type] => IMO 1
[date_of_manu] =>
[name_manu] =>
[last25] =>
[cert25] =>
[last5] =>
[cert5] =>
[list2_item_0] => 1
[list2_kondisi_0] => 9
[list3_item_0] => 15
[list3_kondisi_0] => 3
[comments] =>
)
My case is, I want to chunk a lot of those element array into another array for insert_batch in my database.
This is the php code to chunk those array:
public function get_partition($array, $p, $c) {
$partition = array_slice($array, $p);
array_pop($partition);
return $chunk = array_chunk($partition, $c);
}
Now, use it,
$detail = $this->get_partition($this->input->post(), 17, 2);
The result is :
Array
(
[0] => Array
(
[0] => 1
[1] => 9
)
[1] => Array
(
[0] => 15
[1] => 3
)
)
My question in, how to change the key [0] and [1] into another key like [ID] and [CODE_DAMAGE]
I want them looked like this :
Array
(
[0] => Array
(
[ID] => 1
[CODE_DAMAGE] => 9
)
[1] => Array
(
[ID] => 15
[CODE_DAMAGE] => 3
)
)
Re-loop the array and achieve your desired result like this:
$detail = $this->get_partition($this->input->post(), 17, 2);
$new_array = array();
$count = 0;
foreach($detail as $row){
$new_array[$count]['ID'] = $row[0];
$new_array[$count++]['CODE_DAMAGE'] = $row[1];
}
If the indexes were already correct you could pass the optional third parameter: http://php.net/array_chunk
<?php
$array = array(0 => array(0 => 123, 1 => 1234), 1 => array(0 => 123, 1 => 1234));
$updatedArray = array();
foreach ($array as $k => $v) {
$updatedArray[$k]['ID'] = $v[0];
$updatedArray[$k]['CODE_DAMAGE'] = $v[1];
}
?>
Try this, I hope this helps.
Try this:
foreach($detail as $key => $value){
if($key == 0){
$detail['ID'] = $value;
unset($detail[$key]);
}
if($key == 1){
$detail['CODE_DAMAGE'] = $value;
unset($detail[$key]);
}
}
Just an example add your array to this code..it will work fine
$main = Array(Array(1,9),Array(15,3));
$b = array('ID', 'CODE_DAMAGE');
$new_array = array();
foreach($main as $subarray)
{
$new_array[] = array_combine($b, $subarray);
}
echo'<pre>';print_r($new_array);
Related
I am trying to return the user_id if value matches with any comma separated value, I am using strpos but I don't why is it not working with 3rd case:
To Compare: (This value is stored in $myArray variable)
Array
(
[0] => cloud
[1] => ai
[2] => test
)
Compare with: (This value is stored in $array_meta_values variable)
Array
(
[0] => Array
(
[tags] => cloud,ai
[user_id] => 1
)
[1] => Array
(
[tags] => cloud,ai,test
[user_id] => 108
)
[2] => Array
(
[tags] => storage,backup,ai
[user_id] => 101
)
)
function searchForId($meta_value, $array)
{
foreach ($array as $key => $val) {
if (strpos($val['tags'], $meta_value)) {
return $val['user_id'];
}
}
}
foreach ($myArray as $usertags) {
$userids[] = searchForId($usertags, $array_meta_values);
}
print_r($userids);
Getting this Output:
Array
(
[1] => 1
[2] => 108
)
It was supposed to add 101 as third element in output array but don't know why it is not working.
Any help appreciated.
Hi so I tried to replicate you question,
$existing = ['cloud', 'ai', 'test'];
$checker = [
array(
"tags" => "cloud,ai",
"user_id" => 1
),
array(
"tags" => "cloud,ai,test",
"user_id" => 108
),
array(
"tags" => "storage,backup,ai",
"user_id" => 101
)
];
function searchForId($meta_value, $array)
{
$ret_array = [];
foreach ($array as $val) {
$et = explode(",", $val['tags']);
if (in_array($meta_value, $et)) {
$ret_array[] = $val['user_id'];
}
}
return $ret_array;
}
foreach ($existing as $usertags) {
$userids[$usertags][] = searchForId($usertags, $checker);
}
echo "<pre>";
print_r($userids);
Is this what you looking for?
Your question is quite unclear, but I'll try with this one:
<?php
$myArray = array('cloud', 'ai', 'test');
$array_meta_values = array(
array(
'tags' => 'cloud,ai',
'user_id' => 1,
),
array(
'tags' => 'cloud,ai,test',
'user_id' => 108,
),
array(
'tags' => 'storage,backup,ai',
'user_id' => 101,
),
);
$userids = [];
foreach ($myArray as $value) {
foreach ($array_meta_values as $array) {
$arrayValues = explode(',', $array['tags']);
if (in_array($value, $arrayValues)) {
$userids[$value][] = $array['user_id'];
}
}
}
echo '<pre>';
print_r($userids);
Output:
Array
(
[cloud] => Array
(
[0] => 1
[1] => 108
)
[ai] => Array
(
[0] => 1
[1] => 108
[2] => 101
)
[test] => Array
(
[0] => 108
)
)
I have this multi-dimensional array and I'm trying to convert it into array given below
Array
(
[id] => Array
(
[0] => 1
[1] => 3
)
[team_id] => Array
(
[0] => 654868479
[1] => 463733228
)
[seed] => Array
(
[0] => 1
[1] => 2
)
)
I want following result
Array
(
[0] => Array
(
[id] => 1
[team_id] => 654868479
[seed] => 1
)
[1] => Array
(
[id] => 3
[team_id] => 463733228
[seed] => 3
)
)
Here is what I have achieved so far. I actually want $seeded[] array is the same format as it is required to submit update_batch. Which will ultimately update database records.
$seeds = $this->input->post();
$i=0;
foreach ($seeds as $key => $value){
if(!empty($key) && !empty($value)){
for($i=0; $i=5; $i++) {
$seeded[] = array(
'id' => (id go here),
'tournament_id' => $tournament_id,
'stage_id' => $stage_id,
'seed_id' => (seed go here),
'team_name' => (team_id go here),
);
}
$this->db->update_batch('tournament_seed', $seeded, 'id');
}
}
Iterate the array and convert it using below code.
$seeded= array();
for($i =0; $i < count($seeds['id']); $i++){
$tempArr['id'] = $seeds['id'][$i];
$tempArr['team_id'] = $seeds['team_id'][$i];
$tempArr['seed'] = $seeds['seed'][$i];
$seeded[] = $tempArr;
}
I've written a function that will allow you to transform any array of similar structure to what you have above, to an array of the form you are looking for.
You can build your array in this way:
$arr = [
'id' => [1, 3],
'team_id' => [654868479, 463733228],
'seed' => [1, 2],
];
function array_flatten($arr) {
$res = [];
foreach($arr as $id => $valuesArray) {
foreach($valuesArray as $index => $value)
$res[$index][$id] = $value;
}
return $res;
}
print_r(array_flatten($arr));
Hope this helps,
Hi all i need to merge the same key to convert to single array from multiple array list please any one help me to the problem
for example here the array.
Array
(
[0] => Array
(
[0] => Mr.
[1] => Mrs.
)
[1] => Array
(
[0] => Rob
[1] => Tam
)
[2] => Array
(
[0] => kar
[1] => Man
)
[3] => Array
(
[0] => 55345345345
[1] => 44545345435
)
)
i need the output is
Array
(
[0] => Array
(
[0] => Mr.
[1] => Rob
[2] => kar
[3] => 55345345345
)
[1] => Array
(
[0] => Mrs.
[1] => Tam
[2] => Man
[3] => 44545345435
)
)
Please any one help
Thanks
For PHP version >= 5.5.0 You can use array_column() and array_merge() for this as
$result = array_merge(array_column($records, '0'), array_column($records, '1'));
print_r($result);
$a = array(
0 => array(
0 => 'Mr.',
1 => 'Mrs.'
),
1 => array
(
0 => 'Rob',
1 => 'Tam'
),
2 => array
(
0 => 'kar',
1 => 'Man'
),
3 => array
(
0 => 55345345345,
1 => 44545345435
)
);
$arr1 = array();
foreach($a as $arr)
{
foreach($arr as $key=>$value)
{
$arr1[$key][] = $value;
}
}
echo '<pre>';
print_r($arr1);
Use this one. You can get output same as you want.
try like this
$out = array();
foreach ($arr1 as $key => $value){
$out[] = (object)array_merge((array)$arr2[$key], (array)$value);
}
print_r($out)
$title = $array[0];
$firstname = $array[1];
$lastname = $array[2];
$number = $array[3];
$output = array();
for($i=0; $i < count($title); $i++)
{
$output[] = array($title[$i],$firstname[$i],$lastname[$i],$number[$i])
}
var_dump($output);
I have an array like this and it can contain multiple values:
Array
(
[rpiid] => Array
(
[1] => 86
)
[sensor_id] => Array
(
[1] => 1
)
[when] => Array
(
[1] => 2014-02-24
)
[val] => Array
(
[1] => 000
)
[train] => Array
(
[1] => True
)
[valid] => Array
(
[1] => False
)
[button] => update
)
Of course, here there is only the number 1 each time but sometimes I have 0, 1, 2 and a value associated. This is because I get this from a GET from multiple forms.
How can I transform this array into
Array
(
[0] => Array
(
[rpiid] => 86
[sensor_id] => 1
...
Thanks,
John.
if your array is $get
$newArray = Array();
foreach($get as $secondKey => $innerArray){
foreach($value as $topKey => $value) {
$newArray[$topKey][$secondKey] = $value;
}
}
This should work
$new_array = array();
foreach($first_array as $value => $key){
$new_array[$key] = $value[1];
}
Sure you can, take a look at this small example:
$a = [ 'rpid' => [1], 'cpid' => [2,2] ];
$nodes = [];
foreach($a as $node => $array) {
foreach($array as $index => $value) {
if(empty($nodes[$index]))
$nodes[$index] = [];
$nodes[$index][$node] = $value;
}
}
print_r($nodes):
Array
(
[0] => Array
(
[rpid] => 1
[cpid] => 2
)
[1] => Array
(
[cpid] => 2
)
)
I have an array that looks like the following:
Array (
[0] => Array
(
[id] => 10
[value] => 5
)
[1] => Array
(
[id] => 10
[value] => 1
)
[2] => Array
(
[id] => 11
[value] => 1
)
[3] => Array
(
[id] => 11
[value] => 1
)
)
How can I consolidated the array by id? The resulting array I'm looking for would be something like:
Array (
[0] => Array
(
[id] => 10
[value] => 6
)
[1] => Array
(
[id] => 11
[value] => 2
)
)
This is not a very efficient structure. Have you considered consolidating it in this form?
array
(
10 => 6,
11 => 2,
);
That would allow fast key lookup on ID.
TO consolidate your first array into that form, just do this:
$array2 = array();
foreach($array1 as $row)
{
if(isset($array2[$row['id']]))
$array2[$row['id']] += $row['value'];
else
$array2[$row['id']] = $row['value'];
}
Which would give you an array in the form of:
$array2 = array
(
10 => 6,
11 => 2,
);
If you really need it in your requested form, one more processing loop will get it there...
$array3 = array();
foreach($array2 as $id => $value)
{
$array3[] = array('id' => $id, 'value' => $value);
}
So, there you go!
And more compact:
$array2 = array();
foreach($array1 as $row)
$array2[$row['id']] = (isset($array2[$row['id']]) ? $array2[$row['id']] : 0) + $row['value'];
$array3 = array();
foreach($array2 as $id => $value)
$array3[] = array('id' => $id, 'value' => $value);