How can I use Array1 and Array2 to get the Wanted Result?
Array1
array(
(int) 0 => '37',
(int) 1 => '38'
)
Array2
array(
(int) 0 => array(
'ParentKey' => array(
'ChildKey1' => '1',
'ChildKey2' => '2'
)
),
(int) 1 => array(
'ParentKey' => array(
'ChildKey1' => '1',
'ChildKey2' => '1'
)
)
)
Wanted Result
array(
(int) 0 => array(
'ParentKey' => array(
'Array1Key' => 37,
'ChildKey1' => '1',
'ChildKey2' => '2'
)
),
(int) 1 => array(
'ParentKey' => array(
'Array1Key' => 37,
'ChildKey1' => '1',
'ChildKey2' => '1'
)
)
(int) 2 => array(
'ParentKey' => array(
'Array1Key' => 38,
'ChildKey1' => '1',
'ChildKey2' => '2'
)
),
(int) 3 => array(
'ParentKey' => array(
'Array1Key' => 38,
'ChildKey1' => '1',
'ChildKey2' => '1'
)
)
)
I have tried several things but this got me the closest...
PHP
$data = array();
foreach($Array1 as $id)
{
foreach($Array2 as $Array1Result)
{
$data[]['ParentKey'] = array(
'Array1Key' => $id,
$Array1Result['ParentKey']
);
}
}
Result
array(
(int) 0 => array(
'ParentKey' => array(
'Array1Key' => '37',
(int) 0 => array(
'ChildKey1' => '1',
'ChildKey2' => '2'
)
)
),
(int) 1 => array(
'ParentKey' => array(
'Array1Key' => '37',
(int) 0 => array(
'ChildKey1' => '1',
'ChildKey2' => '1'
)
)
),
(int) 2 => array(
'ParentKey' => array(
'Array1Key' => '38',
(int) 0 => array(
'ChildKey1' => '1',
'ChildKey2' => '2'
)
)
),
(int) 3 => array(
'ParentKey' => array(
'Array1Key' => '38',
(int) 0 => array(
'ChildKey1' => '1',
'ChildKey2' => '1'
)
)
)
)
What you get is exactly what you do. You put an array inside an array. But you need to merge two arrays together. The first one being the Array1Key and the second one the old array. You can achieve this with array_merge.
$data = array();
foreach($Array1 as $id)
{
foreach($Array2 as $Array1Result)
{
$data[]['ParentKey'] = array_merge(
array('Array1Key' => $id),
$Array1Result['ParentKey']
);
}
}
Related
I have 3 arrays. These inter-related. I want to combine these statements.
<?php
$post = array(
array (
'id' => 1,
'title' => 'Title 1',
'content' => 'Content 1'
),
array (
'id' => 2,
'title' => 'Title 2',
'content' => 'Content 2'
),
array (
'id' => 3,
'title' => 'Title 3',
'content' => 'Content 3'
),
);
$user = array(
array (
'id' => 1,
'name' => 'Mark'
),
array (
'id' => 2,
'name' => 'Selena'
)
);
$post_user = array(
array (
'id' => 1,
'post_id' => 1,
'user_id' => 1
),
array (
'id' => 2,
'post_id' => 2,
'user_id' => 1
),
array (
'id' => 3,
'post_id' => 3,
'user_id' => 2
),
);
$merge = array();
foreach($posts_user as $data){
foreach ($posts as $post){
if($data['post_id'] == $post['id'] ){
foreach ($users as $user){
if($data['user_id'] == $user['id']){
$post['user'] = $user;
$merge['post'][] = $post;
}
}
}
}
}
print_r($merge);
I want to be as follows. How can I do it?
I want results.
$merge = array(
array (
'id' => 1,
'title' => 'Title 1',
'content' => 'Content 1',
'user' => array (
'id' => '1',
'name' => 'Mark'
),
),
array (
'id' => 2,
'title' => 'Title 2',
'content' => 'Content 2',
'user' => array (
'id' => '1',
'name' => 'Mark'
),
),
array (
'id' => 3,
'title' => 'Title 3',
'content' => 'Content 3',
'user' => array (
'id' => '2',
'name' => 'Selena'
),
),
);
Is there another alternative? For example; How do I do with them?
array_walk_recursive(), ArrayIterator(), RecursiveArrayIterator()
I tried to do.
<?php
$post = array(
array (
'id' => 1,
'title' => 'Title 1',
'content' => 'Content 1'
),
array (
'id' => 2,
'title' => 'Title 2',
'content' => 'Content 2'
),
array (
'id' => 3,
'title' => 'Title 3',
'content' => 'Content 3'
),
);
$user = array(
array (
'id' => 1,
'name' => 'Mark'
),
array (
'id' => 2,
'name' => 'Selena'
)
);
$post_user = array(
array (
'id' => 1,
'post_id' => 1,
'user_id' => 1
),
array (
'id' => 2,
'post_id' => 2,
'user_id' => 1
),
array (
'id' => 3,
'post_id' => 3,
'user_id' => 2
),
);
$newuser = array();
foreach($post_user as $data){
foreach ($user as $users){
if($data['user_id'] == $users['id']){
$newuser[] = $users;
}
}
}
$mi = new MultipleIterator(MultipleIterator::MIT_NEED_ALL|MultipleIterator::MIT_KEYS_ASSOC);
$mi->attachIterator(new ArrayIterator($post),1);
$mi->attachIterator(new ArrayIterator($newuser),'user');
$newArray = array();
foreach($mi as $details) {
$newArray[] = $details;
}
print_r($newArray);
Results
Array
(
[0] => Array
(
[1] => Array
(
[id] => 1
[title] => Title 1
[content] => Content 1
)
[user] => Array
(
[id] => 1
[name] => Mark
)
)
[1] => Array
(
[1] => Array
(
[id] => 2
[title] => Title 2
[content] => Content 2
)
[user] => Array
(
[id] => 1
[name] => Mark
)
)
[2] => Array
(
[1] => Array
(
[id] => 3
[title] => Title 3
[content] => Content 3
)
[user] => Array
(
[id] => 2
[name] => Selena
)
)
)
Hello guys I am stuck for this kind of merging of arrays:
Sample Array
Array(
[0] => Array(
'id' => '1',
'task' => 'Task 1.0'
),
[1] => Array(
'id' => '1',
'task' => 'Task 1.1'
),
[2] => Array(
'id' => '2',
'task' => 'Task 2.0'
),
[3] => Array(
'id' => '2',
'task' => 'Task 2.1'
)
)
Expected Result
Array(
[0] => Array(
'id' => '1',
'task' => array(
[0] => 'Task 1.0',
[1] => 'Task 1.1'
)
),
[1] => Array(
'id' => '2',
'task' => array(
[0] => 'Task 2.0',
[1] => 'Task 2.1'
)
)
)
How can I do this kind of merging?
Thanks in advance.
this might not be the best solution, but i would consider it as an aproach:
$oldArray = array (
0 => array(
'id' => '1',
'task' => 'Task 1.0'
),
1 => array(
'id' => '1',
'task' => 'Task 1.1'
),
2 => array(
'id' => '2',
'task' => 'Task 2.0'
),
3 => array(
'id' => '2',
'task' => 'Task 2.1'
)
);
$newArray = array();
foreach( $oldArray as $array ) {
if( !isset( $newArray[$array["id"]] ) ) {
$newArray[$array["id"]] = array( "id" => $array["id"] );
}
$newArray[$array["id"]]["task"][] = $array["task"];
}
// reset the temp keys
$newArray = array_values( $newArray );
Edited: forgot "tasks" in $newArray[$array["id"]]["task"][] = $array["task"];, made an edit again
I have 2 arrays:
$myArray = array ( 'name' => 'Dollar', 'sign' => '$', 'format' => '1', 'decimals' => '1', 'conversion_rate' => '1.324400' );
$myArrayNew = array ( 0 => 'Name', 1 => 'Sign', 2 => 'Format', 3 => 'Decimals', 4 => 'Conversion Rate');
When i use
$combinedarrays = array_combine($myArrayNew, $myArray);
the output is
Array ( [Name] => Dollar [Sign] => $ [Format] => 1 [Decimals] => 1 [Conversion Rate] => 1.324400 )
That is what I need, but the problem is when my first array is multidimensional like:
$myArray = array( array ( 'name' => 'Dollar', 'sign' => '$', 'format' => '1', 'decimals' => '1', 'conversion_rate' => '1.324400' ),
array ( 'name' => 'Euro', 'sign' => '€', 'format' => '2', 'decimals' => '1', 'conversion_rate' => '1.000000' ));
So, how to change the keys to be like below?
$myArray = array( array ( 'Name' => 'Dollar', 'Sign' => '$', 'Format' => '1', 'Decimals' => '1', 'Conversion Rate' => '1.324400' ),
array ( 'Name' => 'Euro', 'Sign' => '€', 'Format' => '2', 'Decimals' => '1', 'Conversion Rate' => '1.000000' ));
You need to loop on sub arrays.
Try this:
$myArrayNew = array ( 0 => 'Name', 1 => 'Sign', 2 => 'Format', 3 => 'Decimals', 4 => 'Conversion Rate');
$myArray = array( array ( 'name' => 'Dollar', 'sign' => '$', 'format' => '1', 'decimals' => '1', 'conversion_rate' => '1.324400' ),
array ( 'name' => 'Euro', 'sign' => '€', 'format' => '2', 'decimals' => '1', 'conversion_rate' => '1.000000' ));
$combinedarrays = array();
foreach($myArray as $subarr)
$combinedarrays[] = array_combine($myArrayNew, $subarr);
print_r($combinedarrays);
I have this array:
$result = array(
'0' => array(
'field_categories' => 'Visitor',
'field_name' => 'Visitor-1',
),
'1' => array(
'field_categories' => 'Visitor',
'field_name' => 'Visitor-2'
),
'2' => array(
'field_categories' => 'Visitor',
'field_name' => 'Visitor-3'
),
'3' => array(
'field_categories' => 'Visitor',
'field_name' => 'Visitor-4'
),
'4' => array(
'field_categories' => 'Doctor',
'field_name' => 'Doctor-1'
),
'5' => array(
'field_categories' => 'Doctor',
'field_name' => 'Doctor-2'
),
'6' => array(
'field_categories' => 'Manager',
'field_name' => 'Manager-1'
),
'7' => array(
'field_categories' => 'Manager',
'field_name' => 'Manager-2'
),
'8' => array(
'field_categories' => 'Manager',
'field_name' => 'Manager-3'
)
);
echo '<pre>',print_r($result),'</pre>';
And how can I convert it to this format?
(output of echo '<pre>',print_r($result),'</pre>';) Thanks
Array
(
[0] => Visitor
[1] => Visitor-1
[2] => Visitor-2
[3] => Visitor-3
[4] => Visitor-4
[5] => Doctor
[6] => Doctor-1
[7] => Doctor-2
[8] => Manager
[9] => Manager-1
[10] => Manager-2
[11] => Manager-3
)
$result = array(
'0' => array(
'field_categories' => 'Visitor',
'field_name' => 'Visitor-1',
),
'1' => array(
'field_categories' => 'Visitor',
'field_name' => 'Visitor-2'
),
'2' => array(
'field_categories' => 'Visitor',
'field_name' => 'Visitor-3'
),
'3' => array(
'field_categories' => 'Visitor',
'field_name' => 'Visitor-4'
),
'4' => array(
'field_categories' => 'Doctor',
'field_name' => 'Doctor-1'
),
'5' => array(
'field_categories' => 'Doctor',
'field_name' => 'Doctor-2'
) );
foreach($result as $key=>$val)
{
$newarray[$val['field_categories']][] = $val['field_name'];
}
print_r( $newarray);
output
Array
(
[Visitor] => Array
(
[0] => Visitor-1
[1] => Visitor-2
[2] => Visitor-3
[3] => Visitor-4
)
[Doctor] => Array
(
[0] => Doctor-1
[1] => Doctor-2
)
)
Just add this line before you echo statement:
$result = array_map(function($e){return isset($e['field_name'])?$e['field_name']:null;} , $result);
If you are trying to de-dupe field_name and field_categories you can assign each value to the key of an array, and then do an array_flip
$new_result = array();
$i = 0;
foreach($result as $res){
$new_result[$res["field_name"]] = $i;
$new_result[$res["field_categories"]] = $i+1;
$i += 2;
}
$new_result = array_flip($new_result);
I have a piece of code that get's processed differently on my localhost and live server.
I have no idea why or what to change.
Here is the piece of code:
for($k = 0; $k < count($data['SurveyAnswer']); $k++) {
if(isset($data['SurveyAnswer'][$k]['answer']['number'])) {
if($data['SurveyAnswer'][$k]['answer']['number'] != '')
$data['SurveyAnswer'][$k]['answer'] = $data['SurveyAnswer'][$k]['answer']['number'].','.$data['SurveyAnswer'][$k]['answer']['text'];
else
$data['SurveyAnswer'][$k]['answer'] = '';
} else if(isset($data['SurveyAnswer'][$k]['answer']['yn'])) {
if($data['SurveyAnswer'][$k]['answer']['yn'] == 'No')
$data['SurveyAnswer'][$k]['answer'] = 'No,' . $data['SurveyAnswer'][$k]['answer']['text'];
else
$data['SurveyAnswer'][$k]['answer'] = 'Yes';
} else if(isset($data['SurveyAnswer'][$k]['answer']['scale'])) {
$data['SurveyAnswer'][$k]['answer'] = $data['SurveyAnswer'][$k]['answer']['scale'] . ',' . $data['SurveyAnswer'][$k]['answer']['text'];
}
}
For arguments sake, this is the data:
data = array(
'SurveyAnswer' => array(
0 => array (
'answer' => array(
'number' => '4',
'text' => 'Test text'
),
1 => array (
'answer' => array(
'number' => '',
'text' => ''
),
2 => array (
'answer' => array(
'yn' => 'No',
'text' => 'Test text'
),
3 => array (
'answer' => array(
'yn' => 'Yes',
'text' => ''
),
4 => array (
'answer' => array(
'scale' => 'Good',
'text' => 'Testing text'
)
),
5 => array (
'answer' => '3'
)
)
);
This is how my localhost changes the data (PHP version 5.4.7 on Windows):
data = array(
'SurveyAnswer' => array(
0 => array (
'answer' => '4,Test text'
),
1 => array (
'answer' => ''
),
2 => array (
'answer' => 'No,Test text'
),
3 => array (
'answer' => 'Yes'
),
4 => array (
'answer' => 'Good,Testing text'
)
),
5 => array (
'answer' => '3'
)
)
);
And this is how my live server changes the data (PHP version 5.3.23 on CentOS):
data = array(
'SurveyAnswer' => array(
0 => array (
'answer' => '4,4'
),
1 => array (
'answer' => ''
),
2 => array (
'answer' => 'T,T'
),
3 => array (
'answer' => 'Yes'
),
4 => array (
'answer' => 'T,T'
)
),
5 => array (
'answer' => '3'
)
)
);
Can it be the version difference causing the issue or is there something else.
UPDATE
Real world data, got this with a CakePHP debug before and CakePHP debug after the code:
localhost
\app\Controller\SurveyAnswersController.php (line 51) (BEFORE)
array(
'SurveyAnswer' => array(
(int) 0 => array(
'answer' => '1'
),
(int) 1 => array(
'answer' => '2'
),
(int) 2 => array(
'answer' => '3'
),
(int) 3 => array(
'answer' => array(
'number' => '3',
'text' => 'asdfasdfasdf'
)
)
)
)
\app\Controller\SurveyAnswersController.php (line 67) (AFTER)
array(
'SurveyAnswer' => array(
(int) 0 => array(
'answer' => '1'
),
(int) 1 => array(
'answer' => '2'
),
(int) 2 => array(
'answer' => '3'
),
(int) 3 => array(
'answer' => '3,asdfasdfasdf'
)
)
)
webserver
/app/Controller/SurveyAnswersController.php (line 51) (BEFORE)
array(
'SurveyAnswer' => array(
(int) 0 => array(
'answer' => '1'
),
(int) 1 => array(
'answer' => '2'
),
(int) 2 => array(
'answer' => '3'
),
(int) 3 => array(
'answer' => array(
'number' => '3',
'text' => 'asdfasdfasdf'
)
)
)
)
app/Controller/SurveyAnswersController.php (line 67) (AFTER)
array(
'SurveyAnswer' => array(
(int) 0 => array(
'answer' => '1,1'
),
(int) 1 => array(
'answer' => '2,2'
),
(int) 2 => array(
'answer' => '3,3'
),
(int) 3 => array(
'answer' => '3,asdfasdfasdf'
),
)
)
I shortened the data as it's around 100 questions.
Believe it or not, but it was a version issue. Updated my PHP to 5.4.x and it worked.