I have an array of arrays that contain array key with the value, here the example :
Array (
[0] => Array ( [Hotel] => N [Jayakarta] => NE [Jaya] => NE [sangat] => ADV [nyaman] => ADJ [ditempati] => V. )
[1] => Array ( [Andi] => NOB [Hotel] => N [menginap] => V [disana] => N [selama] => N [satu] => NUM [minggu] => N. )
)
I want to make an output if I found a key of "Hotel" then I will print next key from "Hotel". For example :
Hotel Jayakarta Jaya
Hotel menginap disana
Here's what I am doing for the moment:
foreach($token2 as $index => $tok){
foreach ($tok as $tokkey => $tokvalue) {
if ($tokkey == "Hotel" and $tokvalue == "N"){
echo $tokkey . " " ;
while($cek == true && $x <= 2){
$next = next($tok);
echo key($tok). " " ;
$x++;
}
echo ", ";
$x = 1;
}
}
}
And the output :
Hotel Jayakarta jaya sangat , Hotel Hotel menginap
The Expected Output should be as below:
Hotel Jayakarta jaya sangat , Hotel menginap disana selama
Any help is much appreciated, Thank you.
Do something like following:
$i = 3;
foreach($token2 as $index => $tok){
foreach ($tok as $tokkey => $tokvalue) {
if ($tokkey == "Hotel" && $tokvalue == "N"){
echo $tokkey . " " ;
$i = 1;
}
else if($i < 3){
echo $tokkey . " ";
$i++;
}
}
echo ", ";
}
maybe you could try this ?
$arrayOfHotels = array(0 => array('Hotel' => 'N' , 'Jakarta' => 'NE', 'Jaya' => 'NE', 'sangat' => 'ADV', 'nyaman' => 'ADJ', 'ditempati' => 'V.'),1 => array('Andi' => 'NOB', 'Hotel' => 'N', 'menginap' => 'V', 'disana' => 'N', 'selama' => 'N', 'satu' => 'NUM', 'minggu' => 'N.'));
echo '<pre>';
print_r(findHotel($arrayOfHotels));
echo '</pre>';
function findHotel($hotels)
{
$isHotelFind = false;
$isFirstThrough = true;
foreach($hotels as $row => $value)
{
foreach ($value as $row2 => $value2)
{
if($row2 == "Hotel" && $value = 'N' && !$isHotelFind)
{
if($isFirstThrough)
{
echo $row2 . " ";
}
else
{
echo ", " . $row2 . " ";
}
$isHotelFind = true;
$isFirstThrough = false;
}
if($isHotelFind && $row2 != "Hotel")
{
echo $row2 . " ";
}
}
$isHotelFind = false;
}
}
You can use array_search to find your key and remove leading elements before it.
$array = [
[
"Hotel" => 'N',
"Jayakarta" => 'NE',
'Jaya' => 'NE',
'sangat' => 'ADV',
'nyaman' => 'ADJ',
'ditempati' => 'V.'
]
,
[
"Andi" => 'NOB',
"Hotel" => 'N',
'menginap' => 'V',
'disana' => 'N',
'selama' => 'N',
'satu' => 'NUM',
'minggu' => 'N.'
]
];
foreach ($array as $data) {
// Find index to start
$index = array_search("Hotel", array_keys($data));
// Remove other keys
$new_array_starting_hotel = array_splice($data, $index, count($data));
// merge results
$merged[] = implode(' ', array_keys($new_array_starting_hotel));
}
print_r($merged);
And output will be like:
Array
(
[0] => Hotel Jayakarta Jaya sangat nyaman ditempati
[1] => Hotel menginap disana selama satu minggu
)
You can merge or implode all however you like.
Related
so, i have this
Array
(
[ModuleCode] => Array
(
[0] => MD001
[1] => MD002
[2] => MD004
[3] => MD005
)
[MD001] => Array
(
[insert] => on
[edit] => on
[delete] => on
)
[MD002] => Array
(
[insert] => on
[edit] => on
[delete] => on
)
[MD005] => Array
(
[insert] => on
[edit] => on
[delete] => on
[access_edit] => on
)
)
as you can see there are an array with ModuleCode as key.
After some try i can get this
MD001
insert => 1
edit => 1
delete => 1
access_edit => 0
MD002
insert => 1
edit => 1
delete => 1
access_edit => 0
MD004
insert => 0
edit => 0
delete => 0
access_edit => 0
MD005
insert => 1
edit => 1
delete => 1
access_edit => 1
with this script
$dataModul = $this->input->post('ModuleCode');
$field = array ("insert","edit","delete","access_edit");
for($x=0;$x<count($dataModul);$x++){
echo "<pre>".$dataModul[$x] . "<br>";
for($a=0;$a<count($field);$a++){
$subcheck = (isset($this->input->post($dataModul[$x])[$field[$a]])) ? 1 : 0;
echo $field[$a]. " => " . $subcheck . "<br>" ;
}
echo "<pre>";
}
Ok, here is what i want to achieve . from this part (for an example)
MD001
insert => 1
edit => 1
delete => 1
access_edit => 0
i want to make something like this
Update TableName set insert = 1, edit = 1, delete = 1 , access_edit = 0 where ModuleCode = 'MD001'
How can i achieve that ? thanks in advance
You can try this code:
You can call use the function as echo generate_query_string('MD004', $modules); where the first parameter is the module code and the second the whole array.
<?php
function generate_query_string( $module, $module_arr ) {
if( !isset( $module_arr[$module] ) ) { return false; } // return false if module does not exist
// set default values
$defaults = array(
'insert' => 0,
'edit' => 0,
'delete' => 0,
'access_edit' => 0
);
$settings = array_merge( $defaults, $module_arr[$module] ); // merge default values and the actual values
$settings = array_filter( $settings ); // remove items with 0 value since we don't need to include them on the query string
// render the query string values
$values = [];
foreach ($settings as $key => $value) {
$value = ( $value == 'on' )? 1 : 0;
$values[] = $key. ' = '. $value;
}
$values = implode(', ', $values);
return 'Update TableName set '. $values .' where ModuleCode = '. $module;
}
?>
I found the solustion. Here is what i do
First. i add a custom function ( ref : https://stackoverflow.com/a/42052020/6354277 )
function custom_function($input_array)
{
$output_array = array();
for ($i = 0; $i < count($input_array); $i++) {
for ($j = 0; $j < count($input_array[$i]); $j++) {
$output_array[key($input_array[$i])] = $input_array[$i][key($input_array[$i])];
}
}
return $output_array;
}
then i change my code to this.
function updateaccess(){
$dataModul = $this->input->post('ModuleCode');
$field = array ("insert","edit","delete","access_edit");
for($x=0;$x<count($dataModul);$x++){
for($a=0;$a<count($field);$a++){
$subcheck[$a] = (isset($this->input->post($dataModul[$x])[$field[$a]])) ? 1 : 0;
$mynewarray[$dataModul[$x]][] = array($field[$a] => $subcheck[$a]);
}
foreach ($mynewarray as $key => $value) {
$forSave[$dataModul[$x]] = $this->custom_function($value);
}
}
foreach ($forSave as $key2 => $values2) {
$this->mainmodel->updateRow(array("ModuleCode" => $key2),"user_modules", $values2 );
}
}
I have an array of arrays that contain array key with the value, here the example :
$text = [
[
'Hotel' => 'N',
'Jayakarta' => 'NE',
'Jaya' => 'NE',
'sangat' => 'ADV',
'nyaman' => 'ADJ',
'ditempati' => 'V.'
]
,
[
'Andi' => 'NOB',
'menginap' => 'V',
'di' => 'PREP',
'Hotel' => 'N',
'Neo' => 'NE',
'Malioboro => 'NE',
'selama' => 'N',
'satu' => 'NUM',
'minggu' => 'N.'
]
];
And I have array prefix that contain :
$prefix = [ [0] => Hotel [1] => Hostel [2] => Losmen [3] => Motel [4] => Penginapan [5] => Pesanggrahan [6] => Pondok [7] => Wisma ]
If $prefix elements are in $text array then I will check next array value of text. If array value is N or NE then I will make an output from prefix text until the end of array that contain value N or NE.
Here's what I am doing for the moment:
foreach($text as $index => $tok){
foreach ($tok as $tokkey => $tokvalue) {
if(in_array($tokkey, $prefix)){
echo $tokkey;
$next = next($tok);
if($tokvalue == "N" or $tokvalue == "NE"){
echo key($tok);
}
}
}
}
The output I got :
Hotel Jayakarta, Hotel menginap
The expected output should be:
- Hotel Jayakarta Jaya
- Hotel Neo Malioboro selama
Any help is much appreciated, Thank you.
Below are the steps I followed to get the desired out put.
1) If tokkey present in prefix array, call a function.
2) Create a new array starting from the element which matches with prefix array.
3) Loop the new array and check for next value to be either 'N' or 'NE'
4) Display those keys from array.
foreach($text as $index => $tok){
foreach ($tok as $tokkey => $tokvalue) {
if(in_array($tokkey, $prefix)){
getKeys($tokkey,$tok);
echo "<br/>";
}
}
}
function getKeys($tokkey,$tok){
$flag = 0;
echo $tokkey.' ';
foreach($tok as $k=>$v){
if($k==$tokkey){
$flag = 1;
}
if($flag){
$new_array[$k]=$v;
}
}
foreach($new_array as $k=>$v){
$ff = next($new_array);
if(($ff == 'NE' || $ff == 'N')){
echo key($new_array).' ';
}
}
}
Out Put:
Hotel Jayakarta Jaya Hotel Neo Malioboro selama
http://sandbox.onlinephpfunctions.com/code/2777d3ab3d34c941c23886d42e537cde7fff2351
From what I understand from your text, you need to replace your if() that only runs once, for a while() that runs while the next() string is NE or N.
foreach($text as $index => $tok){
foreach ($tok as $tokkey => $tokvalue) {
if(in_array($tokkey, $prefix)){
echo $tokkey;
next($tok);
while($tokvalue == "N" || $tokvalue == "NE")
{ echo key($tok);
next($tok);
$tokvalue = current($tok);
}
}
}
echo "\n";
}
}
I want to foreach the game name and info and each game must filter as platform_name.
$list = (object)[];
$list->egame =
[
(object)['platform_name'=>'TT', 'game'=>(object)[(object)['game_name'=>'game1', 'info'=>'test1'],(object)['game_name'=>'game2', 'info'=>'test2'],(object)['game_name'=>'game3', 'info'=>'test3']]],
(object)['platform_name'=>'TG', 'game'=>(object)[(object)['game_name'=>'game4', 'info'=>'test4'],(object)['game_name'=>'game5', 'info'=>'test5']]],
(object)['platform_name'=>'TBIN', 'game'=>(object)[(object)['game_name'=>'game6', 'info'=>'test6']]]
];
?>
Try this
$list = (object)[];
$list->egame =
[
(object)['platform_name' => 'TT', 'game' => (object)[(object)['game_name' => 'game1', 'info' => 'test1'], (object)['game_name' => 'game2', 'info' => 'test2'], (object)['game_name' => 'game3', 'info' => 'test3']]],
(object)['platform_name' => 'TG', 'game' => (object)[(object)['game_name' => 'game4', 'info' => 'test4'], (object)['game_name' => 'game5', 'info' => 'test5']]],
(object)['platform_name' => 'TBIN', 'game' => (object)[(object)['game_name' => 'game6', 'info' => 'test6']]]
];
$arr = (array)$list->egame;
for ($i = 0; $i < count($arr); $i++) {
foreach ($arr[$i] as $key => $value) {
$aa = (array)$arr[$i]->game;
foreach ($aa as $k => $v) {
echo $aa[$k]->game_name." ".$aa[$k]->info."<br/>";
}
echo "<br/>";
}
}
Here You go:
<?php
$list = (object)[];
$list->egame =
[
(object)['platform_name'=>'TT', 'game'=>(object)[(object)['game_name'=>'game1', 'info'=>'test1'],(object)['game_name'=>'game2', 'info'=>'test2'],(object)['game_name'=>'game3', 'info'=>'test3']]],
(object)['platform_name'=>'TG', 'game'=>(object)[(object)['game_name'=>'game4', 'info'=>'test4'],(object)['game_name'=>'game5', 'info'=>'test5']]],
(object)['platform_name'=>'TBIN', 'game'=>(object)[(object)['game_name'=>'game6', 'info'=>'test6']]]
];
foreach ( $list->egame as $eg ) {
foreach ( $eg->game as $game ) {
echo "game: " . $game->game_name . " info: " . $game->info . "<br>";
}
}
?>
Edit #1
Includes platform:
foreach ( $list->egame as $eg ) {
foreach ( $eg->game as $game ) {
echo "platform: " . $eg->platform_name . " game: " . $game->game_name . " info: " . $game->info . "<br>";
}
}
I have this code (sample out of 24500 line code is provided below) in php language and I want to use this code in my project but problem is that, It is converted to unknown format.
$O = array( function($XB0) use (&$O)
{
$lej = array( 6323 => "sq", 1630 => "invalid_perfectmoney_account", 1462 => "Lebanon", 1867 => "/<br> Cash Passport Result URL - ", 261 => "alertpay_from_account", 5096 => " + interval 1 hour and deposit_id = ", 7651 => " group by t.id ", 845 => "select email from hm2_users where id = 1", 5952 => "Location: ", 9642 => "]", 3792 => "select username from hm2_users where id = ", 1591 => "update hm2_users set verify = 1 where id = " );
return $lej[$XB0];
}
, 289, 691, 561, 691, function($LBV) use (&$O)
{
$b4X = array( 1591 => "zero_amount", 3792 => "l dS of F Y h:i:s A", 1630 => "QXCR58Z29CNWP8GVKDXP", 1867 => "India", 845 => "Define Key String (random string)", 7651 => "actual_amount", 5096 => "k", 261 => "</ns1:Receiver> <ns1:Currency>", 5952 => ") as create_account_date, now() - interval 2 minute > l_e_t as should_count from hm2_users where ", 9642 => "update hm2_users set password = ", 1462 => "Romania", 6323 => ", ip_reg = ", 4683 => "qplans" );
return $b4X[$LBV];
}
, 515, 901, 487, 779, function($wjS, $I8j, $Sbx) use (&$O)
{
if( $I8j[$O[327](7651)] == $O[1299](7651) )
{
$S3J = 0;
print " ";
if( $I8j[$O[90](5096)] != "" )
{
$S3J = 1;
$O[703](7651);
print $O[703](7651);
$O[161](7651);
print $I8j[$O[161](7651)];
$O[1184](7651);
print $O[1184](7651);
$O[161](7651);
print $I8j[$O[161](7651)];
$O[478](5096);
print $O[478](5096);
}
$O[703](7651);
print $O[703](7651);
$O[161](7651);
print $I8j[$O[161](7651)];
$O[828](7651);
print $O[828](7651);
print ($S3J ? $O[814](7651) : "");
$O[39](7651);
print $O[39](7651);
$O[1043](7651);
print $I8j[$O[1043](7651)];
$O[194](5096);
print $O[194](5096);
$O[380]($I8j[$O[1016](7651)]);
print $O[380]($I8j[$O[1016](7651)]);
$O[99](5096);
print $O[99](5096);
if( $S3J )
{
$O[996](7651);
print $O[996](7651);
$O[1043](7651);
print $I8j[$O[1043](7651)];
$O[467](7651);
print $O[467](7651);
$O[161](7651);
print $I8j[$O[161](7651)];
$O[1244](5096);
print $O[1244](5096);
$O[161](7651);
print $I8j[$O[161](7651)];
$O[1236](7651);
print $O[1236](7651);
}
$O[1063](845);
print $O[1063](845);
}
else
{
if( $I8j[$O[327](7651)] == $O[582](5096) )
{
$O[1189](5096);
print $O[1189](5096);
$O[1043](7651);
print $I8j[$O[1043](7651)];
$O[103](7651);
print $O[103](7651);
$O[380](($I8j[$O[90](5096)] != "" ? $I8j[$O[90](5096)] : $I8j[$O[100](7651)]));
print $O[380](($I8j[$O[90](5096)] != "" ? $I8j[$O[90](5096)] : $I8j[$O[100](7651)]));
$O[536](7651);
print $O[536](7651);
$O[380]($I8j[$O[1016](7651)]);
print $O[380]($I8j[$O[1016](7651)]);
$O[446](7651);
print $O[446](7651);
}
else
{
if( $I8j[$O[327](7651)] == $O[793](7651) )
{
$O[965](5096);
print $O[965](5096);
$O[1043](7651);
print $I8j[$O[1043](7651)];
$O[888](7651);
print $O[888](7651);
foreach( $DSo as $lIx => $RdI )
{
print " ";
if( $RdI[$O[735](7651)] == 1 )
{
$O[327](5096);
print $O[327](5096);
print $lIx;
$O[937](5096);
print $O[937](5096);
$O[90](5096);
print ($lIx == $I8j[$O[90](5096)] ? $O[352](7651) : "");
$O[901](7651);
print $O[901](7651);
$O[1016](5096);
print $RdI[$O[1016](5096)];
$O[598](5096);
print $O[598](5096);
}
print " ";
}
$O[271](5096);
print $O[271](5096);
return NULL;
}
else
{
if( $I8j[$O[327](7651)] == $O[183](7651) )
{
$xb4 = $I8j[$O[183](7651)];
if( $xb4 )
{
$xb4($wjS, $I8j, $Sbx);
print $xb4($wjS, $I8j, $Sbx);
}
}
}
}
}
}
, 373, 154, function($x71) use (&$O)
{
$Bsd = array( 6357 => "min_user_password_length", 4683 => "\"/>", 3792 => "\"> <input type=hidden name=ureturn value=\"", 1462 => "~/[\\w\\d]+\\.php.*~", 9642 => "crate", 5952 => " to ", 261 => "last_pay_date", 5096 => "on", 7651 => "/_secret_key/", 845 => " from hm2_history group by user_id, ec", 1867 => "%.0", 1630 => "https://chart.googleapis.com/chart?chs=200x200&chld=M|0&cht=qr&chl=", 6323 => "Purse", 1591 => "recipient", 2607 => "last_withdrawals", 2429 => "Russian Federation", 7210 => "use_transaction_code", 4186 => "2 weeks" );
return $Bsd[$x71];
}
I have two questions from it:
What php format is this code?
How to convert it back into readable format?
Thanks in advance..
Right now i got an array which has some sort of information and i need to create a table from it. e.g.
Student{
[Address]{
[StreetAddress] =>"Some Street"
[StreetName] => "Some Name"
}
[Marks1] => 100
[Marks2] => 50
}
Now I want to create database table like which contain the fields name as :
Student_Address_StreetAddress
Student_Address_StreetName
Student_Marks1
Student_Marks2
It should be recursive so from any depth of array it can create the string in my format.
You can use the RecursiveArrayIterator and the RecursiveIteratorIterator (to iterate over the array recursively) from the Standard PHP Library (SPL) to make this job relatively painless.
$iterator = new RecursiveIteratorIterator(new RecursiveArrayIterator($arr));
$keys = array();
foreach ($iterator as $key => $value) {
// Build long key name based on parent keys
for ($i = $iterator->getDepth() - 1; $i >= 0; $i--) {
$key = $iterator->getSubIterator($i)->key() . '_' . $key;
}
$keys[] = $key;
}
var_export($keys);
The above example outputs something like:
array (
0 => 'Student_Address_StreetAddress',
1 => 'Student_Address_StreetName',
2 => 'Student_Marks1',
3 => 'Student_Marks2',
)
(Working on it, here is the array to save the trouble):
$arr = array
(
'Student' => array
(
'Address' => array
(
'StreetAddress' => 'Some Street',
'StreetName' => 'Some Name',
),
'Marks1' => '100',
'Marks2' => '50',
),
);
Here it is, using a modified version of #polygenelubricants code:
function dfs($array, $parent = null)
{
static $result = array();
if (is_array($array) * count($array) > 0)
{
foreach ($array as $key => $value)
{
dfs($value, $parent . '_' . $key);
}
}
else
{
$result[] = ltrim($parent, '_');
}
return $result;
}
echo '<pre>';
print_r(dfs($arr));
echo '</pre>';
Outputs:
Array
(
[0] => Student_Address_StreetAddress
[1] => Student_Address_StreetName
[2] => Student_Marks1
[3] => Student_Marks2
)
Something like this maybe?
$schema = array(
'Student' => array(
'Address' => array(
'StreetAddresss' => "Some Street",
'StreetName' => "Some Name",
),
'Marks1' => 100,
'Marks2' => 50,
),
);
$result = array();
function walk($value, $key, $memo = "") {
global $result;
if(is_array($value)) {
$memo .= $key . '_';
array_walk($value, 'walk', $memo);
} else {
$result[] = $memo . $key;
}
}
array_walk($schema, 'walk');
var_dump($result);
I know globals are bad, but can't think of anything better now.
Something like this works:
<?php
$arr = array (
'Student' => array (
'Address' => array (
'StreetAddress' => 'Some Street',
'StreetName' => 'Some Name',
),
'Marks1' => array(),
'Marks2' => '50',
),
);
$result = array();
function dfs($data, $prefix = "") {
global $result;
if (is_array($data) && !empty($data)) {
foreach ($data as $key => $value) {
dfs($value, "{$prefix}_{$key}");
}
} else {
$result[substr($prefix, 1)] = $data;
}
}
dfs($arr);
var_dump($result);
?>
This prints:
array(4) {
["Student_Address_StreetAddress"] => string(11) "Some Street"
["Student_Address_StreetName"] => string(9) "Some Name"
["Student_Marks1"] => array(0) {}
["Student_Marks2"] => string(2) "50"
}
function getValues($dataArray,$strKey="")
{
global $arrFinalValues;
if(is_array($dataArray))
{
$currentKey = $strKey;
foreach($dataArray as $key => $val)
{
if(is_array($val) && !empty($val))
{
getValues($val,$currentKey.$key."_");
}
else if(!empty($val))
{
if(!empty($strKey))
$strTmpKey = $strKey.$key;
else
$strTmpKey = $key;
$arrFinalValues[$strTmpKey]=$val;
}
}
}
}