Setting a long Array . Missing data - php

I check every variable, but each one is echoing a verified result, so the problem is in the array. When I try to echo the full list of 56 values, I just get 11.
All variables are SET before, and i check for example if i echo $uno, i get a value of 12.. so why isnt that value on the array.
Here is my code..
$items = array(
$uno => "item1",
$dos => "item2",
$tres => "item3",
$cuatro => "item4",
$cinco => "item5",
$seis => "item6",
$siete => "item7",
$ocho => "item8",
$nueve => "item9",
$diez => "item10",
$once => "item11",
$doce => "item12",
$trece => "item13",
$catorce => "item14",
$quince => "item15",
$dieciseis => "item16",
$diecisiete => "item17",
$dieciocho => "item18",
$diecinueve => "item19",
$veinte => "item20",
$veintiuno => "item21",
$veintidos => "item22",
$veintitres => "item23",
$veinticuatro => "item24",
$veinticinco => "item25",
$veintiseis => "item26",
$veintisiete => "item27",
$veintiocho => "item28",
$veintinueve => "item29",
$treinta => "item30",
$treintayuno => "item31",
$treintaydos => "item32",
$treintaytres => "item33",
$treintaycuatro => "item34",
$treintaycinco => "item35",
$treintayseis => "item36",
$treintaysiete => "item37",
$treintayocho => "item38",
$treintaynueve => "item39",
$cuarenta => "item40",
$cuarentayuno => "item41",
$cuarentaydos => "item42",
$cuarentaytres => "item43",
$cuarentaycuatro => "item44",
$cuarentaycinco => "item45",
$cuarentayseis => "item46",
$cuarentaysiete => "item47",
$cuarentayocho => "item48",
$cuarentaynueve => "item49",
$cincuenta => "item50",
$cincuentayuno => "item51",
$cincuentaydos => "item52",
$cincuentaytres => "item53",
$cincuentaycuatro => "item54",
$cincuentaycinco => "item55",
$cincuentayseis => "item56",
);
The PHP CODE
<?PHP
echo "<strong>Original</strong><br />";
foreach($items as $k => $v){
echo $k . " = " . $v . "<br />";
}
asort($items);
echo "<strong>Ascending Sort</strong><br />";
foreach($items as $k => $v){
echo $k . " = " . $v . "<br />";
}
arsort($items);
echo "<strong>Descending Sort</strong><br />";
foreach($items as $k => $v){
echo $k . " = " . $v . "<br />";
}
?>
I get this as output..
Original
11 = item30
5 = item35
12 = item20
8 = item42
7 = item45
4 = item44
6 = item47
9 = item41
10 = item37
2 = item46
0 = item56
Ascending Sort
12 = item20
11 = item30
5 = item35
10 = item37
9 = item41
8 = item42
4 = item44
7 = item45
2 = item46
6 = item47
0 = item56
Descending Sort
0 = item56
6 = item47
2 = item46
7 = item45
4 = item44
8 = item42
9 = item41
10 = item37
5 = item35
11 = item30
12 = item20

