How to convert array php with an object json - arrays

I have one array with PHP like this:
Array (
[SCAN] => Array (
[SITE] => Array ([0] => http://www.site.com )
[DOMAIN] => Array ([0] => www.site.com )
[IP] => Array ( [0] => 134.0.xx.xxx )
)
[SYSTEM] => Array ( [NOTICE] )
)
And I need to convert an object JSON with json_encode() function. But the problem is returns me this:
"Array\n(\n[SCAN] => Array\n (\n [SITE] => Array\n (\n [0] => http:\/\/www.site.com \n)\n\n"

Related

How Elimante Duplicate Data in multiple Array

Houston I have a Problem. I have this preg_match_all the response is many array with duplicate numbers (the original data has these duplicates). I need to eliminate duplicate numbers and transfer to one variable. I Try with array_unique() and array_merge(). Thx for your help.
preg_match_all('~x">([^"]*)<\/h4>|[0-9]{5}~',$preg_data,$item, PREG_SET_ORDER);
Output:
Array ( )
Array ([0] => x"> 13:30 - Denver</h4> [1] => 13:30 - Denver)
Array ( )
Array ([0] => 69275)Array([0] => Array([0] => 69275[1] => 69275))
Array ([0] => 69275)Array([0] => Array([0] => 69275[1] => 69275))
Array ([0] => 69275)Array([0] => Array([0] => 69275[1] => 69275))
Array ()
Array ([0] => x"> 16:00 - Miami</h4>[1] => 16:00 - Miami)
Array ()
Array ([0] => 69275)Array([0] => Array([0] => 69275[1] => 69280))
Array ([0] => 69275)Array([0] => Array([0] => 69275[1] => 69280))
Array ([0] => 69275)Array([0] => Array([0] => 69275[1] => 69280))
if (#preg_match_all('/([0-9]{5})/',$item[0],$match, PREG_SET_ORDER)); {
print_r($match);}
Array ( )
Array ( [0] => Array ( [0] => 69268 [1] => 69268 ))
Array ( [0] => Array ( [0] => 69268 [1] => 69268 ))
Array ( )
Array ( [0] => Array ( [0] => 69270 [1] => 69270 ))
Array ( [0] => Array ( [0] => 69270 [1] => 69270 ))
Array ( )
Array ( )
Array ( [0] => Array ( [0] => 69270 [1] => 69270 ))
Array ( [0] => Array ( [0] => 69270 [1] => 69270 ))
Array ( )
Array ( )
Array ( [0] => Array ( [0] => 69270 [1] => 69270 ))
Array ( [0] => Array ( [0] => 69270 [1] => 69270 ))
Array ( )
I use this combinations
preg_match_all('~x">([^"]*)<\/h4>|[0-9]{5}~',$preg_data,$data_mach);
$data_clean= array_values(array_filter(array_unique($data_mach[0])));
preg_mach to extrac data
array_unique to eliminate duplicates
array_filter to eliminate blank
array_values to work only numbers.
Done.

Extract specific values from stdClass Object

I have the following stdClass Object contained within $response:
stdClass Object ( [domain] => stdClass Object ( [id] => d1111111 [spamscore] => 75 [rejectscore] => 200 ) [domainalias] => Array ( ) [wildcard] => Array ( ) [catchall] => Array ( ) [forward] => Array ( ) [mailbox] => Array (
[0] => stdClass Object ( [highEmailNotification] => [id] => m1111111 [lastPasswordChange] => 2020-02-19T22:41:12+00:00 [local] => mailbox1 [lowEmailNotification] => [quotaMB] => 10240 [receive] => 1 [rejectscore] => [send] => 1 [spamscore] => [usageMB] => 0 [enabled] => 1 )
[1] => stdClass Object ( [highEmailNotification] => [id] => m2222222 [lastPasswordChange] => 2020-02-17T15:46:21+00:00 [local] => mailbox2 [lowEmailNotification] => [quotaMB] => 10240 [receive] => 1 [rejectscore] => [send] => 1 [spamscore] => [usageMB] => 0 [enabled] => 1 )
[2] => stdClass Object ( [highEmailNotification] => [id] => m3333333 [lastPasswordChange] => 2020-02-19T15:00:36+00:00 [local] => mailbox3 [lowEmailNotification] => [quotaMB] => 1024 [receive] => 1 [rejectscore] => 0 [send] => 1 [spamscore] => 75 [usageMB] => 0 [enabled] => 1 ) ) [spamblacklist] => Array ( ) [spamwhitelist] => Array ( ) [responder] => Array ( ) [name] => domain.com )
I need to convert it into an array and extract particular values, i.e. [id] and [local] from it.
Speed is also an issue, as this array will grow to thousands of items, so if there is other, quicker way than 'foreach' it would be better.
I used some suggestions from here, such as:
$array = json_decode(json_encode($response), True);
foreach ($array as $var)
{
echo $var['id'] . ' - ' . $var['local'] . "<br>";
}
and got partial success with the results:
d1111111 -
-
-
-
-
i - i
(so it found the very first [id] value)
it however missed the most important values I am after.
What I need to get is:
m1111111 - mailbox1
m2222222 - mailbox2
m3333333 - mailbox3
Any suggestions are greatly appreciated.
The values you're trying to print is in a nested array within $response.
Try this.
$array = json_decode(json_encode($response['mailbox']), True);
foreach ($array as $var)
{
echo $var['id'] . ' - ' . $var['local'] . "<br>";
}

how to get an array list into json format

how to get an array list into json format like json_encode([[money,1872416],[moneytransfer,1382619],[api,1986920]]).
My array looks like this:
Array
(
[0] => Array
(
[0] => Array
(
[money] => 1
[moneytransfer] => 4340
[api] => 6546
)
)
[1] => Array
(
[0] => Array
(
[money] => 2
[moneytransfer] => 546546
[api] => 6546
)
)
[2] => Array
(
[0] => Array
(
[money] => 3
[moneytransfer] => 6546
[api] => 6546
)
)
)
This question has answered before : "Can an array be top-level JSON-text?" –
Per the JSON RFC, an array is indeed legal top-level JSON-text: "A JSON text is a serialized object or array."

Save Multiple index array value in cakephp

I want to save my array with multiple indexing array values.
Demo code
Array
(
[CodeConfiguration] => Array
(
[0] => Array
(
[LineNo] => 1
[IsDirty] =>
)
)
[ObjectAccountConfiguration] => Array
(
[0] => Array
(
[LineNo] => 1
[IsDirty] => 2
)
)
[TaxConfiguration] => Array
(
[0] => Array
(
[LineNo] => 2
[IsDirty] => 1
)
[1] => Array
(
[LineNo] => 1
[IsDirty] => 1
)
)
)
I want to save this array values direct into table .Table name is audit_trail_details.
so please suggest mi proper solution for how to save this data into table.
You need to use saveAssociated and parse your array as the first parameter:-
$this->AuditTrailDetail->saveAssociated($data);
You will also need to make sure $data contains the AuditTrailDetail you are saving the associated data against. For example:-
Array
(
[AuditTrailDetail] => Array
(
[id] => 1
)
[CodeConfiguration] => Array
(
[0] => Array
(
[LineNo] => 1
[IsDirty] =>
)
)
[ObjectAccountConfiguration] => Array
(
[0] => Array
(
[LineNo] => 1
[IsDirty] => 2
)
)
[TaxConfiguration] => Array
(
[0] => Array
(
[LineNo] => 2
[IsDirty] => 1
)
[1] => Array
(
[LineNo] => 1
[IsDirty] => 1
)
)
)

How do I format the output array in CakePHP

Let's say I have an index action where I want to get a list of projects:
$this->Project->find('all', array('order' => 'Project.modified DESC', 'conditions' => array('Project.user_id' => 1)));
It works well and returns the following array:
Array ( [0] => Array ( [Project] => Array ( [id] => 2 [title] => test project ) ) [1] => Array ( [Project] => Array ( [id] => 1 [title] => first project ) ) )
How do I modify the find function, so it returns the array in the following format:
Array ( [projects] => Array ( [0] => Array ( [id] => 2 [title] => test project ) [1] => Array ( [id] => 1 [title] => first project ) ) )
Thank you!
You could use the Set::combine() utility method to do this. I've used it for similar means as so:
public function groupByMenu() {
return Set::combine (
$this->find (
'all',
array (
'conditions' => array ( 'NavItem.active' => 1 ),
'order' => 'NavMenuItem.display_order'
)
),
'{n}.NavItem.title',
'{n}',
'{n}.NavMenu.id'
);
}
The code above takes a set of navigation items and reorganizes them so that they're grouped by the menu(s) they are displayed within.
It's not really clear if it's the fact that the result is under 'Project' rather than 'projects', but if you don't like that it's under [0] I believe you could use PHPs array_shift:
$result = $this->Project->find('all', array('order' => 'Project.modified DESC', 'conditions' => array('Project.user_id' => 1)));
$result = array_shift($result);
The result will be:
Array ( [Project] => Array ( [id] => 2 [title] => test project ) ) [1] => Array ( [Project] => Array ( [id] => 1 [title] => first project ) )

Resources