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,
Related
I've been stuck on this for the better part of the day and I'm out of ideas. I have an array like this:
Array
(
[rank] => Array
(
[0] => 1
[1] => 2
[2] => 3
)
[name] => Array
(
[0] => 'Hideki'
[1] => 'Rory'
[2] => 'Sam'
[money] => Array
(
[0] => '$100'
[1] => '$200'
[2] => '$500'
)
)
and I have the task to create an array with the following format from it:
Array
(
[Hideki] => Array
(
[rank] => 1
[money] => '$100'
)
[Rory] => Array
(
[rank] => 2
[money] => '$200'
[Sam] => Array
(
[rank] => 3
[money] => '$500'
)
)
The catch is that 'rank' and 'money' have to be dynamic names
It should be simple as that:
$new = [];
foreach($array['name'] as $key => $name) {
$new[$name] = [
'rank' => $array['rank'][$key],
'money' => $array['money'][$key]
];
}
Little late but here my answear. My approach was to use the array_walk() function.
$array = [
'rank' => [1,2,3],
'name' => ['Hideki', 'Rory', 'Sam'],
'money' => ['$100', '$200', '$500'],
];
$i = 0;
$newArray = [];
array_walk($array['name'], function($name) use (&$i, $array, &$newArray) {
$newArray[$name] = ['rank'=> $array['rank'][$i], 'money' => $array['money'][$i]];
$i++;
});
print_r($newArray);
Run your first array through a foreach loop referencing only the "name" key and using key=>value pairs. Then reference the other keys from the first array when you build the new array, setting the value as the key to the second array.
You will need to first get the keys using array_keys() and use a nested foreach to loop through all the keys.
Example:
$keys1 = array_keys($array1);
foreach ($array1['name'] as $key => $value) {
$val2 = array();
foreach ($keys1 as $k){
if ($k != 'name') $val2[$k] = $array1[$k][$key];
}
$array2[$value] = $val2;
}
I have an array like below: all the values I am getting one array only, but I don't want this way. This is the best way to do so, in php or jQuery both languages are ok for me
Array
(
[0] => Array
(
[val] => facebook
)
[1] => Array
(
[val] => snapchat
)
[2] => Array
(
[val] => instagram
)
[3] => Array
(
[expenses] => 986532
)
[4] => Array
(
[expenses] => 45456
)
[5] => Array
(
[expenses] => 56230
)
[6] => Array
(
[social_id] => 15
)
[7] => Array
(
[social_id] => 16
)
[8] => Array
(
[social_id] => 17
)
)
and I want to output like this..
$result = array(
array(
"val" => "Facebook",
"expenses" => "84512",
"social_id" => 1
),
array(
"val" => "Instagram",
"expenses" => "123",
"social_id" => 2
)
);
but this should be dynamic, the length of the above array can be varies
You can merge the arrays to one and use array_column to get all vals or expenses in a separate array.
Then foreach one array and build the new array with the key.
//$a = your array
// Grab each column separate
$val = array_column($a, "val");
$expenses= array_column($a, "expenses");
$social_id = array_column($a, "social_id");
Foreach($val as $key => $v){
$arr[] = [$v, $expenses[$key], $social_id[$key]];
}
Var_dump($arr);
https://3v4l.org/nVtDf
Edit updated with the 'latest' version of the array. My code works just fine with this array to without any changes.
to get that array style you can do it by hand for each array
function test(array ...$arr)
{
$result = array();
foreach ($arr as $key => $pair) {
foreach ($pair as $item => $value) {
$result[$item] = $value;
}
}
return $result;
}
$arr1 = test( ['facebook' => 1], ['expenses' => 100]);
print_r($arr1);
/* Array (
[facebook] => 1,
[expenses] => 100
)
*/
$arr2 = test( $arr1, ['more info' => 'info'] );
print_r($arr2);
/* Array (
[facebook] => 1,
[expenses] => 100,
[more info] => info
)
*/
you can pass it any number of
['Key' => 'Pair']
array('Key' => 'Pair')
or even a previous made array and it will add them up into 1 big array and return it
I have an N-level Multidimentional array like this:
Array
(
[0] => Array
(
[id] => 7_cat
[text] => cat1
[children] => Array
(
[0] => Array
(
[id] => 9_cat
[text] => cat3
)
)
)
[1] => Array
(
[id] => 8_cat
[text] => cat2
)
[2] => Array
(
[id] => 13_cat
[text] => cat4
)
)
and i have an array of index like this:
Array
(
[0] => 0
[1] => children
[2] => 0
[3] => id
)
so i want to add a new element in first array in these depth. in this example i want to add a new element in $firstarray[0]['children'][0]['id'].
How can i do it?
thanks alot
Do Like this
Here $oldArray is your first array
$indexArray=Array
(
[0] => 0
[1] => children
[2] => 0
[3] => id
);
$indexForold=[];
foreach($indexArray as $indx){
$indexForold=$indexForold[$indx];
}
$oldArray[$indexForold]=$myVal;
The solution is, while traversing the array use reference to keep a reference to the current array element.
Here $nArr is your n-dimensional array and $indexArr is your index array.
And don't forget to replace <YOUR_VALUE> with your desired value.
So your code should be like this:
$arrLength = count($indexArr);
$tmpArr = null;
$i = 0;
for(; $i < $arrLength - 1; ++$i){
if($tmpArr == null){
$tmpArr = &$nArr[$indexArr[$i]];
}else{
$tmpArr = &$tmpArr[$indexArr[$i]];
}
}
$tmpArr[$indexArr[$i]] = <YOUR_VALUE>;
// now display the n-dimensional array
echo "<pre>";
print_r($nArr);
echo "</pre>";
You can try something like this...
$array = [
[
'id' => '7_cat',
'text' => 'cat1',
'children' => [
[
'id' => '9_cat',
'text' => 'cat3'
]
]
],
[
'id' => '8_cat',
'text' => 'cat2'
],
[
'id' => '13_cat',
'text' => 'cat4'
]
];
$multIndexValue = function (array $indexList, array $searchArray) {
$result = $searchArray;
foreach ($indexList as $index) {
$result = $result[$index];
}
return $result;
};
$search = [0, 'children', 0, 'id'];
$result = $multIndexValue($search, $array); // string(5) "9_cat"
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);
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);