Array keys must be unique. So, use itemNN for it.
$items = array(
"item1" => $uno,
"item2" => $dos,
"item3" => $tres,
...
Or even do not use string for keys
$items = array(
1 => $uno,
2 => $dos,
3 => $tres,
...
Or even like this
$items = array(
1 => $uno,
$dos,
$tres,
...

Related

PHP array key sum value if exist in loop with dynamic value

I create of Tree (Bill of Materials) from SQL and i'm make calculation of product. I have few if in if and if in if etc. I want to display sumamry of all individual part number with total quantitny which i'm calculating dynamicly. So, before loop and function if. i'm created three variables:
$TablicaMiH = array();
$TablicaBo = array();
$TablicaAss = array();
when I meet the conditions, which depend on the if function, each of them performs operations of adding to the array. this is:
$TablicaMiH += [$SqlCheckAssyResultShowQ['PartNumber'] => $multipler];
or
$TablicaBo += [$SqlCheckAssyResultShowQ['PartNumber'] => $multipler];
or
$TablicaAssy += [$SqlCheckAssyResultShowQ['PartNumber'] => $multipler];
The commands execute correctly as needed, but they do not add the sum value from the $ multipler variable to me, they only do individual PartNumbers and take the last value from the $ multipler variable
here is the result of what was received from the script:
Tablica MiH:
Array ( [333/E8093] => 2 [332/F2592] => 3 [332/F3144] => 9 [332/F3147] => 21 [332/F2684] => 8 [333/D1641] => 12 [333/D1202] => 22 [332/F2588] => 1 [333/E7883] => 1 [333/E8131] => 1 )
Tablica BO:
Array ( [826/10381] => 12 [331/30854] => 7 [332/F3213] => 4 [123/06090] => 84 [1315/0307Z] => 1 [823/10874] => 1 [333/E7939] => 4 [813/10186] => 2 [332/H3476] => 3 [32/920300] => 11 [332/F3282] => 1 [32/926051] => 1 )
Tablica Ass:
Array ( [2th] => 1 [3TH] => 1 [4th] => 1 [5th] => 1 [6th] => 1 [7th] => 1 [8th] => 1 [9th] => 1 [10Th] => 1 [IN_1TH] => 1 )
and result what i need to have:
$TablicaMiH
332/F2588||5
332/F2592||10
332/F2684||9
332/F3144||27
332/F3147||38
333/D1202||40
333/D1641||16
333/E7883||1
333/E8093||12
333/E8131||1
Tablica BO:
123/06090||85
1315/0307Z||1
32/920300||11
32/926051||1
331/30854||20
332/f3213||29
332/F3282||1
332/H3476||3
333/E7939||4
813/10186||3
823/10874||1
826/10381||13
Tablica Ass:
10Th||1
1TH||1
2TH||1
3TH||1
4th||1
5th||1
6th||1
7th||1
8th||1
9th||1
IN_1TH||1
I Hope you understand me what i mean, and you can help me, thank you
Example to understand my problem:
<?php
$exampleArraY = array(
"PartNumber1" => 1,
"PartNumber2" => 1,
"PartNumber3" => 1,
"PartNumber4" => 1,
"PartNumber5" => 1,
"PartNumber6" => 1,
);
$value = "PartNumber1";
$value2 = "PartNumber2";
$value3 = "PartNumber3";
$value4 = "PartNumber4";
$value5 = "PartNumber5";
$value6 = "PartNumber6";
$value7 = "PartNumber7";
$multipler = 1;
if(in_array($value, $exampleArraY)){
$exampleArraY[$value][1] += $multipler;
}else {
$exampleArraY += [$value => $multipler];
}
if(in_array($value2, $exampleArraY)){
$exampleArraY[$value2][1] += $multipler;
}else {
$exampleArraY += [$value2 => $multipler];
}
if(in_array($value3, $exampleArraY)){
$exampleArraY[$value3][1] += $multipler;
}else {
$exampleArraY += [$value3 => $multipler];
}
if(in_array($value4, $exampleArraY)){
$exampleArraY[$value4][1] += $multipler;
}else {
$exampleArraY += [$value4 => $multipler];
}
if(in_array($value5, $exampleArraY)){
$exampleArraY[$value5][1] += $multipler;
}else {
$exampleArraY += [$value5 => $multipler];
}
if(in_array($value6, $exampleArraY)){
$exampleArraY[$value6][1] += $multipler;
}else {
$exampleArraY += [$value6 => $multipler];
}
if(in_array($value7, $exampleArraY)){
$exampleArraY[$value7][1] += $multipler;
}else {
$exampleArraY += [$value7 => $multipler];
}
print_r($exampleArraY);
?>
Result:
Array ( [PartNumber1] => 1 [PartNumber2] => 1 [PartNumber3] => 1 [PartNumber4] => 1 [PartNumber5] => 1 [PartNumber6] => 1 )
Desired Result:
Array ( [PartNumber1] => 2 [PartNumber2] => 2 [PartNumber3] => 2 [PartNumber4] => 2 [PartNumber5] => 2 [PartNumber6] => 2 [PartNumber7] => 1 )
If I understand the problem correctly, you might be looking for something like this:
// create the starting array
$TablicaMiH = [];
// ... do interesting things... in a loop most likely....
// if we dont have a value for this key yet, set it to 0
if (!isset($TablicaMiH[$SqlCheckAssyResultShowQ['PartNumber']])) {
$TablicaMiH[$SqlCheckAssyResultShowQ['PartNumber']] = 0;
}
// now add the multiplier for this part number
$TablicaMiH[$SqlCheckAssyResultShowQ['PartNumber']] += $multipler;
The problem like i explained was in construction of arrays. Here is my solution to your problem:
<?php
$exampleArraY = array(
"PartNumber1" => 1,
"PartNumber2" => 1,
"PartNumber3" => 1,
"PartNumber4" => 1,
"PartNumber5" => 1,
"PartNumber6" => 1,
);
$values = array(
"value" => "PartNumber1",
"value2" => "PartNumber2",
"value3" => "PartNumber3",
"value4" => "PartNumber4",
"value5" => "PartNumber5",
"value6" => "PartNumber6",
"value7" => "PartNumber7");
$multipler = 1;
foreach($values as $key => $item){
if(isset($exampleArraY[$item])){
echo $exampleArraY[$item] += $multipler;
}else {
$exampleArraY[$item] = $multipler;
}
}
Output:
array(7) {
["PartNumber1"]=>int(2)
["PartNumber2"]=>int(2)
["PartNumber3"]=>int(2)
["PartNumber4"]=>int(2)
["PartNumber5"]=>int(2)
["PartNumber6"]=>int(2)
["PartNumber7"]=>int(1)
}

PHP - Loop through array and print out value only if it changes between iteration

Apologies if this has been asked but I can't find a solution that meets my needs.
I have an array in a PHP 7 application as follows:
$data = [
0 => [
'regulations_label' => 'Europe',
'groups_label' => 'G1',
'filters_label' => 'FF1'
],
1 => [
'regulations_label' => 'Europe',
'groups_label' => 'G1',
'filters_label' => 'FF900'
],
2 => [
'regulations_label' => 'Europe',
'groups_label' => 'G1',
'filters_label' => 'FF324234'
],
3 => [
'regulations_label' => 'Europe',
'groups_label' => 'G2',
'filters_label' => 'FF23942'
],
4 => [
'regulations_label' => 'America',
'groups_label' => 'G29',
'filters_label' => 'FF3242'
],
5 => [
'regulations_label' => 'America',
'groups_label' => 'G29',
'filters_label' => 'FF78978'
],
6 => [
'regulations_label' => 'America',
'groups_label' => 'G29',
'filters_label' => 'FF48395043'
],
7 => [
'regulations_label' => 'Asia',
'groups_label' => 'G2000',
'filters_label' => 'FF7'
],
// ...
];
The output I want to achieve is like this:
Europe
- G1
-- FF1
-- FF900
- G2
-- FF23942
America
- G29
-- FF3242
-- FF48395043
Asia
- G2000
-- FF7
Essentially all it's doing is outputting the array in a structured format such that it shows the regulations_label followed by any corresponding groups_label and then any filters_label.
It's simple enough to loop through the entire array, e.g.
foreach ($data as $d) {
echo $d['regulations_label'] . "\n";
echo ' - ' . $d['groups_label'] . "\n";
echo ' -- ' . $d['filters_label'] . "\n";
}
However this introduces "duplicate" headings for regulations_label and groups_label because it's printing every single key. But I don't see how I can check if this has changed during the foreach statement because $d is always the current element.
I was attempting to do a check based on the previous array key:
foreach ($data as $key => $d) {
if ($data[$key-1]['regulations_label'] !== $d['regulations_label']) {
echo $d['regulations_label'] . "\n";
echo "-" . $d['groups_label'] . "\n";
}
}
The trouble is that this then just prints 1 groups_label so I'd end up with - for example:
Europe
- G1
America
...
It wouldn't get as far as "G2".
I can't help but think I'm going about this in a bizarre way. Can anyone advise a better solution?
Background info: The data I receive in $data isn't something I can modify because that format is required for the use case which is a feature in an application like this: jQuery load more data on scroll
you can use foreach and group by using regulations_label and groups_label
$group = [];
foreach($data as $v){
$group[$v['regulations_label']][$v['groups_label']][] = $v['filters_label'];
}
DEMO
In the simplest case you just need to add some if statements as you'd started to do, but you also have to make sure they only stop the relevant bit from printing - your version was suppressing the whole output, instead of just the top-level label:
foreach ($data as $key => $d) {
if ($key > 0) {
if ($data[$key-1]['regulations_label'] !== $d['regulations_label']) {
echo $d['regulations_label'] . "\n";
}
if ($data[$key-1]['groups_label'] !== $d['groups_label']) {
echo "-" . $d['groups_label'] . "\n";
}
}
else
{
//special case to deal with first row where $key-1 doesn't exist
echo $d['regulations_label'] . "\n";
echo "-" . $d['groups_label'] . "\n";
}
echo ' -- ' . $d['filters_label'] . "\n";
}
Demo: https://3v4l.org/ihYVj
All you need to do is remember what you last processed and then adda couple of IF's
$data = [
0 => ['regulations_label' => 'Europe','groups_label' => 'G1','filters_label' => 'FF1'],
1 => ['regulations_label' => 'Europe','groups_label' => 'G1','filters_label' => 'FF900'],
2 => ['regulations_label' => 'Europe','groups_label' => 'G1','filters_label' => 'FF324234'],
3 => ['regulations_label' => 'Europe','groups_label' => 'G2','filters_label' => 'FF23942'],
4 => ['regulations_label' => 'America','groups_label' => 'G29','filters_label' => 'FF3242'],
5 => ['regulations_label' => 'America','groups_label' => 'G29','filters_label' => 'FF78978'],
6 => ['regulations_label' => 'America','groups_label' => 'G29','filters_label' => 'FF48395043'],
7 => ['regulations_label' => 'Asia','groups_label' => 'G2000','filters_label' => 'FF7']
];
$last_reg = NULL;
$last_grp = NULL;
foreach ($data as $reg) {
if ( $last_reg != $reg['regulations_label']) {
echo $reg['regulations_label'] . "\n";
$last_reg = $reg['regulations_label'];
$last_grp = NULL;
$last_filter = NULL;
}
if ( $last_grp != $reg['groups_label']) {
echo "\t - " . $reg['groups_label'] . "\n";
$last_grp = $reg['groups_label'];
}
echo "\t\t - " . $reg['filters_label'] . "\n";
}
RESULT:
Europe
- G1
- FF1
- FF900
- FF324234
- G2
- FF23942
America
- G29
- FF3242
- FF78978
- FF48395043
Asia
- G2000
- FF7

Retrieve data from multiple arrays

Here are list of arrays which i echoed in
Array
(
[CustomerID] => manish14
[TicketID] => 45691
[TicketNumber] => 1045828
[CustomerUserID] =>
)
Array
(
[CustomerID] => ghisingraaz#gmail.com
[TicketID] => 45686
[TicketNumber] => 1045823
[CustomerUserID] => ghisingraaz#gmail.com
)
Array
(
[CustomerID] => ranjana#classic.com.np
[TicketID] => 45661
[TicketNumber] => 1045798
[CustomerUserID] => ranjana#classic.com.np
)
........
........
........
I want to display customerID , TicketID and TicketNumber of each array in a table format. How can i retrieve those values. I have this in multiple foreach loop thats why i am having problem getting those values.
Here is my code incase anyone wants to see. phpfiddle.org/main/code/wb4u-nrsj
You can find my code in this PhpFiddle.
<?PHP
error_reporting(0);
//////// OTRS specific information ////////
$url = "https://otrs.classic.com.np/otrs/rpc.pl"; //// URL for OTRS server
$username = "ctdeveloper"; //// SOAP username set in sysconfig
$password = "ctdeveloper"; //// SOAP password set in sysconfig
$TicketID = $_GET['id'];
########################################################################
#### You don't have to change anything below here, although you can ####
########################################################################
#### Initialize new client session ####
echo "<table><tr><th>CustomerID</th><th>TicketID</th><th>TicketNumber</th></tr>";
$client = new SoapClient(
null,
array(
'location' => $url,
'uri' => "Core",
'trace' => 1,
'login' => $username,
'password' => $password,
'style' => SOAP_RPC,
'use' => SOAP_ENCODED
)
);
#### Initialize new client session ####
$client = new SoapClient(
null,
array(
'location' => $url,
'uri' => "Core",
'trace' => 1,
'login' => $username,
'password' => $password,
'style' => SOAP_RPC,
'use' => SOAP_ENCODED
)
);
$queues = array(1 => "Postmaster",
2 => "Raw",
3 => "Junk",
4 => "Retail Support",
5 => "Enterprise Support",
6 => "Sales",
7 => "Marketing",
8 => "Fiber Survey",
9 => "Fiber Support",
10 => "Billing",
11 => "NOC",
12 => "Wireless Support",
13 => "Core-Tasks",
14 => "Chitwan",
15 => "Developer",
16 => "Operations",
17 => "Administration",
18 => "Hetauda",
19 => "Business",
20 => "Info",
21 => "Corporate",
22 => "Wireless Survey",
23 => "Recovery",
24 => "L2-Support",
);
foreach ($queues as $queue_number => $queue_name) {
#### Create and send the SOAP Function Call ####
$TicketDetail_search = $client->__soapCall("Dispatch",
array($username, $password,
"TicketObject", "TicketSearch",
"Result", "ARRAY",
"UserID", 1,
"QueueIDs", array($queue_number),
"StateType", "open"
));
// REMOVE s-gensym
$ticketInfo = array();
if ($TicketDetail_search) {
foreach ($TicketDetail_search as $name => $value){
if (false !== strpos($name, "s-gensym"))
{
$ticketInfo[] = $value;
}
}
}
foreach($ticketInfo as $TicketID)
{
$TicketDetail_get = $client->__soapCall("Dispatch",
array($username, $password,
"TicketObject", "TicketGet",
"TicketID", $TicketID,
));
foreach($TicketDetail_get as $t)
{
$ticketInfo1 = array();
$i = 0;
foreach ($TicketDetail_get as $name => $value){ // explode the xml response
if (false !== strpos($name, "s-gensym")){
$temp[$i] = $value;
if($i > 0) {
$v = $temp[$i-1];
if($i % 2 != 0){
$ticketInfo1[$v] = $value;
}
}
$i++;
}
}
}
echo "<tr>";
echo "<td>" . $ticketInfo1['CustomerID'] . "</td>";
echo "<td>" . $ticketInfo1['TicketID'] . "</td>";
echo "<td>" . $ticketInfo1['TicketNumber'] . "</td>";
echo "</tr>";
}
}
echo "</table>";
?>
Is this what you are looking for ?

Combine array to one array

I have an array:
$settings = array(
'name' => array(
0 => 'Large Pouch',
1 => 'XL Pouch'
),
'size' => array(
0 => '9x14',
1 => '12x18'
),
'weight' => array(
0 => '10',
1 => '20'
),
'metro_manila_price' => array(
0 => '59',
1 => '79'
),
'luzvimin_price' => array(
0 => '89',
1 => '139'
)
);
I wanted to put the values from that array to one array. $shipping_options with format of
for example:
$shipping_options = array(
'0' => 'Large Pouch 9x14 - $59',
'1' => 'XL Pouch 12x18 - $79'
);
How to program this?
You could write a loop:
$shipping_options = array();
foreach ($settings['name'] as $key => $value) {
$value = sprintf('%s(%s) - $%s',
$value,
$settings['size'][$key],
$settings['metro_manila_price'][$key]);
$shipping_options[$key] = $value;
}
try this one
echo "<pre>";
$size = count($settings['name']);
$shipping_options = array();
for($i=0; $i<$size; $i++)
{
$shipping_options[$i] = $settings['name'][$i]."(".$settings['size'][$i].") - $".$settings['metro_manila_price'][$i];
}
print_r($shipping_options);
You can try this
foreach ($settings['name'] as $key => $value) {
$shipping_options[$key] = $settings['name'][$key] . " " . $settings['size'][$key] . " - $" . $settings['metro_manila_price'][$key];
}

Arrays into new PHP file [duplicate]

This might be very silly but we have not been able to do this .
We are trying to insert an array into a php file . However we are able to insert the array into a php file but the format is not as desired . We have 2 files .
File1.php
<?php
include_once("../dc/dcCommonLib.php");
include_once("../dc/persistence/UserData.php");
require_once("../../lib/include/connect.inc.php");
$_SESSION["survey"] = "BestBuyAug2012";
$idLink = dbconnect($_SESSION["survey"]);
$surveyWidget = rendererSurveyLoadHandler();
setDataToSession("surveywidget",$surveyWidget);
$userData = &UserData::getInstance();
$userData->setSurvey($surveyWidget);
saveFkidsToOidToFkidMappingArray($userData->OidToFkidMapping);
// prepare demo array
prepareItemArray($userData->OidToFkidMapping["Demographic"]["Q"],$demo,"b");
// prepare item array
prepareItemArray($userData->OidToFkidMapping["Default"]["Q"],$item,"i");
prepareItemArray($userData->OidToFkidMapping["Default"]["M"],$item,"i");
// prepare comment array
prepareCommentArray($userData->OidToFkidMapping["Comment"]["C"],$comment);
if(!function_exists('file_put_contents')) {
function file_put_contents($filename, $data,$type, $file_append = false) {
$fp = fopen($filename, (!$file_append ? 'w+' : 'a+'));
if(!$fp) {
trigger_error('file_put_contents cannot write in file.', E_USER_ERROR);
return;
}
if($type){
$count = count($data);
$i = 1;
//loop through your type array and append comma , to each except last
foreach($data as $key=>$val) {
//if we are before last key, append comma
if($i < $count)
$data[$key] .= ',';
$i++;
}
//run same preg_replace as before
$data2 = preg_replace('/[\[\]]/','"',print_r($data,TRUE));
//write to file
fputs($fp, "$" . $type . " = " . print_r($data2,TRUE));
} else {
fputs($fp, print_r($data, TRUE));
}
fclose($fp);
}
}
print("Strart writting ...................");
file_put_contents("staticSurveyDataFileTest.php", "<?php\n","",true);
file_put_contents("staticSurveyDataFileTest.php", $item,"itemFkids",true);
file_put_contents("staticSurveyDataFileTest.php", $demo,"demoFkids",true);
file_put_contents("staticSurveyDataFileTest.php", $comment,"commentFkids",true);
file_put_contents("staticSurveyDataFileTest.php", "?>","",true);
print("written ...");
function prepareItemArray($userDataArray, &$item, $itemStr) {
if(is_array($userDataArray)) {
foreach($userDataArray as $questId=>$respGrp) {
$selectionGrp=array_unique($respGrp);
if(count($selectionGrp)==1) {
$respGrpId=key($respGrp);
$respId = key($respGrp[$respGrpId]);
$item[$questId."_".key($respGrp)]=$itemStr.$respGrp[$respGrpId][$respId];
}else {
foreach ($respGrp as $respGrpId=>$resp) {
$respGrp[$respGrpId]=$itemStr.$resp;
}
$item[$questId."_".key($respGrp)]=$respGrp;
}
}
}
}
function prepareCommentArray($userDataArray, &$item) {
if(is_array($userDataArray)) {
foreach($userDataArray as $questId=>$respGrp) {
$selectionGrp=array_unique($respGrp);
if(count($selectionGrp)==1) {
$respGrpId=key($respGrp);
$item[$questId."_".key($respGrp)]=$respGrp[$respGrpId]["fk_id"];
}
}
}
}
?>
The array from this file is inserted into staticSurveyDataFileTest.php
staticSurveyDataFileTest.php
<?php
$itemFkids = Array
(
"203_19" => i27,//double quotes missing for all the lines . See below for desired output//
"207_22" => i28,
"357_22" => i99,
"370_70" => i104,
"377_72" => i105,
"140_8" => i1,
"141_8" => i2,
"142_8" => i3,
"143_8" => i4,
"144_8" => i5,
"145_8" => i6,
"151_8" => i7,
"152_8" => i8,
"158_8" => i9,
"164_8" => i10,
"165_8" => i11,
"166_8" => i12,
"167_8" => i13,
"168_8" => i14,
"169_8" => i15,
"175_8" => i16,
"176_8" => i17,
"182_8" => i18,
"183_8" => i19,
"184_8" => i20,
"190_8" => i21,
"196_8" => i22,
"197_8" => i23,
"198_8" => i24,
"199_8" => i25,
"200_8" => i26,
"214_8" => i29,
"215_8" => i30,
"216_8" => i31,
"222_8" => i32,
"223_8" => i33,
"224_8" => i34,
"225_8" => i35,
"226_8" => i36,
"227_8" => i37,
"233_8" => i38,
"234_8" => i39,
"235_8" => i40,
"236_8" => i41,
"237_8" => i42,
"243_8" => i43,
"244_8" => i44,
"245_8" => i45,
"251_8" => i46,
"252_8" => i47,
"253_8" => i48,
"254_8" => i49,
"255_8" => i50,
"256_8" => i51,
"257_8" => i52,
"263_8" => i53,
"264_8" => i54,
"265_8" => i55,
"266_8" => i56,
"267_8" => i57,
"268_8" => i58,
"269_8" => i59,
"270_8" => i60,
"271_8" => i61,
"272_8" => i62,
"278_8" => i63,
"279_8" => i64,
"280_8" => i65,
"281_8" => i66,
"282_8" => i67,
"283_8" => i68,
"289_8" => i69,
"290_8" => i70,
"296_8" => i71,
"297_8" => i72,
"298_8" => i73,
"299_8" => i74,
"300_8" => i75,
"301_8" => i76,
"307_8" => i77,
"308_8" => i78,
"309_8" => i79,
"310_8" => i80,
"311_8" => i81,
"312_8" => i82,
"313_8" => i83,
"314_8" => i84,
"315_8" => i85,
"316_8" => i86,
"317_8" => i87,
"318_8" => i88,
"324_26" => i89,
"328_37" => i90,
"332_48" => i91,
"338_59" => i92,
"344_59" => i93,
"345_59" => i94,
"351_8" => i95,
"352_8" => i96,
"353_8" => i97,
"354_8" => i98,
"364_8" => i100,
"365_8" => i101,
"366_8" => i102,
"367_8" => i103
)//comma missing
$demoFkids = Array
(
"129_2" => b1
)//comma missing
$commentFkids = Array
(
"373_375" => 1,
"380_382" => 2
)//comma missing
?>
We need to insert a file something like this
Desired Output
<?php
$itemFkids = Array
(
"203_19" => "i27",//notices the double quotes here
"207_22" => "i28",
"357_22" => "i99",
.
.
.
.
.
.
),//notice a comma here
$demoFkids = Array
(
"129_2" => "b1"//notices the double quotes here
),//notice a comma here
$commentFkids = Array
(
"373_375" => "1",
"380_382" => "2"//notices the double quotes here
),//notice a comma here
?>
Thanks for your help in advance :)
You can use something like this:
<?php
$a = array (1, 2, array ("a", "b", "c"));
var_export($a);
?>
And this outputs:
array (
0 => 1,
1 => 2,
2 =>
array (
0 => 'a',
1 => 'b',
2 => 'c',
),
)
Check out the documentation at http://php.net/manual/fr/function.var-export.php
Code for the desired output:
<?php
$result = array_merge($itemFkids, $demoFkids, $commentFkids);
var_export($result);
?>
Outputs:
<?php
$itemFkids = Array
(
"203_19" => "i27",
"207_22" => "i28",
"357_22" => "i99",
.
.
.
),
$demoFkids = Array
(
"129_2" => "b1"
),
$commentFkids = Array
(
"373_375" => "1",
"380_382" => "2"
),
?>
Cannot reliably say until we work with the full code! :)
try using the php function var_export
Even trying this is worth:
<?php
class A
{
public $var1;
public $var2;
public static function __set_state($an_array)
{
$obj = new A;
$obj->var1 = $an_array['var1'];
$obj->var2 = $an_array['var2'];
return $obj;
}
}
$a = new A;
$a->var1 = 5;
$a->var2 = 'foo';
eval('$b = ' . var_export($a, true) . ';'); // $b = A::__set_state(array(
// 'var1' => 5,
// 'var2' => 'foo',
// ));
var_dump($b);
?>
The output of the above is:
object(A)#2 (2) {
["var1"]=>
int(5)
["var2"]=>
string(3) "foo"
}

Categories