Offline and online server processes code differently - php

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.

Related

Rebuild array to add keys from a second array

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']
);
}
}

php, 3 array to one array merge alternative

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
)
)
)

Merging 2D array by ID - PHP

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

PHP merge associative arrays by replacing same keys and adding new

I have an array of variable size structured like this (categories is only one of the keys inside data):
print_r($json[123]["data"]["categories"]);
array(
array(
'id' => '2',
'description' => 'Single-player'
),
array(
'id' => '1',
'description' => 'Multi-player'
),
array(
'id' => '9',
'description' => 'Co-op'
),
array(
'id' => '22',
'description' => 'Steam Achievements'
),
array(
'id' => '28',
'description' => 'Full controller support'
)
)
print_r($json[456]["data"]["categories"]);
array(
array(
'id' => '21',
'description' => 'Downloadable Content'
),
array(
'id' => '1',
'description' => 'Multi-player'
)
)
Now, I want to merge these sub-arrays (they can be in variable number) and have all keys added and replaced. I've tried array_merge but it replaces the keys without adding new ones.
In this case I need to obtain this array:
print_r($merged["data"]["categories"]);
array(
array(
'id' => '2',
'description' => 'Single-player'
),
array(
'id' => '1',
'description' => 'Multi-player'
),
array(
'id' => '9',
'description' => 'Co-op'
),
array(
'id' => '22',
'description' => 'Steam Achievements'
),
array(
'id' => '28',
'description' => 'Full controller support'
),
array(
'id' => '21',
'description' => 'Downloadable Content'
)
)
Any help?
Edit:
I think I didn't expressed myself well enough. $json[$id]["data"] has multiple keys I want to merge (categories is just an example). Also the number of $json[$id] keys is variable
Edit2:
The arrays can have duplicate values, and the depth of the keys can be variable. I need to get something like array_merge_recursive() but with same values replaced.
Edit3:
This is the current array. http://pastebin.com/7x7KaAVM I need to merge all keys that have sub-arrays
Try below code:
$json = array(
'123' => array('data' => array('categories' => array(
array(
'id' => '2',
'description' => 'Single-player'
),
array(
'id' => '1',
'description' => 'Multi-player'
),
array(
'id' => '9',
'description' => 'Co-op'
),
array(
'id' => '22',
'description' => 'Steam Achievements'
),
array(
'id' => '28',
'description' => 'Full controller support'
)
))
),
'456' => array('data' => array('categories' => array(
array(
'id' => '21',
'description' => 'Downloadable Content'
)
))
),
);
//print_r($json);
$merged = array();
foreach($json as $j1)
{
foreach($j1 as $j2)
{
foreach($j2 as $key => $j3)
{
foreach($j3 as $j4)
{
$merged[$key][] = $j4;
}
}
}
}
print_r($merged);
Result:
Array
(
[categories] => Array
(
[0] => Array
(
[id] => 2
[description] => Single-player
)
[1] => Array
(
[id] => 1
[description] => Multi-player
)
[2] => Array
(
[id] => 9
[description] => Co-op
)
[3] => Array
(
[id] => 22
[description] => Steam Achievements
)
[4] => Array
(
[id] => 28
[description] => Full controller support
)
[5] => Array
(
[id] => 21
[description] => Downloadable Content
)
)
)
Demo:
http://3v4l.org/X61bE#v430
Try this . To generalize I have added some more arrays.
<?php
$merged_array = array();
$final_array = array();
$json[123]["data"]["categories"] = array(
array(
'id' => '2',
'description' => 'Single-player'
),
array(
'id' => '1',
'description' => 'Multi-player'
),
array(
'id' => '9',
'description' => 'Co-op'
),
array(
'id' => '22',
'description' => 'Steam Achievements'
),
array(
'id' => '28',
'description' => 'Full controller support'
)
);
$json[456]["data"]["categories"] = array(
array(
'id' => '21',
'description' => 'Downloadable Content'
)
);
$json[786]["data"]["categories"] = array(
array(
'id' => '31',
'description' => 'Downloadable Content'
)
);
$json[058]["data"]["categories"] = array(
array(
'id' => '41',
'description' => 'Downloadable Content'
)
);
foreach($json as $key=>$value){
array_push($merged_array,$json[$key]["data"]["categories"]);
}
foreach($merged_array as $value){
foreach($value as $val){
array_push($final_array,$val);
}
}
print_r($final_array);
?>
RESULT
Array
(
[0] => Array
(
[id] => 2
[description] => Single-player
)
[1] => Array
(
[id] => 1
[description] => Multi-player
)
[2] => Array
(
[id] => 9
[description] => Co-op
)
[3] => Array
(
[id] => 22
[description] => Steam Achievements
)
[4] => Array
(
[id] => 28
[description] => Full controller support
)
[5] => Array
(
[id] => 21
[description] => Downloadable Content
)
[6] => Array
(
[id] => 31
[description] => Downloadable Content
)
[7] => Array
(
[id] => 41
[description] => Downloadable Content
)
)

PHP Convert array group by categories with one title only

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);

Categories